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).
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.