Open In App

JavaScript Program to Find Next Smaller Element

Last Updated : 13 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this JavaScript article, we will see how we can find the next smaller elements from the first input Array. We have given the array in which we need to print the next smaller element. The next smaller element is an element that is present after the target element in the input array. For elements for which no next smaller elements are present then the -1 will be printed.

Examples:

Input: inputArray=[ 11, 13, 21, 3 ]
Output: 3 3 3 -1
Explanation:
For the rightmost element (3), the next smaller is always -1 since there are no elements to its right.
Element: 3 => -1
Now, let's find the ISE for each element:
Element: 11: The next smaller element to its right is 3.
Element: 13: The next smaller element to its right is 3.
Element: 21: The next smaller element to its right is 3.
Element: 3: The rightmost element, so ISE is -1.

For printing the next Smaller Element we have four different approaches in JavaScript language. We have mentioned these approaches below:

  • Using Dynamic Programming Approach
  • Using the Stack Data Structure approach
  • Using the Reverse Iteration of the Elements approach
  • Using the Array Map and Find approach

Using Dynamic Programming

In this approach, we are using the DP approach where we are iterating over the array by maintaining an array ‘dp’, where each element stores the index of the immediate smaller element to its right After each iteration, the ‘dp’ is updated with the actual values of the next smaller element and the last element is set as -1.

Example: This example implements the above mentioned approach

Javascript




function nextSmallest(inputArray, arrayLength) {
    let dp = new Array(arrayLength).fill(-1);
  
    for (let i = 0; i < arrayLength; i++) {
        for (let j = i + 1; j < arrayLength; j++) {
            if (
                inputArray[j] < inputArray[i] &&
                (dp[i] === -1 || inputArray[j] > inputArray[dp[i]])
            ) {
                dp[i] = j;
            }
        }
    }
    for (let i = 0; i < arrayLength; i++) {
        if (dp[i] !== -1) {
            dp[i] = inputArray[dp[i]];
        }
    }
    dp[arrayLength - 1] = -1;
    return dp;
}
let inputArr1 = [11, 13, 21, 3];
let inputArr2 = [1, 2, 3, 4];
console.log(
    nextSmallest(inputArr1, inputArr1.length)
        .join(" "));
console.log(
    nextSmallest(inputArr2, inputArr2.length)
        .join(" "));


Output

3 3 3 -1
-1 -1 -1 -1

Using the Stack Data Structure

In this approach, we are using the Stack DS where we are iterating through the elements of the array and also we are maintaining the stack of the elements along with the index values, when the smaller element from the array is computed, it updates the next smaller element for the elements in the stack and then pushes the current element onto the stack. All the remaining elements in the array are then assigned a -1 value.

Example: This example implements the above mentioned approach

Javascript




function nextSmall(inputArray) {
    let inputArrayLength = inputArray.length;
    let outputElements = new Array(inputArrayLength);
    let stackVar = [];
    for (let i = 0; i < inputArrayLength; i++) {
        while (
            stackVar.length > 0 &&
            stackVar[stackVar.length - 1].value > inputArray[i]
        ) {
            let topOfStack = stackVar.pop();
            outputElements[topOfStack.index] = inputArray[i];
        }
        stackVar.push({ index: i, value: inputArray[i] });
    }
    while (stackVar.length > 0) {
        let topOfStack = stackVar.pop();
        outputElements[topOfStack.index] = -1;
    }
  
    return outputElements;
}
let inputArr1 = [11, 13, 21, 3];
let inputArr2 = [1, 2, 3, 4];
console.log(nextSmall(inputArr1).join(" "));
console.log(nextSmall(inputArr2).join(" "));


Output

3 3 3 -1
-1 -1 -1 -1

Using the Reverse Iteration of the Elements

In this approach, we are iterating the input array (inputArray) in reverse order. then we are initializing the array that will sore the immediate smaller elements, also we are filling it with the -1 value. For each element, we are checking the element to its right in the reverse order till we find the immediate smaller element. This updates the immediate smaller element for the current element and continues the process.

Example: This example implements the above mentioned approach

Javascript




function nextSmall(inputArray) {
    let inputArrayLength = inputArray.length;
    let outputElements = new Array(inputArrayLength).fill(-1);
  
    for (let i = inputArrayLength - 2; i >= 0; i--) {
        let j = i + 1;
        while (j !== -1 && inputArray[i] < inputArray[j]) {
            j = outputElements[j];
        }
        outputElements[i] = j;
    }
  
    return outputElements.join(" ");
}
  
let inputArr1 = [11, 13, 21, 3];
let inputArr2 = [1, 2, 3, 4];
  
console.log(nextSmall(inputArr1));
console.log(nextSmall(inputArr2));


Output

3 3 3 -1
-1 -1 -1 -1

Using Array Map and Find method

In this approach, we are using the forEach method that is iterating over the input along with the element’s index value. find method is used to search for the smaller element by specifying the condition to the right by slicing the array from the current index + 1. When the smaller element is for, ut is pushed on the ouputElements array else, the -1 value is been pushed.

Example: This example implements the above mentioned approach

Javascript




function nextSmall(inputArray) {
  
    // let inputArrayLength = inputArray.length;
    let outputElements = [];
  
    inputArray.forEach((element, index) => {
        let smallerElement = inputArray.slice(index + 1)
            .find((e) => e < element);
        outputElements.push(smallerElement || -1);
    });
  
    return outputElements.join(" ");
}
  
let inputArr1 = [11, 13, 21, 3];
let inputArr2 = [1, 2, 3, 4];
  
console.log(nextSmall(inputArr1));
console.log(nextSmall(inputArr2));


Output

3 3 3 -1
-1 -1 -1 -1


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads