7 Line Solution using nth_element


#1
vector<int> Solution::solve(vector<int> &A, int B) {
    int n=A.size();
    B = n-B;
    nth_element(A.begin(),A.begin()+B,A.end());
    vector<int> res;
    for(int i=B;i<n;i++)
        res.push_back(A[i]);
    return res;
}

#2

You can do it in just 2 lines.

vector<int> Solution::solve(vector<int>&A, int K) {
	nth_element(A.begin(), A.begin() + K, A.end(), greater<int>());
	return vector<int>(A.begin(), A.begin() + K);
}