3
2
1
5
6
4
[0][1][2][3][4][5]
Brute force · sort
▸1given arr, k2sort arr ascending3return arr[n − k]
state
- k2
- n6
warming up the animation
Given an integer array and a value k, return the kth largest element by rank in sorted order, which need not be a distinct value.
▸1given arr, k2sort arr ascending3return arr[n − k]
line 1Find the k = 2nd LARGEST value (by rank, not a distinct value). Easiest idea: sort everything, then read off the element k from the end.