Asked at
Kth Smallest Element
EasyVerifiedQuickselectArraySorting~15 min
Given an integer array nums and an integer k, return the kth smallest value (1-indexed).
Quickselect partitions around a pivot and recurses into the side holding rank k-1 — average O(n).
The input arrives as a single object { nums, k }.
Examples
in{ nums: [3, 2, 1, 5, 6, 4], k: 2 }
out2
Sorted: 1, 2, 3, 4, 5, 6 — the 2nd smallest is 2.
in{ nums: [3, 2, 3, 1, 2, 4, 5, 5, 6], k: 4 }
out3
The 4th smallest value is 3.
Constraints
- 1 ≤ k ≤ nums.length ≤ 10⁴
- -10⁴ ≤ nums[i] ≤ 10⁴
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.