Dsa
Monotonic Stack
Easy
Next Greater Element for Stock Prices
You are given the daily closing prices of a stock for a series of days. For each day's price, find the next day with a higher price. If there is no such day, return -1. Your program should read the number of days and then the stock prices, and for each day, print the next greater price or '-1'.
Input format:
The first line contains a single integer, n (1 <= n <= 100000), the number of days.
The second line contains n integers representing the stock prices for each day, where each price is between 1 and 100000.
Output format:
Print n integers as space-separated values, where each integer is the next greater stock price or -1 for that day.
Example:
Input:
5
100 80 60 90 110
Output:
-1 90 90 110 -1
Key concepts
monotonic_stackarraystock_price
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