Last Updated: 27 Jan, 2021

Next Smaller Element

Moderate
Asked in companies
CultfitOracleIBM

Problem statement

You are given an array 'ARR' of integers of length N. Your task is to find the next smaller element for each of the array elements.

Next Smaller Element for an array element is the first element to the right of that element which has a value strictly smaller than that element.

If for any array element the next smaller element does not exist, you should print -1 for that array element.

For Example:

If the given array is [ 2, 3, 1], we need to return [1, 1, -1]. Because for  2, 1 is the Next Smaller element. For 3, 1 is the Next Smaller element and for 1, there is no next smaller element hence the answer for this element is -1.
Input Format:
The first line of input contains an integer ‘T’ which contains the number of test cases.

The first line of each test case contains an integer 'N' denoting the number of elements in the array 'ARR'.

The second line of each test case contains 'N' space-separated integers denoting the array 'ARR'. 
Output Format:
For each test case, print a single line containing 'N' space-separated integers denoting the value of Next Smaller Element for each of the 'N' array elements.

The output for each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10 ^ 5
0 <= ARR [i] <= 10 ^ 9

Time Limit: 1sec.

Approaches

01 Approach

The idea is to find the Next Smaller Element for each array element one by one. To find the Next Smaller for an array element we will start moving to the right of that element and will try to find the first element having a value smaller than that element and set the Next Smaller Element as that element and end our search. If we reach the end of the array without finding any such element then we will set the next smaller element as -1 for that element.

Let ans be an array of length N that stores the Next Smaller Elements.

 

Steps :

  1. Set all values of array ans as -1.
  2. Iterate from i = 0 to N - 1
    • Iterate from j = i + 1 to N -1 
      • If ARR [i] is greater than ARR [j]then set the value of ans [i] to ARR [j] and break the loop.
  3. Return the array ans .

02 Approach

The idea is to find the Next Smaller Element for each of the N array elements in one iteration of the array by using a stack. Our approach is to traverse the array from right to left and for each array element we will first pop all such elements that are currently at the top of stack and having a value greater than or equal to the current element. Now we will set the Next Smaller Element as the element at the top of the stack if the stack is non-empty otherwise we will set it as -1. Now we will push that element into the stack and move ahead.

Let ans be an array of length N that stores the Next Smaller Elements.

 

Steps :

  1. Create an empty stack of type integer and push -1 to it .
  2. Traverse the array from backwards and for every element at index 'i’,
    • We will pop the elements from the stack till we get the smaller element on top of the stack from the current element i.e. ARR[i] and that element will be the answer for the current element.Store the next smaller element in the array ans.
    • Push the current element of the array into the stack.
  3. Return the array ans.

 

 

This algorithm works because whenever we push an element to the stack, we know its value is greater than every element present in the stack. As we visit an array element, we know that if it's value is less than any item in the stack, it must be smaller than the element at the top of the stack because it is the largest item of the stack. Therefore we need not to do any kind of searching on the stack and we can just consider the last item every time.