Dsa
Monotonic Stack
Hard
Next Greater Element with Frequency
You are given an array of integers. Write a program that computes the Next Greater Element (NGE) for each element in the array. The NGE for an element is the first element to its right that is greater than it. If no such element exists, return -1 for that position. Moreover, you need to return the frequency of each NGE value in the output. The output should be formatted as a list of pairs `(NGE, Frequency)` for each element in the input.
Input Format:
- A single line containing space-separated integers representing the array.
Output Format:
- A list of pairs (next_greater, frequency) for each element in the input array.
Example:
Input:
4 5 2 10 8
Output:
[(5, 1), (10, 1), (10, 1), (-1, 0), (-1, 0)]
Key concepts
monotonic_stackarrayfrequency_count
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now