Kotlinlearncs.online LogoJava
Return to List

Solve: Array Select Nth Smallest

Created By: Pranav Narayanan
/ Version: 2025.2.0

Write a method select which takes an Array<Int> and an Int value n, and returns the nth smallest value in the array. The array will have at least 1 element, and n will be positive and less than the length of the array.

You may NOT solve this problem by sorting the array - instead, you should use the partition method described here. You should import it using the following code:

The best algorithm for this is called QuickSelect, and it is described here.

You do not need a private helper function to solve this problem, but you are allowed to use one if you wish. Note that you may NOT modify the input array, so you may need to copy it.

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

Attribution must link to this page: https://www.learncs.online/solve/kotlin/array-select-nth-smallest/[email protected]