best time to buy sell Algorithm

The best time to buy-sell algorithm, commonly known as the Maximum Subarray Problem, is an optimization technique used in finance and economics to determine the most profitable time to buy and sell stocks or other financial instruments. The algorithm is designed to identify the optimal entry and exit points in a given market by analyzing historical price data and identifying trends or patterns that can accurately predict future price movements. The best time to buy-sell algorithm is particularly helpful for individual investors and traders, as well as financial institutions, who aim to maximize their profits and minimize their risks when participating in trading activities. The core concept behind the best time to buy-sell algorithm is to analyze the changes in price over a specific period, such as daily or weekly price fluctuations, and calculate the maximum difference between the lowest and highest points within that time frame. By comparing the potential profit of each possible buying and selling point, the algorithm can determine the most profitable combination of entry and exit points. This approach allows traders and investors to make more informed decisions about when to buy and sell their stocks or other financial instruments, potentially leading to higher returns on their investments. Additionally, the best time to buy-sell algorithm can be adapted to various market conditions and types of financial instruments, making it a versatile and valuable tool for those involved in the financial markets.
/*
 * You are provided a vector of numbers, where each number represents
 * price of a stock on ith day. If you are permitted to only complete
 * one transaction per day (i.e buy one and sell one stock), design
 * an algorithm to find the maximum profit.
 * 
 * Input: [7, 1, 5, 3, 6, 4]
 * Output: 5 
 * max. difference = 6-1 = 5
 * (not 7-1 = 6, as selling price needs to be larger than buying price)
 * 
 * Approach:
 * 
 * We need to find the maximum difference between two numbers in the array
 * (which would be maximum profit) such that selling price(second number)
 *  is bigger than buying price.
 */

#include <iostream>
#include <vector>
#include <limits>

int maximum_profit(const std::vector<int>& prices)
{
    int min_price = std::numeric_limits<int>::max();
    int max_profit = 0;
    for (int i = 0; i < prices.size(); ++i) {
        if (prices[i] < min_price) {
            min_price = prices[i];
        } else if (prices[i] - min_price > max_profit) {
            max_profit = prices[i] - min_price;
        }
    }
    return max_profit;
}

void print_vector(const std::vector<int>& vec) {
    for (auto v : vec) {
        std::cout << v << " ";
    }
    std::cout << std::endl;
}

int main()
{
    std::vector<int> prices{7, 1, 5, 3, 6, 4};
    std::cout << "Prices of stocks in last " << prices.size()
        << " days:" << std::endl;
    print_vector(prices);
    std::cout << "Maximum profit: " << maximum_profit(prices)
        << std::endl;

    return 0;
}

LANGUAGE:

DARK MODE: