Dsa
Monotonic Stack
Warm-up
Next Smaller Element
Given an array of integers, write a program to find the next smaller element for each element in the array. If there is no smaller element on the right side, return -1 for that element.
Input format:
- The first line contains an integer n (1 <= n <= 10000), the number of elements.
- The second line contains n integers (0 <= arr[i] <= 10^5), the elements of the array.
Output format:
- A single line with n integers representing the next smaller element for each element.
Example:
Input:
5
4 5 2 10 8
Output:
2 2 -1 8 -1
Key concepts
monotonic_stackarray
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