Next Smaller Element
Next Smaller Element is a GFG problem (Medium). For every element of arr, return the first element to its right that is strictly smaller. If no such element exists, return −1 for that position.
- Only a strictly smaller value counts. An equal value is not an answer.
- With up to 10⁵ elements, comparing each element with everything to its right can take about 5 × 10⁹ steps, so a linear pass is needed.
- 1 <= arr.size() <= 10⁵
- 1 <= arr[i] <= 10⁵
Intuition
Read the array from the right end. Not every element to the right of arr[i] can be an answer for something. Once a value v sits in front of a larger-or-equal value w, nobody to the left of v can ever pick w: they reach v first, and v is no larger.
So keep a stack of the values that can still be an answer, nearest on top. Scanning right to left, the current value removes every candidate that is not smaller than it, because it now blocks them. Whatever is left on top is the nearest smaller value. Then the current value joins the stack as the newest candidate.
"First smaller value to the right (or left)" for every element is the monotonic stack pattern. It appears inside harder problems as the step that finds how far each element's range reaches.
Approach
Before reading on, take an element v and a larger value w somewhere to its right. Can anything to the left of v have w as its next smaller element? Use the answer to decide which values are worth remembering. Aim for O(n).
Start from the right end
Fill res with −1 and walk i from n − 1 down to 0 with an empty stack. The stack holds values to the right of i that may still be the next smaller element of arr[i] or of something further left.
Pop candidates that are not smaller
While the top of the stack is ≥ arr[i], pop it. It cannot answer arr[i], and it cannot answer anything further left either, because arr[i] stands in front of it and is no larger.
The top is the answer
If the stack still has a value, it is smaller than arr[i] and it is the nearest value that survived, so it goes into res[i]. An empty stack means nothing to the right is smaller, and −1 stays.
Push the current value
Push arr[i]. It is now the nearest candidate for every element to its left. After the pops, the values on the stack strictly increase from bottom to top.
Code in Python, C++ and Java
Common pitfalls
Keeping equal values on the stack
while stack and stack[-1] > arr[i]:
while stack and stack[-1] >= arr[i]:
With >, an equal value survives and becomes the answer. For arr = [5, 5, 3] the first 5 would get 5, but 5 is not smaller than 5. The correct output is [3, 3, -1].
Reading the top before popping
if stack:
res[i] = stack[-1]
while stack and stack[-1] >= arr[i]:
stack.pop()while stack and stack[-1] >= arr[i]:
stack.pop()
if stack:
res[i] = stack[-1]The top may be larger than arr[i] until the pops run. For arr = [1, 3] the 1 would read 3 as its answer, but 3 is not smaller; the output should be [-1, -1].
Edge cases
Nothing is ever popped, so every value stays on the stack and it grows to n entries. Each answer is simply the next element, and the last is −1.
Complexity
Next Smaller Element FAQ
How do I return the index of the next smaller element instead of its value?
Push indices instead of values and compare arr[stack[-1]]. Store the index in res. Indices are what range problems need, because they tell you how far an element's run extends.
Where does this come up in harder problems?
In Largest Rectangle in Histogram and Sum of Subarray Minimums, each bar's range ends at its previous and next smaller elements, so both are found with this stack.
Can I scan from left to right instead?
Yes. Keep indices that have no answer yet, and when a smaller value arrives, pop each larger one and give it that value. The next greater element page runs that direction with the comparison flipped.