
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains two space-separated integers ‘N’ and ‘K’ denoting the size of the ‘flowers’ array and the integer ‘K’.
The next line contains ‘N’ space-separated integers denoting the values of elements of the ‘FLOWERS’ array.
For each test case, print a single line containing a single integer denoting the day. If there are multiple answers, choose the smallest, if there isn't such day, output -1.
The output of each 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 given function.
1 <= T <= 5
1 <= N <= 5000
1 <= FLOWERS[i] < 10 ^ 4
Where ‘T’ is the number of test cases, ‘N’ is the size of an array, and ‘FLOWERS[i] = X’ represents that the unique flower that blooms at day ‘i’ will be at position X.
Time Limit: 1 sec.
The idea here is to go to every position and check for the given condition. If at any position our condition becomes true we return that day as the answer.
The idea here is to find the window of size K + 2 such that in window endpoints refer to a position where the flower has bloomed and all other points in the window refer to points where the flower has not bloomed. So we will use the concept of binary index tree here as suppose binaryindex[i] represents the numbers of flower bloomed from day 1 to day N then if binaryindex[i] - binaryindex[i - k - 2] == 2 and day[i] and day[i - k - 1] have flower bloomed then we can say that in window [i - k - 1, i] only day[i] and day[i - k - 1] have flowers bloomed.
So we need to process two types of queries
1. Make flower bloomed at day i i.e. add 1 to index 'i' in Fenwick Tree
2. For each 'i' check if it can be an endpoint of the window.
i.e. Check if window [i - k - 1,i] or [i, i + k + 1] is valid. So we need to do the above two queries fast. So we will use the Binary Index tree to process these two types of queries.
The idea here is to build another array of days where ‘dayOfFlowerBloomed[i]’ represents which day (1-based) a flower at position i + 1 bloom. Now, we can initialize a window [0, K + 1] and try looking for the right window. When we are iterating the days, we look if dayOfFlowerBloomed[i] is greater than ‘dayOfFlowerBloomed[let]’ and ‘dayOfFlowerBloomed[right]’, we can continue our traversal. Else, if dayOfFlowerBloomed[i] < dayOfFlowerBloomed[left] or dayOfFlowerBloomed[i] < dayOfFlowerBloomed[right], becomes true we can say that we have found a flower in between our window that blooms earlier than the left and right flower so it cannot be in the window. So we shift the window and iterate further. If there is a window in which ‘dayOfFlowerBloomed[i]’ is always greater than ‘dayOfFlowerBloomed[left]’ and ‘dayOfFlowerBloomed[right]’, we found a solution
Suppose the array is [ 1, 2, 3, 4 ], k = 1 so we initialize 3 variable ‘left’, ‘right’ and ‘i = left + 1’ and right is initialized from ‘K + 1’ now we start increasing the value of i and compare it with left and right till i is less than right.