Dsa
Greedy
Hard
Stock Purchase Optimization
You have a list of stock prices for each day and an opportunity to buy and sell stocks. The challenge is to find the maximum profit you can make by buying stocks on certain days and selling them on later days. You can make only one transaction (buy once and sell once). Write a program to calculate the maximum possible profit from a single transaction based on the given stock prices. You will read an integer N representing the length of the price list followed by N space-separated integers representing the prices. Output the maximum profit possible.
Input Format:
The first line contains an integer N (1 <= N <= 10^5) - the number of days.
The second line contains N space-separated integers representing the stock prices on each day.
Output Format:
Print a single integer showing the maximum profit; if no profit is possible, print 0.
Example:
Input:
6
7 1 5 3 6 4
Output:
5
Key concepts
greedydynamic programmingoptimization
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