JavaScript Program to Find Next Smaller Element
Last Updated :
12 Jul, 2024
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
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(" "));
Output3 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(" "));
Output3 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));
Output3 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));
Output3 3 3 -1
-1 -1 -1 -1
Using the Two-Pointer Technique
In this approach, we will use two pointers to find the next smaller element for each element in the array. The idea is to iterate over the array and for each element, use another pointer to check the elements to its right to find the next smaller element. If no such element is found, -1 will be assigned.
Example: This example implements the two-pointer technique
JavaScript
function nextSmallerElements(inputArray) {
let inputArrayLength = inputArray.length;
let outputElements = new Array(inputArrayLength).fill(-1);
for (let i = 0; i < inputArrayLength; i++) {
for (let j = i + 1; j < inputArrayLength; j++) {
if (inputArray[j] < inputArray[i]) {
outputElements[i] = inputArray[j];
break;
}
}
}
return outputElements;
}
let inputArr1 = [11, 13, 21, 3];
let inputArr2 = [1, 2, 3, 4];
console.log(nextSmallerElements(inputArr1).join(" "));
console.log(nextSmallerElements(inputArr2).join(" "));
Output3 3 3 -1
-1 -1 -1 -1
Similar Reads
JavaScript Program for Insertion Sort What is Insertion Sort Algorithm?Insertion sorting is one of the sorting techniques that is based on iterating the array and finding the right position for every element. It compares the current element to the predecessors, if the element is small compare it with the elements before. Move ahead to a
4 min read
JavaScript Program to Find the Distance Value between Two Arrays We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <=
3 min read
Javascript Program To Find Next Greater Element Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1. Examples: For an array, the righ
6 min read
JavaScript Program to Find kth Largest/Smallest Element in an Array JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
5 min read
Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value
3 min read