Finding distance to next greater element in JavaScript

Problem

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.

Our function should construct a new array for the input in which each corresponding element is the distance to the next greater element than the current element, and if there is no greater element to the right of the current element, we should push 0 for that corresponding element in the res array and finally we should return this array.

Example Input and Output

Input

const arr = [12, 13, 14, 11, 16, 10, 12, 17, 19, 18];

Expected Output

[1, 1, 2, 1, 3, 1, 1, 1, 0, 0]

Output Explanation

The distance calculation works as follows:

  • Next greater element to 12 is 13, which is 1 position away
  • Next greater element to 13 is 14, which is 1 position away
  • Next greater element to 14 is 16, which is 2 positions away
  • Next greater element to 11 is 16, which is 1 position away
  • And so on...
  • For 19 and 18, there are no greater elements to the right, so distance is 0

Algorithm Explanation

This problem is efficiently solved using a stack-based approach:

  1. Use a stack to keep track of indices of elements for which we haven't found the next greater element yet
  2. For each element, pop indices from the stack while the current element is greater than the element at the popped index
  3. Calculate the distance as the difference between current index and popped index
  4. Push the current index onto the stack

Implementation

const arr = [12, 13, 14, 11, 16, 10, 12, 17, 19, 18];

const findNextGreater = (arr = []) => {
    const stack = [];
    const res = new Array(arr.length).fill(0);
    
    for (let i = 0; i  arr[stack[stack.length - 1]] && stack.length > 0) {
            const index = stack.pop();
            res[index] = i - index;
        }
        stack.push(i);
    }
    
    return res;
};

console.log(findNextGreater(arr));
[1, 1, 2, 1, 3, 1, 1, 1, 0, 0]

Step-by-Step Trace

Let's trace through the first few elements:

  • i=0 (12): Stack is empty, push index 0. Stack: [0]
  • i=1 (13): 13 > 12, so pop index 0, set res[0] = 1-0 = 1. Push index 1. Stack: [1]
  • i=2 (14): 14 > 13, so pop index 1, set res[1] = 2-1 = 1. Push index 2. Stack: [2]
  • i=3 (11): 11
  • i=4 (16): 16 > 11 and 16 > 14, so pop indices 3 and 2, set res[3] = 1, res[2] = 2

Time and Space Complexity

Aspect Complexity Explanation
Time O(n) Each element is pushed and popped at most once
Space O(n) Stack can contain at most n elements in worst case

Conclusion

The stack-based approach efficiently finds the distance to the next greater element in linear time. This technique is commonly used in problems involving finding the next greater/smaller element and demonstrates the power of using appropriate data structures for optimization.

Updated on: 2026-03-15T23:19:00+05:30

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements