Skip to content
Onsite

Subarrays with K Different Integers

HardP3Coding

Given an integer array nums and an integer k, return the number of contiguous subarrays that contain exactly k different integers.

Subarrays are counted by position, so equal subarrays at different positions count separately.

Example 1

Input
nums = [1,2,1,2,3], k = 2
Output
7

Example 2

Input
nums = [1,2,1,3,4], k = 3
Output
3

Example 3

Input
nums = [1,1,1], k = 1
Output
6

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • 1 <= nums[i], k <= nums.length

Hints

Run checks the 3 visible tests, Submit all 12.