

‘0 <= i < j < N’.
‘ARR[i] > ARR[j]’.
‘0 <= i < N - 1’.
‘ARR[i] > ARR[i + 1]’
‘N = 6’
‘ARR’ = [2, 0, 1, 5, 6, 3]’
Pair of indexes that are global inversions: ‘(0, 1)’, ‘(0, 2)’, ‘(3, 5)’, ‘(4, 5)’.
Indexes that are local inversions: ‘0’, ‘4’.
Thus, you should return ‘False’ as the answer as the number of global inversions, and the number of local inversions is not equal.
1. All integers in ‘ARR’ are unique and ‘ARR’ is a permutation of the integers in the range [0, N - 1].
The first line of input contains an integer ‘T’ which denotes the number of test cases.
Then, the ‘T’ test cases follow.
The first line of each test case contains an integer ‘N’ denoting the size of the array ‘ARR’.
The second line of each test case contains ‘N’ integers representing the array ‘ARR’.
For every test case, return ‘True' if the number of global inversions is equal to the number of local inversions. Otherwise, return ‘False’.
Output for every test case will be printed in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= N <= 1000
Elements of ‘ARR’ are in the range [0, N - 1].
Time limit: 1 sec
A simple approach would be to use two nested loops to count the number of global inversions and a single loop to count the number of local inversions.
Algorithm:
Observe that all the local inversions are also global inversions. So, we can greedily check if there exists at least one global inversion that is not a local inversion. In such a scenario, the local inversions will never be equal to global inversions.
Store the maximum value in ‘ARR’ up to ‘i’ (including ‘i’), let’s say 'CUR_MAX'. If 'CUR_MAX' is greater than ‘ARR[i + 2]’ (as ‘i + 1’ is a local inversion), then there exists at least one global inversion, which is not a local inversion. If 'CUR_MAX' is smaller than ‘ARR[i + 2]’, then all the values before ‘i + 1’ are also smaller, so we will always find that extra global inversion if it exists.
Algorithm:
We stated in the previous approach: “If there is at least one global inversion that is not a local inversion, then the total global inversions will never be equal to the local inversions”. Also, any index can have at most one local inversion. So, if we find an index that is part of more than one global inversion, we should return ‘False’ as the answer.
Consider ‘ARR = [0, 1, 2, 3, 4, 5, 6]’, i.e., in sorted order. We try to place the integer ‘3’ at different locations:
We can generalize these for the right shifts. Let’s ‘DIST[i]’ be the distance of ‘ARR[i]’ from its position in the sorted order. So, for every ‘i’, ‘ARR[i]’ will be part of at least ‘DIST[i]’ global inversions.
As per the question, the array ‘ARR’ is a permutation of the integers from ‘[0, N - 1]’. So, the position of ‘ARR[i]’ in sorted order is ‘ARR[i]’.
We have ‘DIST[i] = abs(ARR[i] - i)’, where ‘abs(X)’ is the absolute value of 'X'.
As per our previous analysis, if ‘DIST[i] > 1’, we should return ‘False’. Also, if for every ‘i’ we have ‘DIST[i] <= 1’, then no integer has moved more than one distance from its sorted position, so every inversion has to be a local inversion.
Algorithm: