
Consider ARR = [4, 5, 1, 2, 3] and Queries=[[2, 3]], in the first operation, we will swap numbers at positions 2 and 3, then the array will be [4, 1, 5, 2, 3]. We will collect 1, 2, and 3 in the first round, and the array after the first round will be [4, 5]. Finally, we will collect 4 and 5 in the second round. Hence, the total number of rounds required is 2 in this case.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and ‘M’, denoting the number of elements in the array 'ARR', and the number of operations to be performed, respectively.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR'.
The Next 'M' lines of each test case contain two space-separated integers denoting the elements of the array ‘Queries’.
For each test case, print ‘M’ space-separated integers - the total number of rounds required to collect the numbers from 1 to ‘N’, in increasing order after each operation.
Print the output of each test case in a separate line.
1 <= T <= 10
1 <= N <= 10^5
1 <= ARR[i] <= N
1 <= Queries[i][0], Queries[i][1] <= N
All elements present in the array ARR are unique.
Queries[i][0] is not equal to Queries[i][1].
Where 'T' denotes the number of test cases, 'N' denotes the number of elements in the array, 'ARR[i]' denotes the 'i'th' element of the array 'ARR' and 'Queries[i]' is a tuple of two positions of the array ‘ARR’, whose numbers are needed to be swapped.
Time limit: 1 sec
A simple method is to perform the operation by swapping the numbers present at the given two positions and traversing through the array ARR on each round until we have collected all numbers from 1 to N in increasing order.
To obtain the total number of rounds after each operation, we will maintain a variable currentNumber, which stores the element’s value, which we are trying to collect from the ARR. On each round, we will iterate index from 0 to N-1, and we will check if ARR[index] is equal to currentNumber, then we will increment currentNumber by 1. After each round, we will check if all numbers are collected. If all numbers are not collected from the ARR, then we will perform another round.
The idea is to observe the fact that whenever the number val occurs before val+1, we can always take both of them in a single round, but if val comes after val+1, we cannot take them in the single round, and we will need to make an extra round to collect val+1. We will use this idea to find the total number of rounds to collect all numbers.
Our approach will be to construct array positions, which will store the index of all elements in the array ARR. For each query, we will perform the operation by swapping the numbers present at the given two positions and update the index of the value placed at both positions in the array positions. We will iterate currentNumber from 2 to N and we will find the index of currentNumber and currentNumber-1 with the help of the array positions. Now, there will be two cases,
As we have discussed in the previous approach, for each value val in the array ARR, we are considering two pairs, i.e, (val-1, val) and (val, val+1). If the index of the first value is greater than the index of the second value, then we need to make an extra round to collect numbers from the second value.
Our approach will be to construct array positions, which will store the index of all elements in the array ARR. We will find the total number of rounds of the current permutation using the same approach used in the previous approach. For each query, we are swapping two numbers placed at positions x and y. We need to consider four pairs to find the total rounds of the modified permutation. The four pairs are (ARR[x] - 1, ARR[x]), (ARR[x], ARR[x] + 1), (ARR[y] - 1, ARR[y]) and (ARR[y], ARR[y] + 1). Our approach will be to decrement the total number of rounds of the current permutation by the number of pairs having an index of the first value greater than the second value. Then we will swap the numbers placed at the positions x and y, and we will update the index of the number placed at x and y in the array positions. Finally, we will increment the total number of rounds by the number of pairs following the given conditions to obtain the total number of rounds of the modified permutation. Note that we will consider each pair no more than once to avoid duplicate values.