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
 
0
maneshram20in 7 hours
BEST OPTIMAL C++ SOLUTION QUICK SORT IMPLEMENTATION TIME : O(N) , SPACE : O(1)
class Solution{
    int partition(int *arr,int low,int high){
        int pivot = arr[high];
        int i = low-1;
        for(int j=low;j<high;j++){
            if(arr[j]<pivot){
                i++;
                swap(arr[i],arr[j]); 
            }
        }
        swap(arr[i+1],arr[high]);
        return (i+1);
    }
    public:
    int kthSmallest(int arr[], int l, int r, int k) {
        int ans;
        while(l<=r){
            int p = partition(arr,l,r);
            if(p==k-1){
                ans = arr[p];
                break;
            }
            else if(p>k-1) r = p-1;
            else l = p+1;
        }
        return ans;
    }
};
0
dasilashubham39in 5 hours
Java Code-→
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<arr.length;i++){          
           if(arr[i]<pq.peek()){             
               pq.remove();
               pq.add(arr[i]);
           }                
       }
       return pq.peek();
   }
0
himanshushukla3112in 5 hours
priority_queue<int, vector<int>, greater<int>> pq;
        int i=0;
        while(i<=r) {
            pq.push(arr[i]);
            i++;
        }
        
        while(k>1) {
         pq.pop();  
         k--;
        }
        
        return pq.top();
0
rajanish8121 hour ago
class Solution{   public:   // arr : given array   // l : starting index of the array i.e 0   // r : ending index of the array i.e size-1   // k : find kth smallest element and return using this function   int kthSmallest(int arr[], int l, int r, int k) {       //code here       sort(arr,arr+r+1);       int n = arr[k-1];       return n; 
0
ujjwaljaiswal909020 hours ago
int kthSmallest(int arr[], int l, int r, int k) {       //code here       int n=r+1;       sort(arr,arr + n);       return arr[k-1];   }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.