Dsa
Greedy
Easy
Optimal Stock Trades
You are given the prices of a stock over n days. You can buy the stock on one day and sell it on a later day, aiming to maximize your profit. Your goal is to calculate the maximum profit you can achieve from a single buy-sell transaction.
Input:
The first line contains an integer n (1 ≤ n ≤ 100) - the number of days.
The next n lines contain one integer each (0 ≤ price ≤ 10000) representing the stock price on that day.
Output:
Print the maximum profit you can achieve. If no profit can be made, print 0.
Example:
Input:
5
7
1
5
3
6
Output:
5
Key concepts
greedystock trading
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now