
For the given LEVELS = [3,5,4,6],the answer array will be [4,6,6,-1].
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, 'N’ denoting the number of Ninjas.
The second line contains ‘N’ numbers denoting the level of each ninja.
For each test case, print a line of ‘N’ elements corresponding to the elements of the answer array.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 3000.
1 <=LEVELS[i] <= 10^6
Time limit: 1 sec
In this approach, we will first declare the answer array ‘ANS’. For each element in the ‘LEVELS’ array, we will iterate over all the elements to their right and check which element satisfies the given conditions and will update ‘ANS’ array accordingly.
In this approach, we will traverse the ‘LEVELS’ array right to left and store them in an ordered set ‘LEVELSET’. Set is an inbuilt data structure implemented using Self-balancing BST that stores the elements in sorted order. Now we can use inbuilt binary search functions to find the minimum value which is greater than LEVELS[i] in this set ‘LEVELSET’. If now suitable element is found, we will set ‘ANS’[i] as -1.
At last, we will return ‘ANS’ array.