194. Next Smaller Element

Given an array of integers arr, your task is to find the Next Smaller Element (NSE) for every element in the array.

The Next Smaller Element for an element x is defined as the first element to the right of x that is smaller than x.

If there is no smaller element to the right, then the NSE is -1.

Example 1:

Input: arr = [4, 8, 5, 2, 25]

Output: [2, 5, 2, -1, -1]

Explanation:

- For 4, the next smaller element is 2.

- For 8, the next smaller element is 5.

- For 5, the next smaller element is 2.

- For 2, there is no smaller element to its right → -1.

- For 25, no smaller element exists → -1.

Example 2:

Input: arr = [10, 9, 8, 7]

Output: [9, 8, 7, -1]

Explanation:

  • Each element’s next right neighbor is smaller.

Now Your Turn!

Pick the correct output for the given input

Input: arr = [1, 2, 3, 4, 5]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= arr.length <= 105
  • -109 <= arr[i] <= 109

Fun Facts

0
class Solution {
public:
vector<int> nextSmallerElements(const vector<int>& arr) {
// Your code goes here
}
};
 
Test Case

Input:

Arr