algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/261 solved

Exact Distinct Subarray Count

Gold
Asked atCodingMart
Solve problem →
2000ms256MBAdded Aug 22, 2026

You are given an integer array nums and an integer k.

A subarray is a contiguous, non-empty part of the array.

Return the number of subarrays of nums that contain exactly k distinct values.

Examples

Example 1

Input: nums = [1,2,1,2,3], k = 2
Output: 7
Explanation: The subarrays with exactly 2 distinct values are [1,2], [2,1],
[1,2], [2,3], [1,2,1], [2,1,2], and [1,2,1,2].

Example 2

Input: nums = [1,2,1,3,4], k = 3
Output: 3
Explanation: The subarrays with exactly 3 distinct values are [1,2,1,3],
[2,1,3], and [1,3,4].

Constraints

  • 1≤1 \leq1≤ nums.length ≤2⋅104\leq 2 \cdot 10^4≤2⋅104
  • 1≤1 \leq1≤ nums[i], k ≤\leq≤ nums.length

Details

Solved by2 people
Time limit2000ms
Memory256MB
AddedAug 22, 20262 weeks ago