FindKthMin returns kth small element given an integer slice with nil `error` if found and returns -1 with `error` `search.ErrNotFound` if not found. NOTE: The `nums` slice gets mutated in the process.
(nums []int, k int)
| 17 | // with nil `error` if found and returns -1 with `error` `search.ErrNotFound` |
| 18 | // if not found. NOTE: The `nums` slice gets mutated in the process. |
| 19 | func FindKthMin(nums []int, k int) (int, error) { |
| 20 | index := k - 1 |
| 21 | return kthNumber(nums, index) |
| 22 | } |
| 23 | |
| 24 | // kthNumber use the selection algorithm (based on the partition method - the same one as used in quicksort). |
| 25 | func kthNumber(nums []int, k int) (int, error) { |