Permuting Two Arrays

  • + 0 comments

    C++ sort either A or B and sort in reverse order in case of the other one. By doing so, we guarantee that the added pair were optimized.

    string twoArrays(int k, vector A, vector B) {

    sort(A.begin(), A.end(), greater<int>());
    sort(B.begin(), B.end());
    
    int n = A.size();
    
    for(int i = 0; i < n; ++i) {
        if(k > A[i] + B[i]) {
            return "NO";
        }
    }
    return "YES";
    

    }