Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Find the Kth Largest Integer in the Array
Bookmark
Input
Standard
Medium
Large Numbers
Same Length
Single
Custom
nums
=
[3, 6, 7, 10]
,
k
=
2
target = 2
target
"3"
0
"6"
1
"7"
2
"10"
3
quickselect: the kth largest lives at sorted index n − k
target = 2
target
"3"
0
"6"
1
"7"
2
"10"
3
quickselect: the kth largest lives at sorted index n − k
target = 2
pivot "6"
target
"3"
0
"6"
pivot
1
"7"
2
"10"
3
pick pivot =
"6"
target = 2
pivot "6"
target
"3"
0
"6"
pivot
1
"7"
2
"10"
3
lo
hi
mid
less than pivot → swap into the
< zone
target = 2
pivot "6"
target
"3"
0
"6"
1
"7"
2
"10"
3
lo
hi
mid
equal to pivot → keep in the
= zone
target = 2
pivot "6"
target
"3"
0
"6"
1
"10"
2
"7"
3
lo
hi
mid
compare
"10"
vs pivot
"6"
→
> pivot
target = 2
target
"3"
0
"6"
1
"10"
2
"7"
3
recurse on the
right partition
target = 2
pivot "10"
target
"3"
0
"6"
1
"10"
pivot
2
"7"
3
lo
hi
mid
3-way partition around the pivot (Dutch national flag)
target = 2
pivot "10"
target
"3"
0
"6"
1
"10"
2
"7"
3
lo
hi
mid
equal to pivot → keep in the
= zone
target = 2
pivot "10"
< pivot
= pivot
target
"3"
0
"6"
1
"7"
2
"10"
3
lo
hi
mid
target index 2 is
in the < zone → search left
target = 2
2nd = "7"
target
"3"
0
"6"
1
"7"
2
"10"
3
2nd largest = "7"
algo
master
.
io
Step:
Start: Find the 2nd largest integer in the array
0 / 25
Input
Standard
Medium
Large Numbers
Same Length
Single
Custom
nums
=
[3, 6, 7, 10]
,
k
=
2
0 / 25
algo
master
.
io
Step:
Start: Find the 2nd largest integer in the array