Count the Number of Good Subarrays

Given an integer array nums and an integer k, return the number of good subarrays of nums.

A subarray arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1
Inputnums = [1,1,1,1,1], k = 10
Output1
The only good subarray is the array nums itself.
Example 2
Inputnums = [3,1,4,3,2,2,4], k = 2
Output4
There are 4 different good subarrays: [3,1,4,3,2,2], [3,1,4,3,2,2,4], [1,4,3,2,2,4], and [4,3,2,2,4].

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i], k <= 10^9

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate