For each day, find the next day with a strictly higher price
Hi! I'm Teju 👋 In stock trading, we often want to know: "When will the price go higher than today?"
For each day, find the next greater price to its right. If none exists → -1
Example:
Prices: [4, 5, 2, 25]
Next Greater: 5 (for 4), 25 (for 5), 25 (for 2), -1 (for 25)
We traverse from right to left, maintaining a stack of prices (decreasing order).
For each price, pop smaller ones — the top of stack will be the next greater!
Time: O(N) — each element pushed/popped at most once
Enter prices and click Find Next Greater to see the algorithm in action...