There are ‘N’ empty places and you are given ‘N’ blocks. The task is to fill each place with a block of some height. After filling all the empty places all blocks should be arranged in increasing order of their height.
Given an array 'heights' which denotes the blocks arranged in some order. You have to find and return the number of blocks that are present in the wrong locations.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains one integer ‘N’ denoting the number of elements in the array.
The second line of each test case contains N space-separated integers denoting the elements of the array “heights”.
Output Format :
For each test case, print the number of blocks that are present in the wrong locations.
Print the output of each test case on a new line.
Note :
You don’t need to print anything; It has already been taken care of.
1 <= T <= 5
1 <= N <= 10^5
1 <= heights[i] <= 10^9
Where 'heights[i]' represents the height of the ith block.
Time Limit: 1 sec
2
4
1 5 2 4
4
2 3 5 4
3
2
For the first test case, Blocks with heights 2, 5, 4 are present in the wrong locations.
For the second test case, Blocks with heights 4, 5 are present in the wrong locations.
2
2
1 2
5
5 1 3 2 4
0
4
Can you think of how to arrange the blocks in the correct order?
The idea here is to create a copy of the array 'HEIGHTS', say 'HEIGHT_COPY', and then sort the 'HEIGHT_COPY' array. Now check if the element present at 'HEIGHTS'[i] is not equal to the 'HEIGHT_COPY'[i], then increment the 'ANS' count by 1.
The algorithm is as follows:
O(N*logN), where N is the size of the array heights.
Since we are sorting the array heightCopy which takes the order of N*logN time. Hence the overall time complexity is O(N*logN).
O(N), where N is the number of vertices.
Since we are using an auxiliary array heightCopy which is of order N. Hence the overall space complexity is O(N).