For the given array 'ARR' = [7, 12, 1, 20]
The next greater element for 7 is 12.
The next greater element for 12 is 20.
The next greater element for 1 is 20.
There is no greater element for 20 on the right side.
So, the output is [12, 20, 20, -1].
The first line contains an Integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains an integer 'N' representing the size of the array/list 'ARR'.
The second line of each test case contains 'N' single space-separated integers representing the elements of the given array/list ‘ARR’.
For each test case, print 'N' single space-separated integers representing the Next Greater Element for each element.
You are not required to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= N <= 5 * 10^3
1 <= ARR[i] <= 10^9
Time Limit: 1 sec
We will iterate through all the elements to their right and check if the element is greater than the current element. If we found the greater element, we will stop iteration for this element and move to the next element.
The steps are as follows:
We will use a Stack to keep track of the next greater element and pop as soon as we find an element greater than it.
The steps are as follows: