C Program to implement solution for Stocks Buy and Sell problem Part 2 - LeetCode


 

// Program by Akash Tripathi (@proakash256) 
 
// Given an Array of the Prices
// of a Stock on Multiple Days.

// Find the Maximum Profit by Buying
// and Selling Stocks at multiple times.
// You can hold only one Stock at a time.

#include <stdio.h>
int main()
{
    int n;
    printf("Enter the number of elements : ");
    scanf("%d", &n);
    int ar[n];
    printf("\nEnter the elements :\n");
    for (int i = 0i < ni = i + 1)
        scanf("%d", &ar[i]);
    int max_profit = 0;
    for(int i = 0i < (n - 1); i = i + 1)
    {
        if(ar[i + 1] > ar[i])
            max_profit += (ar[i + 1] - ar[i]);
    }
    printf("The Maximum Profit is %d." , max_profit);
    return 0;
}

Comments