Subarrays With Enough Pairs
HardSliding Window ยท Capital One
Given an integer array codes and an integer k, return the number of contiguous subarrays in which you can form at least k pairs of equal values, with each element used in at most one pair.
Subarrays are counted by position, so equal subarrays at different positions count separately.
Example 1
- Input:
- codes = [4, 4, 7, 4, 7], k = 2
- Output:
- 2
- Explanation
- Only the whole array and
[4, 7, 4, 7]work; each pairs two 4s and two 7s.
Example 2
- Input:
- codes = [5, 5, 5, 5, 5], k = 2
- Output:
- 3
Example 3
- Input:
- codes = [1, 2, 3], k = 1
- Output:
- 0
Constraints
1 <= codes.length <= 200000-10^9 <= codes[i] <= 10^91 <= k <= 100000