C Program
#include<stdio.h> int main() { int n, a[100], min=1e9, max=0; scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d", &a[i]); if(a[i] < min) min = a[i]; if(a[i] - min > max) max = a[i] - min; } printf("%d", max); }
C Output
Input: 6 7 1 5 3 6 4 Output: 5
C++ Program
#include<iostream> using namespace std; int main() { int n, a[100], minP=1e9, maxP=0; cin >> n; for(int i=0; i<n; i++) { cin >> a[i]; minP = min(minP, a[i]); maxP = max(maxP, a[i] - minP); } cout << maxP; }
C++ Output
Input: 6 7 1 5 3 6 4 Output: 5
JAVA Program
import java.util.*; class Stock { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), min = Integer.MAX_VALUE, max = 0; for(int i=0; i<n; i++) { int p = sc.nextInt(); min = Math.min(min, p); max = Math.max(max, p - min); } System.out.print(max); } }
JAVA Output
Input: 7 9 2 4 6 1 7 8 Output: 7
Python Program
a = list(map(int, input().split())) minP = float('inf') maxP = 0 for p in a: minP = min(minP, p) maxP = max(maxP, p - minP) print(maxP)
Python Output
Input: 3 8 2 5 1 7 Output: 6
In-Depth Learning – Full Concept in Paragraphs
What Is the Stock Buy and Sell Issue?
This traditional issue is finding when to buy and sell a stock in order to maximize profit, having an array of prices over days. You can only buy once and sell once, and you should sell after buying.
The problem is to find the pair (buy_day, sell_day) such that:
Profit = prices[sell_day] - prices[buy_day] is maximum
How the Code Works (Greedy Logic)
We traverse the array once (O(n) time) and:
Maintain the minimum price encountered so far (possible buy day).
At each price, compute possible profit by subtracting this min price.
Update maximum profit if the current profit is greater.
This greedy strategy works because we always keep in mind the best so far price to buy and compute profits from it.
Example
Let's consider:
Prices: [7, 1, 5, 3, 6, 4]
Minimum till now: 1
Max profit: max(0, 5−1=4, 3−1=2, 6−1=5) → 5
Buy at ₹1 (day 2), sell at ₹6 (day 5) → Profit = ₹5
Real-Life Analogy
Suppose you're following a stock in real life:
Each day, you note its price.
You wish to purchase at the lowest level and sell at the highest level later.
Rather than calculating all pair-wise comparisons (which is slow), you keep the lowest cost so far as you go. You ask each day:
"If I bought at the lowest price I've seen thus far, how much profit could I make today?"
This produces the optimal solution in real-time without storing or sorting.
Where and When Is It Used?
This pattern comes up frequently:
Stock market profit simulations
E-commerce price tracking
Data analysis on highs and lows
Interview rounds in FAANG and tech companies
Competitive programming
It trains one to recognize min-max patterns, use greedy algorithms, and reduce space and time.
Time and Space Complexity
Measure Value
Time Complexity O(n)
Space Complexity O(1)
This makes it perfect for processing large data (millions of prices).
Python-Specific Tip
If you're one who likes a one-liner solution:
python
print(max(j - i for i in a for j in a[a.index(i)+1:] if j > i))
But this is not efficient (O(n²)) and must be reserved for demo/educational purposes only.
Use the greedy one for actual coding interviews or production code.
SEO-Optimized Natural Paragraph for Ranking
Want to find the optimal time to buy and sell a stock in C, C++, Java, or Python? This optimized algorithm applies the greedy method to obtain the maximum profit from a single buy-and-sell operation in linear time. This classic interview question educates you on how to maintain a minimum price while dynamically updating the maximum profit. It has real-world applications in trading systems, data analysis, and pricing strategies in e-commerce. Find out how to do this economically with a few lines of elegant, readable code in any popular programming language.
Social Plugin