Count subarrays with ≥k identical pairs
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates proficiency in array algorithms, frequency-based counting and combinatorial reasoning for identifying equal-element pairs within contiguous subarrays.
Constraints
- 1 <= nums.length <= 10^5 (the array may also be empty in edge tests)
- 1 <= nums[i] <= 10^9
- 1 <= k <= 10^9
- The answer can exceed 32-bit range; use a 64-bit integer accumulator
Examples
Input: ([1, 1, 1, 1], 1)
Expected Output: 6
Explanation: Value 1 is duplicated in any subarray of length >= 2; there are 6 such subarrays out of the 10 total.
Input: ([3, 1, 4, 3, 2, 2, 4], 2)
Expected Output: 4
Explanation: Four subarrays contain at least 2 distinct values that each appear twice (e.g. windows spanning the repeated 3, 2, and 4).
Input: ([1, 2, 3, 4], 1)
Expected Output: 0
Explanation: All elements are distinct, so no subarray has even one duplicated value.
Input: ([5, 5, 5], 1)
Expected Output: 3
Explanation: The subarrays [5,5] (twice) and [5,5,5] each contain the duplicated value 5 -> 3 good subarrays.
Input: ([], 1)
Expected Output: 0
Explanation: Empty array has no subarrays.
Input: ([7], 1)
Expected Output: 0
Explanation: A single element can never form a duplicate, so 0 good subarrays.
Input: ([2, 2, 2, 2], 1)
Expected Output: 6
Explanation: Every subarray of length >= 2 contains the duplicated value 2; there are 6 of them.
Input: ([1, 1, 2, 2, 3, 3], 3)
Expected Output: 1
Explanation: Only the full array has 3 distinct values (1, 2, 3) that each appear twice.
Hints
- A subarray is 'good' if the count of distinct values appearing >= 2 times is at least k. Track that count as a running quantity called `pairs`.
- Use a sliding window: when a value's frequency goes from 1 to 2, increment `pairs`; when it drops from 2 to 1 as the left edge shrinks, decrement `pairs`.
- Monotonicity: if a window [left, right] already has pairs >= k, then EVERY window [left', right] with left' <= left also qualifies. So count `n - right` subarrays and advance left while the window stays good, summing as you go.