|
| 1 | +--- |
| 2 | +id: kth-smallest |
| 3 | +title: Kth Smallest |
| 4 | +sidebar_label: Kth-Smallest |
| 5 | +tags: |
| 6 | + - Sorting |
| 7 | + - Algorithms |
| 8 | + - Arrays |
| 9 | + - Searching |
| 10 | +description: "This tutorial covers the solution to the Kth Smallest problem from the GeeksforGeeks." |
| 11 | +--- |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given an array `arr[]` and an integer `k` where `k` is smaller than the size of the array, the task is to find the `kth` smallest element in the given array. It is given that all array elements are distinct. |
| 15 | + |
| 16 | +Note:- `l` and `r` denotes the starting and ending index of the array. |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +``` |
| 23 | +Input: |
| 24 | +n = 6 |
| 25 | +arr[] = 7 10 4 3 20 15 |
| 26 | +k = 3 |
| 27 | +l=0 r=5 |
| 28 | +
|
| 29 | +Output : |
| 30 | +7 |
| 31 | +
|
| 32 | +Explanation : |
| 33 | +3rd smallest element in the given |
| 34 | +array is 7. |
| 35 | +``` |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | +``` |
| 39 | +Input: |
| 40 | +n = 5 |
| 41 | +arr[] = 7 10 4 20 15 |
| 42 | +k = 4 |
| 43 | +l=0 r=4 |
| 44 | +
|
| 45 | +Output : |
| 46 | +15 |
| 47 | +
|
| 48 | +Explanation : |
| 49 | +4th smallest element in the given |
| 50 | +array is 15. |
| 51 | +``` |
| 52 | + |
| 53 | +### Your Task |
| 54 | +You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer k as input and returns the kth smallest element. |
| 55 | + |
| 56 | +Expected Time Complexity: O(n*log(n)) |
| 57 | + |
| 58 | +Expected Auxiliary Space: O(1) |
| 59 | + |
| 60 | +## Constraints |
| 61 | + |
| 62 | +* `1 ≤ N ≤ 10^5` |
| 63 | + |
| 64 | +## Problem Explanation |
| 65 | + |
| 66 | +Given an array `arr[]` and an integer `k` where `k` is smaller than the size of the array, the task is to find the `kth` smallest element in the given array. It is given that all array elements are distinct. |
| 67 | + |
| 68 | +Note:- `l` and `r` denotes the starting and ending index of the array. |
| 69 | + |
| 70 | +## Code Implementation |
| 71 | + |
| 72 | +### C++ Solution |
| 73 | + |
| 74 | +```cpp |
| 75 | +int kthSmallest(int arr[], int l, int r, int k) { |
| 76 | + sort(arr + l, arr + r + 1); |
| 77 | + return arr[l + k - 1]; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +``` |
| 82 | +
|
| 83 | +```java |
| 84 | +
|
| 85 | +int kthSmallest(int arr[], int l, int r, int k) { |
| 86 | + Arrays.sort(arr, l, r + 1); |
| 87 | + return arr[l + k - 1]; |
| 88 | +} |
| 89 | +
|
| 90 | +
|
| 91 | +``` |
| 92 | + |
| 93 | +```python |
| 94 | +def kthSmallest(arr, l, r, k): |
| 95 | + arr.sort() |
| 96 | + return arr[l + k - 1] |
| 97 | + |
| 98 | + |
| 99 | +``` |
| 100 | + |
| 101 | +```javascript |
| 102 | +function kthSmallest(arr, l, r, k) { |
| 103 | + arr.sort((a, b) => a - b); |
| 104 | + return arr[l + k - 1]; |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +## Solution Logic: |
| 111 | + |
| 112 | +1. sort(arr + l, arr + r + 1) (C++), arr.sort() (Python, JavaScript, TypeScript): |
| 113 | + - Sort the array in ascending order. |
| 114 | + - This step is necessary to ensure that the array is in a sorted state, allowing us to easily access the kth smallest element. |
| 115 | +2. return arr[l + k - 1]: |
| 116 | + - Return the element at the index l + k - 1. |
| 117 | + - l is the starting index of the array, k is the position of the element to be found, and -1 is to adjust for zero-based indexing. |
| 118 | + - Since the array is sorted in ascending order, the element at this index will be the kth smallest element. |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +## Time Complexity |
| 123 | + |
| 124 | +* The time complexity is $O(n*log(n))$ due to the sorting step, where n is the length of the array. |
| 125 | + |
| 126 | +## Space Complexity |
| 127 | + |
| 128 | +* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array. |
0 commit comments