#2537

Count the Number of Good Subarrays

specialist · 620 · lc medium +29 · verified · 65.9% accepted · 1,558 likes · top 71%

Description

Given an integer array nums and integer k, count and return the number of contiguous non-empty subarrays that contain at least k pairs of equal-valued elements at distinct positions (i, j) with i < j.

Example 1:

Input: nums = [1,1,1,1,1], k = 10
Output: 1
Explanation: The only good subarray is the array nums itself.

Example 2:

Input: nums = [3,1,4,3,2,2,4], k = 2
Output: 4
Explanation: There are 4 different good subarrays:
- [3,1,4,3,2,2] that has 2 pairs.
- [3,1,4,3,2,2,4] that has 3 pairs.
- [1,4,3,2,2,4] that has 2 pairs.
- [4,3,2,2,4] that has 2 pairs.

Code

1
2
3