Find next higher element in an array for each element
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Find next highest unique number from the given digits
- Find nth SET bit in an int
- Find number in sorted matrix Rows n Columns in Olog n
- Find number of bits to be flipped to get maximum 1's in array
- Find non-common elements in lists
- Find number of permutations of a given sequence of integers which yield the same binary search tree
- Find number of continuous subarray having sum zero
- Find only two numbers in array that evenly divide each other

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.