Input: ‘N’ = 4, ‘HILLS’ = [3, 2, 1, 3]
Output: 5
If we consider the indices from 0 to 3 then the pairs are (0, 1), (0,3), (1, 2), (1,3), and (2, 3).
The soldier at hill 0 can not see hill 2 as hill[1]>hill[2].
The first line will contain the integer 'T', denoting the number of test cases.
The next line contains a single integer ‘N’ representing the number of hills.
The next line contains ‘N’ integers denoting the height of hills.
For each test case print the number of pairs of soldiers who can see the torch of each other’s hills.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= HILLS[i] <= 10^9
Time Limit: 1 sec
A trivial solution that comes to mind is for each hill we can check all the hills ‘j’ that come earlier in the ‘HILLS’ array. Let’s consider the current hill as ‘HILLS[i]’ and for some ‘HILLS[j]’ where( j < i ) there should not be any hill in-between ‘i’ and ‘j’ such that it is taller than any of the hills. We are counting only for previous hills because if we count for hills ahead as well then we will be counting each pair twice.
Trivially all the soldiers of consecutive hills can see each other as there are no other hills between them so the answer will be at least ‘N’-1.
We can use a stack to store a pair of the height of the current hill and the number of hills we can see from the current hill which can be on the left and right.
The execution of the program will happen in the following way:
If the stack is not empty:
We remove all the people from the stack whose height is less than the current person because they won’t be able to see the hills next to the current hill, As the current hill is higher than the previous hills. So we remove them from the stack and increase the answer by the number of hills we can see from the hills that are being removed from the answer.
If the stack is empty, then we can insert the height of the current hill and 1 in the stack as the person at the current hill can look at the torch at the direct next hill if there is any on the right. We are not looking at the back because the previous hill person already counts the answer.
Else if the top element of the stack has the same height as the current hill’s height, Then we increase the answer by the number of hills the top element hill can see. Now the current hill soldier can see all the soldiers the previous hill soldier can see. Also, the current soldiers can also see the top element of stack hill’s soldiers so we increase the count of the number of soldiers top element hill’s soldiers can see as both the hills has the same height. If there is more than 1 element in the stack, then that means there are elements greater than the height of the current hill then the first greater element can also see the current soldiers so we increase the count of answers by 1.
Else the top element must have a height greater than the current one then the soldiers at that hill can also see the soldiers at the next upcoming hill so we increase the answer count by 1 and push the height of the current hill and 1 as pair in the stack.