Given an integer array 'NUMS' of size 'N' and an integer 'K'.
Return the number of good subarrays of 'NUMS'.
A subarray 'ARR' is good if there are at least 'K' pairs of indices (i, j) such that i < j and ARR[i] == ARR[j].
A subarray is a contiguous non-empty sequence of elements within an array.
Let 'N' = 5, 'NUMS' = [1, 2, 1, 1, 3], K = 2.
Consider the subarray [1, 2, 1, 1] (indices 0 to 3 of NUMS). The elements are 1, 2, 1, 1.
Pairs with equal values and i < j within this subarray are:
(1 at index 0, 1 at index 2), (1 at index 0, 1 at index 3), (1 at index 2, 1 at index 3).
There are 3 such pairs. Since 3 >= K (2), this subarray is good.
Consider the subarray [1, 2, 1, 1, 3] (indices 0 to 4 of NUMS). The elements are 1, 2, 1, 1, 3.
Pairs with equal values and i < j within this subarray are:
(1 at index 0, 1 at index 2), (1 at index 0, 1 at index 3), (1 at index 2, 1 at index 3).
There are 3 such pairs. Since 3 >= K (2), this subarray is good.
Consider the subarray [2, 1, 1] (indices 1 to 3 of NUMS). The elements are 2, 1, 1.
Pairs with equal values and i < j within this subarray are:
(1 at index 1, 1 at index 2).
There is 1 such pair. Since 1 < K (2), this subarray is not good.
It can be shown that the only good subarrays are [1, 2, 1, 1] and [1, 2, 1, 1, 3].
Therefore, the total number of good subarrays is 2.
The first line contains two integers, 'N' and 'K'.
The second line contains 'N' integers, the elements of the array 'NUMS'.
Output Format :
Return the number of good subarrays.
Note :
You don't need to print anything. Just implement the given function.
1 <= N <= 10^5
0 <= K <= 10^10
0 <= NUMS[i] <= 10^5
Time Limit: 1 sec
5 2
1 1 2 1 1
3
Here N = 5, NUMS = [1, 1, 2, 1, 1], K = 2.
Consider the subarray [1, 1, 2, 1] (indices 0 to 3 of NUMS). Elements are 1, 1, 2, 1.
Pairs with equal values and i < j within this subarray are:
(1 at index 0, 1 at index 1), (1 at index 0, 1 at index 3), (1 at index 1, 1 at index 3).
There are 3 such pairs. Since 3 >= K (2), this subarray is good.
Consider the subarray [1, 1, 2, 1, 1] (indices 0 to 4 of NUMS). Elements are 1, 1, 2, 1, 1.
Pairs with equal values and i < j within this subarray are:
(1 at index 0, 1 at index 1), (1 at index 0, 1 at index 3), (1 at index 0, 1 at index 4), (1 at index 1, 1 at index 3), (1 at index 1, 1 at index 4), (1 at index 3, 1 at index 4).
There are 6 such pairs. Since 6 >= K (2), this subarray is good.
Consider the subarray [1, 2, 1, 1] (indices 1 to 4 of NUMS). Elements are 1, 2, 1, 1.
Pairs with equal values and i < j within this subarray are:
(1 at index 1, 1 at index 3), (1 at index 1, 1 at index 4), (1 at index 3, 1 at index 4).
There are 3 such pairs. Since 3 >= K (2), this subarray is good.
It can be shown that all other subarrays have less than 2 pairs.
The good subarrays are [1, 1, 2, 1], [1, 1, 2, 1, 1], and [1, 2, 1, 1].
Thus, the total number of good subarrays is 3.
4 3
1 2 1 2
0
Use a sliding window approach.
Approach:
Algorithm:
O(N), where 'N' is the size of the array 'NUMS'.
We iterate through the array 'NUMS' with the 'right' pointer once. The 'left' pointer also moves forward at most 'N' times in total across all iterations of 'right'. The operations within the loop (frequency map operations and pair count updates) take approximately constant time on average. Thus, the overall time complexity is of the order O(N).
O(V), where 'V' is the number of distinct values in 'NUMS'. In the worst case, V can be up to N. Given the constraint on NUMS[i], V is at most 10^5.
We use a frequency map to store the counts of elements in the current window. In the worst case, all elements in 'NUMS' are distinct, and the map stores up to 'N' entries. However, if the range of values in 'NUMS' is limited (e.g., up to 10^5), the size of the map is bounded by this range. Thus, the overall space complexity is of the order O(V).