Ninja has called a meeting of his team members for discussing how they can keep an eye on their enemies from their society. So they made a plan that they will choose such buildings in their locality from which they are capable of seeing the right side as they know the only entry point is on the right side.
As there are various types of buildings in the society of different heights, therefore, they made a plan to choose such buildings which have lower buildings in the right direction from them.
So your task is to help Ninja in finding the buildings which have lower height buildings on the right side.
Formally, return the index of those buildings which have lower height buildings on the right side. You will be provided an arrays/list ‘HEIGHT_ARR’, where ‘HEIGHT_ARR[i]’ represents the height of the ‘ith’ building.
Example :
Suppose given ‘HEIGHT_ARR’ is { 4, 2, 3, 1 } so we return { 0, 2, 3 } as ‘0th’ index building is ‘4’ which is greater than all other buildings on the right side. ‘1st’ index has ‘2’ which is smaller than ‘3’ so we don’t include it (hidden by building height with ‘3’). We include ‘3’ as it is greater than ‘1’ then we consider ‘1’ as nothing is on the right side of ‘1’.
Input Format :
The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains an integer ‘N’ i.e size of the array ‘HEIGHT_ARR’.
The second line of each test case contains an array ‘HEIGHT_ARR’, where ‘HEIGHT_ARR[i]’ denotes the height of the ‘ith’ building.
Output Format :
For each test case, return an array containing the index of those building which have a smaller building in the right side of them.
Note :
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 100
1 <= N <= 1000
1 <= HEIGHT_ARR[i] <= 10^5
Where ‘T’ represents the number of test cases and ‘N’ represents the size of the array and ‘HEIGHT_ARR[i]’ represents the elements of the array.
Time Limit: 1 second
2
7
4 2 3 1 3 1 2
5
5 4 3 2 1
0 4 6
0 1 2 3 4
Test Case 1:
For the first test case given ‘HEIGHT_ARR’ is { 4, 2, 3, 1, 3, 1, 2 } so we return { 0, 4, 6 } as ‘0th’ index building is ‘4’ which is greater than all other buildings on the right side. ‘1st’ index has ‘2’ which is smaller than ‘3’ so we don’t include it. We have ‘3’ at the ‘2nd’ index and which is equal to ‘3’ at the ‘4th’ index so we include ‘3’ of the '4th' index and then ‘2’ at the last index.
Test Case 2:
For this test case given ‘HEIGHT_ARR’ is { 5, 4, 3, 2, 1 } so we return { 0, 1, 2, 3, 4 } as all the elements have smaller elements on the right side of the array.
2
3
5 10 20
5
6 7 3 2 1
2
1 2 3 4
Can you iterate the array?
The idea here is to traverse the array starting from the ‘0th’ index and look for the given condition.
O( N ^ 2 ), where ‘N’ is the size of the array.
As for each element we are iterating the array.
O(N), where ‘N’ is the size of the array.
As we are using an ‘ANS’ array for storing index.