C Program For Stock Buy Sell To Maximize Profit - GeeksforGeeks

30 Dec, 2021
Efficient approach: If we are allowed to buy and sell only once, then we can use following algorithm. Maximum difference between two elements. Here we are allowed to buy and sell multiple times. Following is the algorithm for this problem.  
Find the local minima and store it as starting index. If not exists, return.Find the local maxima. and store it as an ending index. If we reach the end, set the end as the ending index.Update the solution (Increment count of buy-sell pairs)Repeat the above steps if the end is not reached.
Find the local minima and store it as starting index. If not exists, return.
Find the local maxima. and store it as an ending index. If we reach the end, set the end as the ending index.
Update the solution (Increment count of buy-sell pairs)
Repeat the above steps if the end is not reached.
C
// Program to find best buying and // selling days#include <stdio.h>  // Solution structurestruct Interval {    int buy;    int sell;};  // This function finds the buy and sell // schedule for maximum profitvoid stockBuySell(int price[], int n){    // Prices must be given for at     // least two days    if (n == 1)        return;      // Count of solution pairs    int count = 0;       // Solution vector    Interval sol[n / 2 + 1];      // Traverse through given price array    int i = 0;    while (i < n - 1)     {        // Find Local Minima. Note that the         // limit is (n-2) as we are comparing         // present element to the next element.        while ((i < n - 1) &&                (price[i + 1] <= price[i]))            i++;          // If we reached the end, break as no         // further solution possible        if (i == n - 1)            break;          // Store the index of minima        sol[count].buy = i++;          // Find Local Maxima.  Note that the         // limit is (n-1) as we are comparing         // to previous element        while ((i < n) &&                (price[i] >= price[i - 1]))            i++;          // Store the index of maxima        sol[count].sell = i - 1;          // Increment count of buy/sell pairs        count++;    }      // Print solution    if (count == 0)        printf(        "There is no day when buying the stock will make profitn");    else     {        for (int i = 0; i < count; i++)            printf(            "Buy on day: %dt Sell on day: %dn", sol[i].buy, sol[i].sell);    }      return;}  // Driver codeint main(){    // Stock prices on consecutive days    int price[] = {100, 180, 260,                    310, 40, 535, 695};    int n = sizeof(price) / sizeof(price[0]);      // Function call    stockBuySell(price, n);      return 0;}
Output:
Buy on day: 0     Sell on day: 3
Buy on day: 4     Sell on day: 6
Time Complexity: The outer loop runs till I become n-1. The inner two loops increment value of I in every iteration. So overall time complexity is O(n)
Please refer complete article on Stock Buy Sell to Maximize Profit for more details!
Accolite
Amazon
Directi
Flipkart
Goldman Sachs
Hike
MakeMyTrip
Microsoft
Morgan Stanley
Ola Cabs
Oracle
Paytm
Pubmatic
Quikr
Samsung
SAP Labs
Sapient
Swiggy
Walmart
Arrays
C Programs
Paytm
Flipkart
Morgan Stanley
Accolite
Amazon
Microsoft
Samsung
Hike
MakeMyTrip
Ola Cabs
Oracle
Walmart
Goldman Sachs
Directi
SAP Labs
Quikr
Pubmatic
Sapient
Swiggy
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Reversal algorithm for array rotation
Window Sliding Technique
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Strings in C
Arrow operator -> in C/C++ with Examples
Header files in C/C++ and its uses
C Program to read contents of Whole File
UDP Server-Client implementation in C