


Can you solve it in O(N+M) time?
If ‘N’ = 6, ‘A’ = {1, 2, 0, 3, 4, 5}, ‘M’ = 7 and ‘B’ = {3, 5, 0, 2, 1, 6, 4}.
Then, we will return {6, 6, 2, 5, -1, 6} because:
For i = 0, A[i] = 1 and the first element greater than 1 that lies to the right of it in array ‘B’ is 6.
For i = 1, A[i] = 2 and the first element greater than 2 that lies to the right of it in array ‘B’ is 6.
For i = 2, A[i] = 0 and the first element greater than 0 that lies to the right of it in array ‘B’ is 2.
For i = 3, A[i] = 3 and the first element greater than 3 that lies to the right of it in array ‘B’ is 5.
For i = 4, A[i] = 4 and there is no greater element to the right of 4 in array ‘B’.
For i = 5, A[i] = 5 and the first element greater than 5 that lies to the right of it in array ‘B’ is 6.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’, denoting the size of the array ‘A’.
The second line of each test case contains N space-separated integers ‘A’, denoting the array elements.
The third line of each test case contains a single integer ‘M’, denoting the size of the array ‘B’.
The fourth line of each test case contains M space-separated integers ‘B’, denoting the array elements.
For each test case, for each element of array ‘A’, print its corresponding next greater element lying in array ‘B’.
Output for each test case will be printed on a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ N ≤ M ≤ 500
0 ≤ A[i] < N
0 ≤ B[i] < M
Time limit: 1 sec
A simple brute force method to solve this question is to iterate all the elements of array ‘A’, and for each element, we will find the index at which that element occurs in array ‘B’ (the answer will always exist as N ≤ M). Once the index is found we can check all the indices to the right and stop searching when we find a greater element.
The steps are as follows :
For each element in array ‘B’ if the element is less than N, then we can find its next greater element (NGE) and store it in an array or a hash-map.
To find the NGE’s, iterate the array ‘B’ and maintain a stack to store the indices. We push the current index into the stack if the stack is empty or the current element is less than or equal to the element whose index is on the top of the stack.
If the current element is greater than the element whose index is on the top then the NGE(next greater element) for the index on the top of the stack is the current element and we pop out that index from the stack, we keep repeating this till the stack remains non-empty and till the current element is greater, after that push the current index into the stack.
In the end, the indices that will remain in the stack will have their NGE’s as -1. This can be tackled by initializing all the elements as -1.
We now have all the information about NGE’s of each element from 0 to N - 1, we can simply traverse the array ‘A’, and look into the NGE’s calculated for each element of array ‘B’ to find the corresponding answer that is required.
The steps are as follows :