


The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘K’. Here ‘N’ denotes the total number of elements present in Goku’s list and ‘K’ is the number given by Goku.
The next line contains ‘N’ single space-separated elements, representing the elements of the list given by Goku.
For each test case, print the minimum number of swaps required to make all elements less than or equal to ‘K’ adjacent to each other. 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 <= 10^5
1 <= data, K <= 10^9
Where 'data' is the value of elements of Goku’s list.
Time Limit: 1sec
In this solution, we will first count the total numbers that are less than or equal to ‘K’, say ‘count’. Then we will calculate the minimum number of swaps for each window of size count by counting numbers that are greater than ‘K’.
For examples :
Let the array = [1, 1, 2, 3, 1] and K = 1
Here count = 3 (As the number of elements less than or equal to 1 are 3).
So, we will calculate the answer for each subarray of size count.
[1, 1, 2]
Here only 2 is greater than 1 so we can swap this element with 1 which is present at the end of the array. Then this subarray becomes [1, 1, 1]. Hence, this subarray/window requires only 1 swap to bring all elements less than or equal to ‘K’ together.
Algorithm:
The idea here is to use the sliding window technique to optimize our previous approach. Instead of running a loop to calculate the minimum number of swaps for each window, we will maintain a variable to keep track of the minimum number of swaps required for the current window.
Algorithm: