Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Frequency of the Most Frequent Element
Bookmark
Brute Force
Sliding Window
Prefix + Binary Search
Input
Input 1
Input 2
Unsorted
Longer
Custom
nums
=
[1, 2, 4]
,
k
=
5
0
1
2
1
2
4
0
1
2
1
2
4
0
1
2
1
2
4
sort ascending, then extend left from each index
0
1
2
1
2
4
maxFreq =
1
extend left from every index
0
1
2
1
2
4
target 2
maxFreq = 1
left
right
cost =
1
vs k =
5
fits, count = 1
maxFreq =
1
cost =
1
k
k =
5
0
1
2
1
2
4
target 2
maxFreq = 2
left
right
2
> old maxFreq
new best
maxFreq =
2
maxFreq =
2
cost =
1
k
k =
5
0
1
2
1
2
4
target 4
maxFreq = 2
left
right
cost +=
4
−
2
=
2
, cost =
2
maxFreq =
2
cost =
2
k
k =
5
0
1
2
1
2
4
target 4
maxFreq = 2
left
right
cost =
2
vs k =
5
fits, count = 1
maxFreq =
2
cost =
2
k
k =
5
0
1
2
1
2
4
target 4
maxFreq = 2
left
right
cost =
5
vs k =
5
fits, count = 2
maxFreq =
2
cost =
5
k
k =
5
0
1
2
1
2
4
target 4
maxFreq = 3
left
right
3
> old maxFreq
new best
maxFreq =
3
maxFreq =
3
cost =
5
k
k =
5
0
1
2
1
2
4
maxFreq = 3
maxFreq =
3
algo
master
.
io
Step:
Start of algorithm
0 / 11
Input
Input 1
Input 2
Unsorted
Longer
Custom
nums
=
[1, 2, 4]
,
k
=
5
0 / 11
algo
master
.
io
Step:
Start of algorithm