
Consider the array ARR = [4, 5, 1, 2, 3], we will collect 1,2 and 3 in the first round, and the array ARR after the first round will be [4, 5]. Finally, we will collect 4 and 5 in the second round. The minimum total number of rounds required is 2. Hence, the answer is 2 in this case.
The first line of the input contains an integer, 'T,’ denoting the number of test cases. The 'T' test cases follow.
The first line of each test case contains a single 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 'ARR'.
For each test case, print a single integer - the minimum total number of rounds required to collect the numbers from 1 to ‘N’ in increasing order.
Print the output of each test case in a separate line.
1 <= T <= 10
1 <= N <= 10^5
1 <= ARR[i] <= N
All elements present in the array ARR are unique.
Time limit: 1 sec
A simple method is to traverse through the array ARR on each round until we have collected all numbers from 1 to N in increasing order.
Therefore, 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 the 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. In the end, we will return the total number of rounds that we required to collect all numbers.
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 and maintain a variable numberIndex, which stores the value of the index at which we are currently present in the ARR. We will initialize numberIndex as 0. We will iterate currentNumber from 1 to N. We will find the index of currentNumber with the help of the array positions. Now, there will be two cases,
In the end, we will return the total number of rounds that we required to collect all numbers.