![]() |
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 ] For printing the next Smaller Element we have four different approaches in JavaScript language. We have mentioned these approaches below: Using Dynamic ProgrammingIn 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
Output 3 3 3 -1 -1 -1 -1 -1 Using the Stack Data StructureIn 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
Output 3 3 3 -1 -1 -1 -1 -1 Using the Reverse Iteration of the ElementsIn 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
Output 3 3 3 -1 -1 -1 -1 -1 Using Array Map and Find methodIn 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
Output 3 3 3 -1 -1 -1 -1 -1 Using the Two-Pointer TechniqueIn 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
Output 3 3 3 -1 -1 -1 -1 -1 |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |