08. Next Element with Greater Frequency
β GFG solution to the Next Element with Greater Frequency problem: find the closest element to the right with higher frequency using monotonic stack technique. π
π§© Problem Description
π 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
π Constraints
β
My Approach
Forward Stack + Map
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
π§βπ» Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated