You are given an array ‘ARR’ of size ‘N’ containing each number between 1 and ‘N’ exactly once. On each round, you can traverse through the array ‘ARR’ from left to right and collect as many numbers as possible in increasing order. You are given ‘M’ operations in a 2-dimensional array ‘Queries’, in which Queries[i] contains two positions of the array ‘ARR’, whose values need to be swapped. The operations are performed one by one in the given order. Your task is to find the total number of rounds required to collect all the numbers from 1 to ‘N’ in increasing order after each operation.
For example :
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’.
Output Format :
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
2
3 1
2 1 3
2 3
3 2
3 2 1
1 3
1 2
2
1 2
For the first test case,
In the first operation, we will swap numbers at positions 2 and 3, and then the array will be [2, 3, 1]. We will collect 1 in the first round, and the array after the first round will be [2, 3]. Finally, we will collect 2 and 3 in the second round. Hence, the total number of rounds required is 2 in this case.
For the second test case,
In the first operation, we will swap numbers at positions 1 and 3, and then the array will be [1, 2, 3]. We will collect all numbers in the first round. Hence, the total number of rounds required is 1 in this case.
In the second operation, we will swap numbers at positions 1 and 2, then the array will be [2, 1, 3]. We will collect 1 in the first round, and the array after the first round will be [2, 3]. Finally, we will collect 2 and 3 in the second round. Hence, the total number of rounds required is 2 in this case.
2
5 2
4 2 1 5 3
2 3
3 5
5 2
1 2 3 4 5
2 3
5 4
2 3
2 3
After performing each operation on the array, traverse through the input array in the described manner and collect as many numbers as possible in each round.
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.
Algorithm:
O((N^2)*M), where N denotes the number of elements in the array ARR and M is the number of queries.
We are doing M iterations, and in each iteration, it takes O(N^2) time to traverse through the array ARR to collect numbers. Hence, the overall Time Complexity is O((N^2)*M).
O(1).
Constant space is being used. Hence, the overall Space Complexity is O(1).