fork(7) download
  1. #include<stdio.h>
  2.  
  3. void getBestTime(int stocks[], int size)
  4. {
  5. int buy = 0, sell = 0;
  6.  
  7. int min = 0;
  8. int i;
  9. int maxDiff = 0;
  10. buy = sell = 0;
  11.  
  12. for (i = 0; i < size; i++)
  13. {
  14. if (stocks[i] < stocks[min])
  15. min = i;
  16. int diff = stocks[i] - stocks[min];
  17.  
  18. if (diff > maxDiff)
  19. {
  20. buy = min;
  21. sell = i;
  22. maxDiff = diff;
  23. }
  24. }
  25.  
  26. printf("\nThe day to buy is- %d at price %d",buy, stocks[buy]);
  27. printf("\nThe day to sell is- %d at price %d",sell, stocks[sell]);
  28. }
  29.  
  30. int main(void)
  31. {
  32. int stocks[] = {3, 6, 10, 2, 66, 43, 1, 23};
  33.  
  34. int buy = 0;
  35. int sell = 0;
  36.  
  37. getBestTime(stocks, 8);
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
The day to buy is- 3 at price 2
The day to sell is- 4 at price 66