you want to buy a particular stock at its lowest price and sell it later at its highest price,since the stock market is unpredictable,you steal the price plans of a company for this stock for the next N days.
Find the best price you can get to buy this stock to achieve maximum profit.
Note:The initial price of the stock is 0.
Input1:Nnumber of days.
Input2:Array representing changing stock price for the day.
Output: Your function must return the best price to buy the stock at.
Example:
Input1:5
Input2:{-39957,-17136,35466,21820,-26711}
Output:-57093
#include <stdio.h>
int main()
{
int N, i, best_price, price;
printf("Input number of days: ");
scanf("%d", &N);
int arr[N];
printf("Enter array:\n");
for (i=0; i<N; i++)
scanf("%d", &arr[i]);
printf("\nArray:\n");
price = 0;
best_price = 0;
for (i=0; i<N; i++)
{
printf("%d ", arr[i]);
price = price + arr[i];
if (best_price > price)
best_price = price;
}
printf("\nBest price: %d\n", best_price);
return 0;
}
Comments
Leave a comment