The sequence of price is make more sense if buy price is less than next
value then you can buy even its not the min and next day you can sell all
and
Buy again when min price stock come.
Eg 6, 10, 5, 7, 2,7.

There are many case need to consider the above is the only one scenario.
Other like random order profits and loss so need to decide min n max for a
interval.

Here is no profit
9 8 7 6 5 4 3 2 1 :(.
Sent from my Windows Phone
------------------------------
From: Mukul Gupta
Sent: 08/05/2012 8:47 PM
To: algogeeks@googlegroups.com
Subject: Re: [algogeeks] DE Shaw written test

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.

-- 
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