Thanks for pointing out the mistake.Though my code will correctly calculate
the max_profit but I must keep track of the buying_day.I have made some
modification in my code.Hope it works fine now.


int min = a[0];  // initialized to first element
int max_profit = 0;  //when you buy and sell on same day
int buying_day = a[0];
for ( int i = 1; i < n; i++ ){

      if ( max_profit < (a[i] - min ) ){
              max_profit = a[i] - min;
              buying_day = min;
      }


      if ( a[i] < min )
               min = a[i];
}

Finally. I'll have buying_day and  max_profit, so if you need to find the
selling day you can easily calculate :

Selling day = buying_day+max_profit;

Correct me if I'm wrong.

On Sun, Aug 5, 2012 at 5:43 PM, Arun Kindra <arunkin...@gmail.com> wrote:

> @harsha : yes, the problem is if u r finding only min and max value, it
> might happen that u sell the stock before buying. Ex-  int a [ ] = { 5, 10,
> 4, 6, 7 }; the min value is 4 and max is 10 and 10 comes before 4, means u
> sell the stock before buying.
> and i think the sol given by mukul does the same mistake.we need to keep
> track this case also whether the day(array index) i m buying is not more
> than the day(array index) we are selling the stock.
>
> *correct me if  m wrong*.....
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to