Next Smaller Element

10 Jun, 2022
Given an array, print the Next Smaller Element (NSE) for every element. The NSE for an element x is the first smaller element on the right side of x in array. Elements for which no smaller element exist (on right side), consider NSE as -1. Examples: a) For any array, rightmost element always has NSE as -1. b) For an array which is sorted in increasing order, all elements have NSE as -1. c) For the input array [4, 8, 5, 2, 25}, the NSE for each element are as follows.
Element         NSE
   4      -->    2
   8      -->    5
   5      -->    2
   2      -->   -1
   25     -->   -1
d) For the input array [13, 7, 6, 12}, the next smaller elements for each element are as follows.  
  Element        NSE
   13      -->    7
   7       -->    6
   6       -->   -1
   12      -->   -1
Method 1 (Simple) Use two loops: The outer loop picks all the elements one by one. The inner loop looks for the first smaller element for the element picked by outer loop. If a smaller element is found then that element is printed as next, otherwise, -1 is printed.Thanks to Sachin for providing following code.  
C++
C
Java
Python
C#
PHP
Javascript
// Simple C++ program to print// next smaller elements in a given array#include "bits/stdc++.h"using namespace std; /* prints element and NSE pairfor all elements of arr[] of size n */void printNSE(int arr[], int n){    int next, i, j;    for (i = 0; i < n; i++)    {        next = -1;        for (j = i + 1; j < n; j++)        {            if (arr[i] > arr[j])            {                next = arr[j];                break;            }        }        cout << arr[i] << " -- "             << next << endl;    }} // Driver Codeint main(){    int arr[]= {11, 13, 21, 3};    int n = sizeof(arr) / sizeof(arr[0]);    printNSE(arr, n);    return 0;} // This code is contributed by shivanisinghss2110
// Simple C program to print next smaller elements// in a given array#include<stdio.h> /* prints element and NSE pair for all elements ofarr[] of size n */void printNSE(int arr[], int n){    int next, i, j;    for (i=0; i<n; i++)    {        next = -1;        for (j = i+1; j<n; j++)        {            if (arr[i] > arr[j])            {                next = arr[j];                break;            }        }        printf("%d -- %d\n", arr[i], next);    }} int main(){    int arr[]= {11, 13, 21, 3};    int n = sizeof(arr)/sizeof(arr[0]);    printNSE(arr, n);    return 0;}
// Simple Java program to print next// smaller elements in a given array class Main {    /* prints element and NSE pair for     all elements of arr[] of size n */    static void printNSE(int arr[], int n)    {        int next, i, j;        for (i = 0; i < n; i++) {            next = -1;            for (j = i + 1; j < n; j++) {                if (arr[i] > arr[j]) {                    next = arr[j];                    break;                }            }            System.out.println(arr[i] + " -- " + next);        }    }     public static void main(String args[])    {        int arr[] = { 11, 13, 21, 3 };        int n = arr.length;        printNSE(arr, n);    }}
# Function to print element and NSE pair for all elements of listdef printNSE(arr):     for i in range(0, len(arr), 1):         next = -1        for j in range(i + 1, len(arr), 1):            if arr[i] > arr[j]:                next = arr[j]                break                     print(str(arr[i]) + " -- " + str(next)) # Driver program to test above functionarr = [11, 13, 21, 3]printNSE(arr) # This code is contributed by Sunny Karira
// Simple C# program to print next// smaller elements in a given arrayusing System; class GFG {     /* prints element and NSE pair for    all elements of arr[] of size n */    static void printNSE(int[] arr, int n)    {        int next, i, j;        for (i = 0; i < n; i++) {            next = -1;            for (j = i + 1; j < n; j++) {                if (arr[i] > arr[j]) {                    next = arr[j];                    break;                }            }            Console.WriteLine(arr[i] + " -- " + next);        }    }     // driver code    public static void Main()    {        int[] arr = { 11, 13, 21, 3 };        int n = arr.Length;         printNSE(arr, n);    }} // This code is contributed by Sam007
<?php// Simple PHP program to print next// smaller elements in a given array /* prints element and NSE pair for   all elements of arr[] of size n */function printNSE($arr, $n){    for ($i = 0; $i < $n; $i++)    {        $next = -1;        for ($j = $i + 1; $j < $n; $j++)        {            if ($arr[$i] > $arr[$j])            {                $next = $arr[$j];                break;            }        }        echo $arr[$i]." -- ". $next."\n";             }}     // Driver Code    $arr= array(11, 13, 21, 3);    $n = count($arr);    printNSE($arr, $n);     // This code is contributed by Sam007?>
<script> // Simple Javascript program to print// next smaller elements in a given array /* prints element and NSE pairfor all elements of arr[] of size n */function printNSE(arr, n){    var next, i, j;    for (i = 0; i < n; i++)    {        next = -1;        for (j = i + 1; j < n; j++)        {            if (arr[i] > arr[j])            {                next = arr[j];                break;            }        }        document.write( arr[i] + " -- "             + next+"<br>" );    }} // Driver Codevar arr= [11, 13, 21, 3];  var n = arr.length;printNSE(arr, n); </script>
11 -- 3
13 -- 3
21 -- 3
3 -- -1
Time Complexity: 
The worst case occurs when all elements are sorted in decreasing order.
Auxiliary Space: O(1) 
As constant extra space is used
Method 2 (Using Segment Tree and Binary Search) 
This method is also pretty simple if one knows Segment trees and Binary Search. Lets consider an array  and lets suppose NSE for  is , we simply need to binary search for  in range  to .  will be the first index , such that range minimum of elements from index  to  () is lesser than .
C++
#include <bits/stdc++.h>using namespace std; // Program to find next smaller element for all elements in// an array, using segment tree and binary search // --------Segment Tree Starts Here----------------- vector<int> seg_tree; // combine function for combining two nodes of the tree, in// this case we need to take min of twoint combine(int a, int b) { return min(a, b); } // build function, builds seg_tree based on vector parameter// arrvoid build(vector<int>& arr, int node, int tl, int tr){    // if current range consists only of one element, then    // node should be this element    if (tl == tr) {        seg_tree[node] = arr[tl];    }    else {        // divide the build operations into two parts        int tm = (tr - tl) / 2 + tl;         build(arr, 2 * node, tl, tm);        build(arr, 2 * node + 1, tm + 1, tr);         // combine the results from two parts, and store it        // into current node        seg_tree[node] = combine(seg_tree[2 * node],                                 seg_tree[2 * node + 1]);    }} // query function, returns minimum in the range [l, r]int query(int node, int tl, int tr, int l, int r){    // if range is invalid, then return infinity    if (l > r) {        return INT32_MAX;    }     // if range completely aligns with a segment tree node,    // then value of this node should be returned    if (l == tl && r == tr) {        return seg_tree[node];    }     // else divide the query into two parts    int tm = (tr - tl) / 2 + tl;     int q1 = query(2 * node, tl, tm, l, min(r, tm));    int q2 = query(2 * node + 1, tm + 1, tr, max(l, tm + 1),                   r);     // and combine the results from the two parts and return    // it    return combine(q1, q2);} // --------Segment Tree Ends Here----------------- void printNSE(vector<int> arr, int n){    seg_tree = vector<int>(4 * n);     // build segment tree initially    build(arr, 1, 0, n - 1);     int q, l, r, mid, ans;    for (int i = 0; i < n; i++) {        // binary search for ans in range [i + 1, n - 1],        // initially ans is -1 representing there is no NSE        // for this element        l = i + 1;        r = n - 1;        ans = -1;         while (l <= r) {            mid = (r - l) / 2 + l;            // q is the minimum element in range [l, mid]            q = query(1, 0, n - 1, l, mid);             // if the minimum element in range [l, mid] is            // less than arr[i], then mid can be answer, we            // mark it, and look for a better answer in left            // half. Else if q is greater than arr[i], mid            // can't be an answer, we should search in right            // half             if (q < arr[i]) {                ans = arr[mid];                r = mid - 1;            }            else {                l = mid + 1;            }        }         // print NSE for arr[i]        cout << arr[i] << " ---> " << ans << "\n";    }} // Driver program to test above functionsint main(){    vector<int> arr = { 11, 13, 21, 3 };    printNSE(arr, 4);    return 0;}
11 ---> 3
13 ---> 3
21 ---> 3
3 ---> -1
Time Complexity : 
 For each of  array elements we do a binary search, which includes  steps, and each step costs operations [range minimum queries].
Auxiliary Space: O(N)
As extra space is used for storing the elements of the segment tree.
Method 3 (Using Segment Tree and Coordinate Compression)
In this approach, we build a segment tree on indices of compressed array elements:
Somewhere along the lines, we would build a array such-that  is the smallest index at which  is present in input array.Its easy to see that we need to compress the input array so as to build this array  because if  exceeds (memory limit of online judge) chances are we would get a segmentation fault.To compress we sort the input array, and then for each new value seen in array we map it to a corresponding smaller value, if possible. Use these mapped values to generate a  array with same order as input array.So now that we are done with compression, we can begin with the query part:Suppose in previous step, we compressed the array to  distinct values. Initially set , this signifies no value is processed at any index as of now.Traverse the compressed array in reverse order, this would imply that in past we would have only processed elements that are on the right side.For , query (and store in ) the smallest index of values  using segment tree, this must be the NSE for !Update the index of  to .We stored the index of NSEs for all array elements, we can easily print NSEs themselves as shown in code.
Somewhere along the lines, we would build a array such-that  is the smallest index at which  is present in input array.
Its easy to see that we need to compress the input array so as to build this array  because if  exceeds (memory limit of online judge) chances are we would get a segmentation fault.
To compress we sort the input array, and then for each new value seen in array we map it to a corresponding smaller value, if possible. Use these mapped values to generate a  array with same order as input array.
So now that we are done with compression, we can begin with the query part:Suppose in previous step, we compressed the array to  distinct values. Initially set , this signifies no value is processed at any index as of now.Traverse the compressed array in reverse order, this would imply that in past we would have only processed elements that are on the right side.For , query (and store in ) the smallest index of values  using segment tree, this must be the NSE for !Update the index of  to .
Suppose in previous step, we compressed the array to  distinct values. Initially set , this signifies no value is processed at any index as of now.
Traverse the compressed array in reverse order, this would imply that in past we would have only processed elements that are on the right side.For , query (and store in ) the smallest index of values  using segment tree, this must be the NSE for !Update the index of  to .
For , query (and store in ) the smallest index of values  using segment tree, this must be the NSE for !
Update the index of  to .
We stored the index of NSEs for all array elements, we can easily print NSEs themselves as shown in code.
Note: In implementation we use INT32_MAX instead of -1 because storing INT32_MAX doesn’t affect our min-segment tree and still serves the purpose of identifying unprocessed values.
As extra space is used for storing the elements of the segment tree.
C++
#include <bits/stdc++.h>using namespace std; // Program to find next smaller element for all elements in// an array, using segment tree and coordinate compression // --------Segment Tree Starts Here----------------- vector<int> seg_tree; // combine function for combining two nodes of the tree, in// this case we need to take min of twoint combine(int a, int b) { return min(a, b); } // build function, builds seg_tree based on vector parameter// arrvoid build(vector<int>& arr, int node, int tl, int tr){    // if current range consists only of one element, then    // node should be this element    if (tl == tr) {        seg_tree[node] = arr[tl];    }    else {        // divide the build operations into two parts        int tm = (tr - tl) / 2 + tl;         build(arr, 2 * node, tl, tm);        build(arr, 2 * node + 1, tm + 1, tr);         // combine the results from two parts, and store it        // into current node        seg_tree[node] = combine(seg_tree[2 * node],                                 seg_tree[2 * node + 1]);    }} // update function, used to make a point update, update// arr[pos] to new_val and make required changes to segtreevoid update(int node, int tl, int tr, int pos, int new_val){    // if current range only contains one point, this must    // be arr[pos], update the corresponding node to new_val    if (tl == tr) {        seg_tree[node] = new_val;    }    else {        // else divide the range into two parts        int tm = (tr - tl) / 2 + tl;         // if pos lies in first half, update this half, else        // update second half        if (pos <= tm) {            update(2 * node, tl, tm, pos, new_val);        }        else {            update(2 * node + 1, tm + 1, tr, pos, new_val);        }         // combine results from both halfs        seg_tree[node] = combine(seg_tree[2 * node],                                 seg_tree[2 * node + 1]);    }} // query function, returns minimum in the range [l, r]int query(int node, int tl, int tr, int l, int r){    // if range is invalid, then return infinity    if (l > r) {        return INT32_MAX;    }     // if range completely aligns with a segment tree node,    // then value of this node should be returned    if (l == tl && r == tr) {        return seg_tree[node];    }     // else divide the query into two parts    int tm = (tr - tl) / 2 + tl;     int q1 = query(2 * node, tl, tm, l, min(r, tm));    int q2 = query(2 * node + 1, tm + 1, tr, max(l, tm + 1),                   r);     // and combine the results from the two parts and return    // it    return combine(q1, q2);} // --------Segment Tree Ends Here----------------- void printNSE(vector<int> original, int n){    vector<int> sorted(n);    map<int, int> encode;     // -------Coordinate Compression Starts Here ------     // created a temporary sorted array out of original    for (int i = 0; i < n; i++) {        sorted[i] = original[i];    }    sort(sorted.begin(), sorted.end());     // encode each value to a new value in sorted array    int ctr = 0;    for (int i = 0; i < n; i++) {        if (encode.count(sorted[i]) == 0) {            encode[sorted[i]] = ctr++;        }    }     // use encode to compress original array    vector<int> compressed(n);    for (int i = 0; i < n; i++) {        compressed[i] = encode[original[i]];    }     // -------Coordinate Compression Ends Here ------     // Create an aux array of size ctr, and build a segtree    // based on this array     vector<int> aux(ctr, INT32_MAX);    seg_tree = vector<int>(4 * ctr);     build(aux, 1, 0, ctr - 1);     // For each compressed[i], query for index of NSE and    // update segment tree     vector<int> ans(n);    for (int i = n - 1; i >= 0; i--) {        ans[i] = query(1, 0, ctr - 1, 0, compressed[i] - 1);        update(1, 0, ctr - 1, compressed[i], i);    }     // Print -1 if NSE doesn't exist, otherwise print NSE    // itself     for (int i = 0; i < n; i++) {        cout << original[i] << " ---> ";        if (ans[i] == INT32_MAX) {            cout << -1;        }        else {            cout << original[ans[i]];        }        cout << "\n";    }} // Driver program to test above functionsint main(){    vector<int> arr = { 11, 13, 21, 3 };    printNSE(arr, 4);    return 0;}
11 ---> 3
13 ---> 3
21 ---> 3
3 ---> -1
Time Complexity:  
Auxiliary Space: O(N)
Method 4 (Using Stack) This problem is similar to next greater element. Here we maintain items in increasing order in the stack (instead of decreasing in next greater element problem).
Push the first element to stack.Pick rest of the elements one by one and follow following steps in loop.Mark the current element as next.If stack is not empty, then compare next with stack top. If next is smaller than top then next is the NSE for the top. Keep popping from the stack while top is greater than next. next becomes the NSE for all such popped elementsPush next into the stackAfter the loop in step 2 is over, pop all the elements from stack and print -1 as next element for them.
Push the first element to stack.
Pick rest of the elements one by one and follow following steps in loop.Mark the current element as next.If stack is not empty, then compare next with stack top. If next is smaller than top then next is the NSE for the top. Keep popping from the stack while top is greater than next. next becomes the NSE for all such popped elementsPush next into the stack
Mark the current element as next.
If stack is not empty, then compare next with stack top. If next is smaller than top then next is the NSE for the top. Keep popping from the stack while top is greater than next. next becomes the NSE for all such popped elements
Push next into the stack
After the loop in step 2 is over, pop all the elements from stack and print -1 as next element for them.
Note: To achieve same order, we use a stack of pairs, where first element is the value and second element is index of array element. 
C++
Java
Python3
C#
Javascript
// A Stack based C++ program to find next// smaller element for all array elements#include <bits/stdc++.h>using namespace std; // prints NSE for elements of array arr[] of size n void printNSE(int arr[], int n){    stack<pair<int, int> > s;    vector<int> ans(n);     // iterate for rest of the elements    for (int i = 0; i < n; i++) {        int next = arr[i];         // if stack is empty then this element can't be NSE        // for any other element, so just push it to stack        // so that we can find NSE for it, and continue        if (s.empty()) {            s.push({ next, i });            continue;        }         // while stack is not empty and the top element is        // greater than next        //  a) NSE for top is next, use top's index to        //    maintain original order        //  b) pop the top element from stack         while (!s.empty() && s.top().first > next) {            ans[s.top().second] = next;            s.pop();        }         // push next to stack so that we can find NSE for it         s.push({ next, i });    }     // After iterating over the loop, the remaining elements    // in stack do not have any NSE, so set -1 for them     while (!s.empty()) {        ans[s.top().second] = -1;        s.pop();    }     for (int i = 0; i < n; i++) {        cout << arr[i] << " ---> " << ans[i] << endl;    }} // Driver program to test above functionsint main(){    int arr[] = { 11, 13, 21, 3 };    int n = sizeof(arr) / sizeof(arr[0]);    printNSE(arr, n);    return 0;}
// A Stack based Java program to find next// smaller element for all array elements// in same order as input.import java.io.*;import java.lang.*;import java.util.*; class GFG {    /* prints element and NSE pair for all    elements of arr[] of size n */    public static void printNSE(int arr[], int n)    {        Stack<Integer> s = new Stack<Integer>();        HashMap<Integer, Integer> mp            = new HashMap<Integer, Integer>();         /* push the first element to stack */        s.push(arr[0]);         // iterate for rest of the elements        for (int i = 1; i < n; i++) {             if (s.empty()) {                s.push(arr[i]);                continue;            }             /* if stack is not empty, then    pop an element from stack.    If the popped element is greater    than next, then    a) print the pair    b) keep popping while elements are    greater and stack is not empty */             while (s.empty() == false                   && s.peek() > arr[i]) {                mp.put(s.peek(), arr[i]);                s.pop();            }             /* push next to stack so that we can find            next smaller for it */            s.push(arr[i]);        }         /* After iterating over the loop, the remaining        elements in stack do not have the next smaller        element, so print -1 for them */        while (s.empty() == false) {            mp.put(s.peek(), -1);            s.pop();        }         for (int i = 0; i < n; i++)            System.out.println(arr[i] + " ---> "                               + mp.get(arr[i]));    }     /* Driver program to test above functions */    public static void main(String[] args)    {        int arr[] = { 11, 13, 21, 3 };        int n = arr.length;        printNSE(arr, n);    }}
# A Stack based Python3 program to find next# smaller element for all array elements# in same order as input.using System; """ prints element and NSE pair for allelements of arr[] of size n """  def printNSE(arr, n):    s = []    mp = {}     # push the first element to stack    s.append(arr[0])     # iterate for rest of the elements    for i in range(1, n):        if (len(s) == 0):            s.append(arr[i])            continue         """ if stack is not empty, then        pop an element from stack.        If the popped element is greater        than next, then        a) print the pair        b) keep popping while elements are        greater and stack is not empty """        while (len(s) != 0 and s[-1] > arr[i]):            mp[s[-1]] = arr[i]            s.pop()         """ push next to stack so that we can find        next smaller for it """        s.append(arr[i])     """ After iterating over the loop, the remaining    elements in stack do not have the next smaller    element, so print -1 for them """    while (len(s) != 0):        mp[s[-1]] = -1        s.pop()     for i in range(n):        print(arr[i], "--->", mp[arr[i]])  arr = [11, 13, 21, 3]n = len(arr)printNSE(arr, n) # This code is contributed by decode2207.
// A Stack based C# program to find next// smaller element for all array elements// in same order as input.using System;using System;using System.Collections.Generic; class GFG {    /* prints element and NSE pair for all    elements of arr[] of size n */    public static void printNSE(int[] arr, int n)    {        Stack<int> s = new Stack<int>();        Dictionary<int, int> mp            = new Dictionary<int, int>();         /* push the first element to stack */        s.Push(arr[0]);         // iterate for rest of the elements        for (int i = 1; i < n; i++) {            if (s.Count == 0) {                s.Push(arr[i]);                continue;            }            /* if stack is not empty, then            pop an element from stack.            If the popped element is greater            than next, then            a) print the pair            b) keep popping while elements are            greater and stack is not empty */            while (s.Count != 0 && s.Peek() > arr[i]) {                mp.Add(s.Peek(), arr[i]);                s.Pop();            }             /* push next to stack so that we can find            next smaller for it */            s.Push(arr[i]);        }         /* After iterating over the loop, the remaining        elements in stack do not have the next smaller        element, so print -1 for them */        while (s.Count != 0) {            mp.Add(s.Peek(), -1);            s.Pop();        }         for (int i = 0; i < n; i++)            Console.WriteLine(arr[i] + " ---> "                              + mp[arr[i]]);    }     // Driver code    public static void Main()    {        int[] arr = { 11, 13, 21, 3 };        int n = arr.Length;        printNSE(arr, n);    }}// This code is contributed by// 29AjayKumar
<script>    // A Stack based Javascript program to find next    // smaller element for all array elements    // in same order as input.         /* prints element and NSE pair for all    elements of arr[] of size n */    function printNSE(arr, n)    {        let s = [];        let mp = new Map();         /* push the first element to stack */        s.push(arr[0]);          // iterate for rest of the elements        for (let i = 1; i < n; i++)        {            if (s.length==0)            {                s.push(arr[i]);                continue;            }            /* if stack is not empty, then            pop an element from stack.            If the popped element is greater            than next, then            a) print the pair            b) keep popping while elements are            greater and stack is not empty */            while (s.length != 0 && s[s.length - 1] > arr[i])            {                mp[s[s.length - 1]] = arr[i];                s.pop();            }             /* push next to stack so that we can find            next smaller for it */            s.push(arr[i]);        }           /* After iterating over the loop, the remaining        elements in stack do not have the next smaller        element, so print -1 for them */        while (s.length != 0)        {            mp[s[s.length - 1]] = -1;            s.pop();        }          for (let i = 0; i < n; i++)            document.write(arr[i] + " ---> " + mp[arr[i]] + "</br>");    }         let arr = [11, 13, 21, 3];    let n = arr.length;    printNSE(arr, n);         // This code is contributed by divyesh072019.</script>
11 ---> 3
13 ---> 3
21 ---> 3
3 ---> -1
Time Complexity: 
As we use only single for loop and all the elements in the stack are push and poped atmost once.
Auxiliary Space: O(N)
As extra space is used for storing the elements of the stack. 
nabaneet247
princiraj1992
29AjayKumar
shivanisinghss2110
birlaritik113
rvriteshwar167
itsok
patel2127
decode2207
divyesh072019
sarvjot
abhijeet19403
cpp-stack
Arrays
Searching
Stack
Arrays
Searching
Stack
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in Java
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Introduction to Arrays
K'th Smallest/Largest Element in Unsorted Array | Set 1
Subset Sum Problem | DP-25
Binary Search
K'th Smallest/Largest Element in Unsorted Array | Set 1
Search an element in a sorted and rotated array
Find the Missing Number
Search, insert and delete in an unsorted array