Problems > Exactly K Distinct

Exactly K Distinct

Difficulty: 1900 4000 ms 128 MB

two-pointers

Count the contiguous subarrays containing EXACTLY K distinct values.

Input: the first line has N and K; the second line has N integers.

Output: the number of subarrays with exactly K distinct values.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ K ≤ N
  • 1 ≤ ai ≤ 109

Sample Tests

Input 1
4 2
1 2 1 3
Output 1
4
Input 2
3 1
5 5 5
Output 2
6

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.