Answer to Question #331312 in C for shiva

Question #331312

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


1
Expert's answer
2022-04-22T04:43:48-0400
#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;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS