Kth smallest element | Practice | GeeksforGeeks

Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.
Example 1:
Input:
N = 6
arr[] = 7 10 4 3 20 15
K = 3
Output : 7
Explanation :
3rd smallest element in the given 
array is 7.

Example 2:
Input:
N = 5
arr[] = 7 10 4 20 15
K = 4
Output : 15
Explanation :
4th smallest element in the given 
array is 15.
Constraints:
1 <= N <= 105
1 <= arr[i] <= 105
1 <= K <= N
 
+1
diwakarghosh42 hours ago
Python Solution 1liner
arr.sort()
return arr[k-1]
0
kumkumjain61612 hours ago
class Solution{
    public static int kthSmallest(int[] arr, int l, int r, int k) 
    { 
       PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
       for(int i =0; i<k;i++){
           pq.add(arr[i]);
       }
       for(int i = k; i<=r;i++){
           if(arr[i] < pq.peek()){
               pq.poll();
               pq.add(arr[i]);
           }
       }
      int ans = pq.peek();
      return ans;
    } 
}

+1
mamidisettynikhilesh18 hours ago
class Solution{   public static int kthSmallest(int[] arr, int l, int r, int k)    {        //Your code here       Arrays.sort(arr);       return arr[k-1];   } }
0
himanshusethin2 days ago
sort(arr,arr+r+1);       return arr[k-1];
-1
singhpriti2813 days ago
priority_queue<int>maxh; 
   for(int i=l; i<r; i++){       maxh.push(arr[i]);        if(maxh.size()>k){            maxh.pop();       }     }  return maxh.top();
0
parmeetsghai
This comment was deleted.
+1
harshscode4 days ago
in O(n)...min heap.......
priority_queue<int,vector<int>,greater<int>> q;
        for(int i=l;i<=r;i++)
        q.push(a[i]);
        
        for(int i=1;i<k;i++)
        q.pop();
        int ans=q.top();
        return ans;
0
koulikmaity5 days ago
int kthSmallest(int arr[], int l, int r, int k) {       priority_queue<int> q;              // step: 1       for(int i=0; i<k; i++)       {           q.push(arr[i]);       }              // step: 2       for(int i=k; i<=r; i++)       {           if(q.top() > arr[i])           {               q.pop();               q.push(arr[i]);           }       }              // step: 3       int ans = q.top();       return ans;   }
+1
devanshukum7586 days ago
 priority_queue<int> maxh;       int n = r-l+1;       for(int i =0; i <k; i++){           maxh.push(arr[i]);       }       for( int i =k; i <n; i++){           if( arr[i] < maxh.top()){               maxh.pop();               maxh.push(arr[i]);           }       }       return maxh.top();   }
-12
prasantpoudel331 week ago
def kthSmallest(self,arr, l, r, k):
       
        arr.sort()

        return arr[k-1]
easy
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.