FastPrepCount Subarrays with K Disjoint Equal Pairs

Count Subarrays with K Disjoint Equal Pairs

Capital One logoCapital One● MediumFULLTIMEOA
Learn

Problem statement

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 = 1return = 2

The qualifying subarrays are [1,1] and [1,1,2].

Example 2

nums = [1,1,1,1]k = 2return = 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.

Source note: Source screenshot from a reported Capital One online assessment.

More Capital One problems

See Capital One hiring insights
public long countSubarraysWithEqualPairs(int[] nums, int k) {
  // write your code here
}
nums[1,1,2]
k1
expected2
Checking account…