diff --git a/best_time_to_buy_sell_stock.cpp b/best_time_to_buy_sell_stock.cpp new file mode 100644 index 0000000..9821e01 --- /dev/null +++ b/best_time_to_buy_sell_stock.cpp @@ -0,0 +1,29 @@ +// C++ code for the above approach +#include +using namespace std; + +int maxProfit(int prices[], int n) +{ + int buy = prices[0], max_profit = 0; + for (int i = 1; i < n; i++) { + + // Checking for lower buy value + if (buy > prices[i]) + buy = prices[i]; + + // Checking for higher profit + else if (prices[i] - buy > max_profit) + max_profit = prices[i] - buy; + } + return max_profit; +} + +// Driver Code +int main() +{ + int prices[] = { 7, 1, 5, 6, 4 }; + int n = sizeof(prices) / sizeof(prices[0]); + int max_profit = maxProfit(prices, n); + cout << max_profit << endl; + return 0; +}