A bracelet-stringing log records the color (a positive integer) of each bead threaded on, in order, as an array of n integers. Given an integer K, count how many contiguous subarrays contain EXACTLY K distinct bead colors.
Line 1: two integers n and K.
Line 2: n space-separated positive integers, the bead colors in order.
A single integer: the number of contiguous subarrays with exactly K distinct colors.
Example 1
Input
5 2 1 2 1 3 2
Expected
5
Explanation
The subarrays with exactly 2 distinct colors are [1,2], [1,2,1], [2,1], [1,3], and [3,2] — 5 in total.
Example 2
Input
4 1 7 7 7 7
Expected
10
Explanation
Every one of the 10 possible subarrays has exactly 1 distinct color (7), so the count is 10.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →