


For 'ARR' = [1, 2, 1, 3, 2, 4] and 'B' = 3, one of the good subarrays which contains three distinct integers is [1, 2, 1, 3].
An array 'C' is a subarray of array 'D' if it can be obtained by deletion of several elements(possibly zero) from the beginning and the end from array 'D'.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains two single space-separated integers ‘N’ and ‘B’ denoting the number of integers in the array/list and the given integer.
The second line of each test case contains ‘N’ single space-separated integers, denoting the elements of the array.
For each test case, print an integer denoting the number of good subarrays in the given array/list.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 5000
1 <= B <= N
1 <= ARR[i] <= 10^9
Time Limit: 1sec
We will iterate through all possible subarrays of the array with the help of two nested loops. We will maintain a Set data structure for the count of distinct integers in a subarray.
The algorithm will be-
We can use the method of two pointers with the sliding window technique to find the number of good subarrays with atmost ‘B’ distinct elements. Then we can subtract with it the number of good subarrays with atmost ‘B’ - 1 distinct elements to find our final answer.
The current window will be our current processing subarray-
The algorithm will be-