class Solution {
public:
    vector<int> num;
    int res;
    void find(int l, int r, int k) {
        int pivot = num[l];
        int rr = r;
        int ll = l;
        while (l < r) {
            while (l<r && num[r]>=pivot) {
                r--;
            }
            num[l] = num[r];
            while (l < r && num[l] <= pivot) {
                l++;
            }
            num[r] = num[l];
        }
        num[l] = pivot;
        if (l == k) {
            if (res == -1) res = num[l];
            return;
        }
        else if (l < k) {
            find(l+1, rr, k);
        }
        else {
            find(ll, l-1, k);
        }
    }
    int findKth(vector<int> a, int n, int K) {
        // write code here
        res = -1;
        a.insert(a.begin(), 1, 0);
        num=a;
        find(1, n, n-K+1);
        cout << res;
        return res;
    }
};