#include<stdio.h>
void getBestTime(int stocks[], int size)
{
int buy = 0, sell = 0;
int min = 0;
int i;
int maxDiff = 0;
buy = sell = 0;
for (i = 0; i < size; i++)
{
if (stocks[i] < stocks[min])
min = i;
int diff = stocks[i] - stocks[min];
if (diff > maxDiff)
{
buy = min;
sell = i;
maxDiff = diff;
}
}
printf("\nThe day to buy is- %d at price %d",buy
, stocks
[buy
]); printf("\nThe day to sell is- %d at price %d",sell
, stocks
[sell
]); }
int main(void)
{
int stocks[] = {3, 6, 10, 2, 66, 43, 1, 23};
int buy = 0;
int sell = 0;
getBestTime(stocks, 8);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCnZvaWQgZ2V0QmVzdFRpbWUoaW50IHN0b2Nrc1tdLCBpbnQgc2l6ZSkKewoJaW50IGJ1eSA9IDAsIHNlbGwgPSAwOwoJCglpbnQgbWluID0gMDsKCWludCBpOwoJaW50IG1heERpZmYgPSAwOwoJYnV5ID0gc2VsbCA9IDA7CgkKCWZvciAoaSA9IDA7IGkgPCBzaXplOyBpKyspCgl7CgkJaWYgKHN0b2Nrc1tpXSA8IHN0b2Nrc1ttaW5dKQoJCQltaW4gPSBpOwoJCWludCBkaWZmID0gc3RvY2tzW2ldIC0gc3RvY2tzW21pbl07CgkJCgkJaWYgKGRpZmYgPiBtYXhEaWZmKQoJCXsKCQkJYnV5ID0gbWluOwoJCQlzZWxsID0gaTsKCQkJbWF4RGlmZiA9IGRpZmY7CgkJfQoJfQoJCglwcmludGYoIlxuVGhlIGRheSB0byBidXkgaXMtICVkIGF0IHByaWNlICVkIixidXksIHN0b2Nrc1tidXldKTsKCXByaW50ZigiXG5UaGUgZGF5IHRvIHNlbGwgaXMtICVkIGF0IHByaWNlICVkIixzZWxsLCBzdG9ja3Nbc2VsbF0pOwp9CgppbnQgbWFpbih2b2lkKQp7CglpbnQgc3RvY2tzW10gPSB7MywgNiwgMTAsIDIsIDY2LCA0MywgMSwgMjN9OwoJCglpbnQgYnV5ID0gMDsKCWludCBzZWxsID0gMDsKCQoJZ2V0QmVzdFRpbWUoc3RvY2tzLCA4KTsKCglyZXR1cm4gMDsKfQ==