You are given an array/list ‘ARR’ containing permutation from 1 to 'N' and an integer ‘K’. You have a binary string ‘S’ of size ‘N’ initialised to all zeroes. You perform ‘N’ operations on 'S' as follows:
In the ‘i-th’ operation, you set the ‘ARR[i]-th’ bit of ‘S’ to ‘1’.
After performing each operation, you get a "group". A "group" of '1s', is a substring of all '1s' in 'S' which can not be extended to its left or right by adding '1s'.
You have to find the last operation when there was a group of size exactly 'K'. If the group of size 'K' was never present, then return -1.
Note :
Both 'ARR' and 'S' are 1 indexed.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain two space-separated integers, ‘N’ and 'K', which denotes the length of the permutation 'ARR', and the required size of the group, respectively.
The second line of each test case will contain 'N' single space-separated integers, representing the permutation 'ARR'.
Output Format :
For each test case, print the last operation when there was a group of size 'K', else print -1.
Output for every test case will be printed in a separate line.
Note :
You don’t need to print anything; It has already been taken care of.
1 <= T <= 10
1 <= N <= 100000
1<= K <=N
1<= ARR[i] <= N
Where ‘T’ is the number of test cases, 'N' is the size of the permutation, 'K' is the size of the required group, and 'ARR[i]' is an element of the permutation.
Time limit: 1 sec
2
3 1
1 3 2
4 3
2 1 4 3
2
-1
In the first test case :
After 1st operation, 'S' = "100". There is 1 group of size 1 from [1,1].
After 2nd operation, 'S' = ''101". There are 2 groups of size 1 from [1,1] and [3,3].
After 3rd operation, 'S' = "111". There is one group of size 3 from [1,3].
A group of size 1 was last present after the second move.
In the second test case :
After 1st operation, 'S' = "0100". There is 1 group of size 1 from [2,2].
After 2nd operation, 'S' = "1100". There is 1 groups of size 2 from [1,2].
After 3rd operation, 'S' = "1101". There is 1 group of size 2 from [1,2] and 1 of size 1 from [4,4].
After 4th operation, 'S' = "1111". There is one group of size 4 from [1,4].
Group of size 3 was never present.
2
1 1
1
7 7
1 2 3 4 5 6 7
1
7
Can we perform all the operations and merge two adjacent groups and create a bigger group?
We will go through all the operations and check which old groups will merge and which new groups will get created.
We will maintain two arrays, 'left' and 'right' to store the length of groups of '1s' ending at 'i' and starting at 'i'.
To get the last index when there was a group of size 'K', we will check whether the newly merged group had size 'K'.
The steps are as follows:
O(N), where ‘N’ is the length of the permutation.
We will do each operation once so, the overall time complexity will be O(N).
O(N), where ‘N’ is the length of the permutation.
Since the space required by 'left' and 'right' array is O(N) so, the overall space complexity will be O(N).