import java.util.*;

public class Solution {
    public int findKth(int[] a, int n, int K) {
        // write code here
        return findKth_(a,0,n-1,K);
    }
    public static int findKth_(int[] arry,int low,int hei,int k){
        /*if (hei-low<1){
            return;
        }*/
        int midValue = arry[low];
        int start = low;
        int end = hei;
        while (end>start){
            while (end>start&&midValue<arry[end]){
                end--;
            }
            if (start<end&&midValue>arry[end]){
                arry[start++]=arry[end];
            }
            while (start<end&&midValue>arry[start]){
                start++;
            }
            if (start<end){
                arry[end--]=arry[start];
            }

        }
        arry[end]=midValue;
       if (hei-end==k-1){
            return arry[hei-k+1];
        }else if(hei-end<k-1){
            return findKth_(arry,low,start-1,k-hei+start-1);
        }else{
            return findKth_(arry,start+1,hei,k);
        }
    }
}