Count Subarrays with K Disjoint Equal Pairs
Reported by candidates from Capital One's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Capital One reported this one in September 2026, and the constraint is the whole story: nums can hit 2 * 10^5 elements, so checking every subarray at O(n^2) is dead on arrival. The task is counting subarrays with at least k disjoint equal pairs, where each value contributes floor(freq/2) pairs. The hinted tag says union-find, but the real engine is a sliding window with two pointers. If you've got an OA invite and 48 hours, learn this shape. StealthCoder is the safety net on the live OA if your mind goes blank on the window logic.
The problem
Given an integer array nums and a positive integer k, count contiguous subarrays that contain at least k pairwise disjoint pairs of equal values. Each array occurrence may belong to at most one pair. Therefore, a subarray with frequencies freq[x] contains sum floor(freq[x] / 2) disjoint equal pairs. Function countSubarraysWithEqualPairs(nums: int[], k: int) → long Examples Example 1 nums = [1,1,2] k = 1 return = 2 The qualifying subarrays are [1,1] and [1,1,2]. Example 2 nums = [1,1,1,1] k = 2 return = 1 Only the full array contains two disjoint equal pairs. Constraints 1 <= nums.length <= 2 * 10^5. 1 <= k <= nums.length / 2. The answer fits in a 64-bit signed integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is monotonicity. Pairs only grow as a subarray extends right, and only shrink as the left edge moves right. So for each left index, there's a smallest right index where the pair count reaches k, and every longer extension also qualifies. Use two pointers and a frequency map. Keep a running pair total. When you add a value, if its new frequency is even, pairs go up by one. When you remove a value, if its old frequency was even, pairs go down by one. Expand right until pairs >= k, then add n - right to the answer, then advance left. Pitfall: forgetting that three copies give only one pair, and using int instead of long for the count. Union-find doesn't help here, so don't force it. StealthCoder is the hedge on the live OA if the add/remove parity updates trip you up under pressure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Subarrays with K Disjoint Equal Pairs cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Capital One's OA.
Capital One reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Subarrays with K Disjoint Equal Pairs FAQ
What's the trick for Count Subarrays with K Disjoint Equal Pairs?+
Sliding window with a frequency map. Pair count only rises as you extend right and only falls as you shrink left. Once a window hits k pairs, every longer window with the same left edge also qualifies, so you add n minus right to the total in O(1).
Why not brute force this one?+
The length goes up to 2 * 10^5. Checking every subarray is O(n^2) pairs of endpoints, around 4 * 10^10 operations, and recounting frequencies makes it worse. You need near-linear time, which the two-pointer window gives you with a hash map.
Is union-find actually needed here?+
No. The hint points at union-find, but the problem is about counting frequencies inside a moving window. Nothing merges or connects components. Use a hash map of counts and two pointers. Reaching for union-find will waste your time.
How do I update the pair count when the window changes?+
On adding x, increment freq[x]. If the new freq is even, pairs go up by 1. On removing x, check if the current freq is even before decrementing. If so, pairs go down by 1. Odd-to-even and even-to-odd transitions are the only ones that matter.
How do I prepare for this in 48 hours?+
Practice two or three variable-window counting problems where the condition is monotonic, like subarrays with at least K of something. Write the add/remove logic from memory, use a 64-bit type for the answer, and test with [1,1,1,1] and k=2, which should return 1.