Dsa
Dynamic Programming
Easy
Maximal Stock Profit
You are given an array `prices` where `prices[i]` is the price of a stock on day `i`. You want to calculate the maximum profit you can achieve by buying on one day and selling on another day after that. Implement a function that reads the price list from standard input and prints the maximum profit that can be made. If no profit is possible, return 0.
Input format:
- The first line contains an integer `n` (1 ≤ n ≤ 100).
- The second line contains `n` space-separated integers representing the `prices` array (1 ≤ prices[i] ≤ 1000).
Output format:
- Print a single integer, the maximum profit that can be achieved.
Example:
Input:
5
7 1 5 3 6 4
Output:
5
(You buy at 1 and sell at 6, profit = 6 - 1 = 5).
Key concepts
dynamic_programminggreedarray
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