Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Subarrays with K Different Integers
Bookmark
Brute Force
At Most K
Two Pointers
Input
Input 1
Input 2
Repeats
k = 3
Custom
nums
=
[1, 2, 1, 2, 3]
,
k
=
2
0
1
2
3
4
1
2
1
2
3
distinct
0
/ 2 needed
count
0
0
1
2
3
4
1
2
1
2
3
distinct
0
/ 2 needed
count
0
0
1
2
3
4
1
2
1
2
3
left
right
include nums[
1
] =
2
distinct
1
/ 2 needed
count
0
0
1
2
3
4
exactly 2 distinct +1
1
2
1
2
3
left
right
exactly 2 distinct, count =
1
+
1
=
2
distinct
2
/ 2 needed
count
2
0
1
2
3
4
1
2
1
2
3
left
right
distinct =
3
> 2: this window does not count
distinct
3
/ 2 needed
count
3
0
1
2
3
4
1
2
1
2
3
left
right
distinct =
2
distinct
2
/ 2 needed
count
3
0
1
2
3
4
1
2
1
2
3
left
right
include nums[
4
] =
3
distinct
2
/ 2 needed
count
5
0
1
2
3
4
1
2
1
2
3
left
right
distinct =
1
< 2: this window does not count
distinct
1
/ 2 needed
count
5
0
1
2
3
4
1
2
1
2
3
left
right
distinct =
3
> 2: this window does not count
distinct
3
/ 2 needed
count
6
0
1
2
3
4
1
2
1
2
3
left
right
distinct =
2
distinct
2
/ 2 needed
count
6
0
1
2
3
4
1
2
1
2
3
count =
7
algo
master
.
io
Step:
Start of algorithm
0 / 47
Input
Input 1
Input 2
Repeats
k = 3
Custom
nums
=
[1, 2, 1, 2, 3]
,
k
=
2
0 / 47
algo
master
.
io
Step:
Start of algorithm