Find next higher element in an array for each element
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Here's a detailed article on finding the next higher element in an array for each element:
Introduction
In the problem of finding the next higher or greater element for each element of an array, the goal is to identify the first element that is greater than the current element for each position within the array. This is a common problem encountered in various computational scenarios, such as stock price analysis, data processing pipelines, and algorithm optimizations. The efficiency of solving this problem can have a significant impact on performance, particularly for large datasets.
Problem Statement
Given an array of integers, arr[], for each element arr[i], find the smallest index j such that:
j > iarr[j] > arr[i]
If no such j exists, the next higher element for arr[i] should be marked as -1.
Approaches
Naive Approach
The straightforward way to solve this problem is to use a nested loop structure:
- For each element in the array, iterate through the subsequent elements.
- Compare and find the first element that is greater than the current element.
This approach has a time complexity of , where is the number of elements in the array. This is due to the nested iteration over all pairs, making it inefficient for large arrays.
Efficient Approach Using a Stack
A more efficient approach leverages a stack data structure to maintain a list of elements whose next higher element hasn't been found yet. The time complexity of this approach is , as each element is pushed and popped from the stack only once.
Algorithm:
- Initialize an empty stack and a result list filled with
-1. - Traverse the array from right to left (i.e., reverse order):
- While the stack is not empty and the top of the stack is less than or equal to the current element, pop the stack.
- If the stack is not empty after the pop operations, the top of the stack is the next higher element for the current element.
- Push the current element onto the stack.
- The result list will contain the next higher elements for each position in the original array.
Example
Let's consider an example array to illustrate both approaches:
For the naive approach, the result would be:
- For
4, the next higher is5. - For
5, the next higher is25. - For
2, the next higher is25. - For
25, there is no higher element, so-1. - For
7, the next higher is8. - For
8, there is no higher element, so-1.
Result: [5, 25, 25, -1, 8, -1]
Using the efficient stack-based approach, we'll get the same result faster.
Summary Table
| Element | Next Higher Element |
| 4 | 5 |
| 5 | 25 |
| 2 | 25 |
| 25 | -1 |
| 7 | 8 |
| 8 | -1 |
Additional Details
Stack-Based Solution Advantages
- Time Complexity: The time complexity is , which makes it suitable for large datasets.
- Space Complexity: Since we use a stack to store elements, the space complexity is in the worst case, which is manageable.
Applications
- Stock Span Problem: Can be adapted to determine the span of stock’s price for all days.
- Temperature Monitoring: Find the next hottest day for a sequence of daily temperatures.
- Data Streaming: Efficient online computation for finding peaks in streaming data.
In conclusion, the stack-based solution offers an efficient way to solve the problem, ensuring that large datasets can be processed quickly while maintaining clarity in implementation.

