Basic example
Input
[4, 5, 2, 25]
Output
[2, 2, -1, -1]
For 4 the next smaller is 2; for 5 it's 2; for 2 none; for 25 none.
Full lesson preview
For each element in an array, find the next smaller element to its right using a monotonic stack.
Problem statement
Task
Examples
Input
[4, 5, 2, 25]
Output
[2, 2, -1, -1]
For 4 the next smaller is 2; for 5 it's 2; for 2 none; for 25 none.
Input format
Output format
Constraints
Samples
Input
[3, 7, 1, 7, 2]
Output
[1, 1, -1, 2, -1]
For 3 the next smaller is 1, for 7 it's 1, 1 has none, for the next 7 it's 2, for 2 none.