![]() |
VOOZH | about |
Given an array of distinct integers and an integer k, where k is smaller than the array's size, the task is to find the k'th smallest element in the array.
Examples:
Input:
arr = [7, 10, 4, 3, 20, 15],k = 3
Output:7
Explanation: The sorted array is[3, 4, 7, 10, 15, 20], so the 3rd smallest element is7.Input:
arr = [7, 10, 4, 3, 20, 15],k = 4
Output:10
Explanation: The sorted array is[3, 4, 7, 10, 15, 20], so the 4th smallest element is10.
Please note that there are multiple ways to solve this problem discussed in kth-Smallest/Largest Element in Unsorted Array. The solution discussed here works best in practice.
The idea is to use a randomized pivot selection to partition the array, reducing the search space by focusing on the subarray where the k'th element must lie.
Step by step approach:
Choose a Random Pivot: Randomly select an element as the pivot. This helps avoid the worst-case scenario in some cases (like when the array is already sorted).
Partitioning: Rearrange the array such that all elements less than the pivot are on the left side, and those greater than the pivot are on the right side.
Recursive Search: Once the pivot is positioned, if its index equals n-kcomparison , then it’s the Kth largest element. If not, recursively search the appropriate partition (left or right) based on the with n-k.
5
Time Complexity: O(n) The worst-case time complexity of the above solution is still O(n2). In the worst case, the randomized function may always pick a corner element. However, the average-case time complexity is O(n). The assumption in the analysis is, random number generator is equally likely to generate any number in the input range.
Auxiliary Space: O(1) since using constant variables.
Even if the worst case time complexity is quadratic, this solution works best in practice.