18. Next Element with Greater Frequency
β GFG solution to find the next element with greater frequency using stack-based approach with frequency mapping for optimal performance. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given an array arr[] of integers, for each element, find the first element to its right that has a higher frequency than the current element. If no such element exists, return -1 for that position.
π Examples
Example 1
Input: arr[] = [2, 1, 1, 3, 2, 1]
Output: [1, -1, -1, 2, 1, -1]
Explanation: Frequencies: 1 β 3 times, 2 β 2 times, 3 β 1 time.
For arr[0] = 2, the next element 1 has a higher frequency β 1.
For arr[1] and arr[2], no element to the right has a higher frequency β -1.
For arr[3] = 3, the next element 2 has a higher frequency β 2.
For arr[4] = 2, the next element 1 has a higher frequency β 1.
For arr[5] = 1, no elements to the right β -1.Example 2
Input: arr[] = [5, 1, 5, 6, 6]
Output: [-1, 5, -1, -1, -1]
Explanation: Frequencies: 1 β 1 time, 5 β 2 times, 6 β 2 times.
For arr[0] and arr[2], no element to the right has a higher frequency β -1.
For arr[1] = 1, the next element 5 has a higher frequency β 5.
For arr[3] and arr[4], no element to the right has a higher frequency β -1.π Constraints
$1 \le \text{arr.size()} \le 10^5$
$1 \le \text{arr}[i] \le 10^5$
β
My Approach
The optimal approach combines Frequency Mapping with a Monotonic Stack technique to efficiently find the next greater frequency element:
Frequency Map + Monotonic Stack (Backward Traversal)
Build Frequency Map:
Traverse the entire array once to count frequency of each element.
Store frequencies in an unordered map for O(1) lookup.
Initialize Result and Stack:
Create result vector initialized with -1 for all positions.
Use stack to store indices in decreasing order of their frequencies.
Backward Traversal:
Traverse array from right to left (n-1 to 0).
For current index i, compare frequency of arr[i] with stack top frequencies.
Stack Maintenance:
Pop indices from stack while their frequency is less than or equal to current element's frequency.
If stack is not empty after popping, the top index gives the answer.
Push current index onto stack for future comparisons.
Result Construction:
When a valid next greater frequency element is found, store its value in result array.
Otherwise, -1 remains as the default value.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. The frequency map is built in O(n) time. Each element is pushed and popped from the stack at most once during the backward traversal, resulting in O(n) operations overall.
Expected Auxiliary Space Complexity: O(n), as we use a hash map to store frequencies of up to n distinct elements, a stack that can hold up to n indices in the worst case, and a result vector of size n.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated