int MaxStockGain(int arr[], int size) { if(size <= 0) return 0;
int curMin = arr[0]; int MaxGain = 0; for(int i = 1; i < size; ++i) { if (arr[i] < curMin) { curMin = arr[i]; } int currGain = arr[i] - curMin; if(currGain > MaxGain) { MaxGain = currGain; } } return MaxGain; } On Tue, Jul 13, 2010 at 4:02 PM, srikanth sg <srikanthini...@gmail.com>wrote: > how you do that in O(n)... > can you explain ??? > > > On Mon, Jul 12, 2010 at 11:37 PM, amit <amitjaspal...@gmail.com> wrote: > >> Stock prices are given to you at various time intervals. p1, p2, >> p3,... you are allowed to buy and sell only once each. So write a >> program to find the index where you would buy and where you would sell >> to maximize profit >> >> Correct me if i am wrong --> >> >> I think we can solve it in O(n) we simple have to find max(A[j]-A[i]) >> with j>i. >> This is already discussed or am i missing something?? >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algoge...@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@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 algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- With Regards, Jalaj Jaiswal +919026283397 B.TECH IT IIIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@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.