Permuting Two Arrays

Sort by

recency

|

346 Discussions

|

  • + 0 comments

    Here is my Python solution!

    def twoArrays(k, A, B):
        B.sort()
        for num1 in A:
            working = False
            for num2 in B:
                if num1 + num2 >= k:
                    working = True
                    B.remove(num2)
                    break
            if not working:
                return "NO"
        return "YES"
    
  • + 0 comments

    Here is my c++ solution, you can watch the vidéo explanation here : https://youtu.be/JQQV9IZlz7g

    string twoArrays(int k, vector<int> A, vector<int> B) {
        sort(A.begin(), A.end());
        sort(B.begin(), B.end(), [](int l, int r){ return l > r;});
        for(int i = 0; i < A.size(); i++){
            if(A[i] + B[i] < k) return "NO";
        }
        return "YES";
    }
    
  • + 0 comments

    PYTHON

    def twoArrays(k, A, B):
        # Write your code here
        A.sort()
        B.sort(reverse=True)
        for (i,j) in zip(A,B):
            if ((i+j) < k):
                return "NO"
        return "YES"
    
  • + 0 comments
    string twoArrays(int k, vector<int> A, vector<int> B) {
        int c=0;
        for(int i=0;i<A.size();i++){
            if(A[i]/k>0){
                A[i] = k;
            }
            if(B[i]/k>0){
                B[i] = k;
            }
            int d = A[i]+B[i]+c;
            c = d - k;
        }
        if(c<0)return "NO";
        else return "YES";
    }
    
  • + 0 comments

    My Python 3 solution:

    def twoArrays(k, A, B):
        A.sort()
        B.sort(reverse=True)
        if any(a + b < k for (a, b) in zip(A, B)):
            return "NO"
        return "YES"
    

    Some coders use a for loop instead of any. Is it more or less efficient to use any?