Permuting Two Arrays

Sort by

recency

|

343 Discussions

|

  • + 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

    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?

  • + 0 comments

    Java

    public static String twoArrays(int k, List<Integer> A, List<Integer> B) {
    // Write your code here
        Collections.sort(A);
        Collections.sort(B,Collections.reverseOrder());
        for(int i=0;i<A.size();i++){
            if((A.get(i)+B.get(i)>=k)){
                continue;
            }else{
                return "NO";
            }  
        }
     return "YES";   
    }
    
  • + 0 comments

    Python:

    def twoArrays(k, A, B):
        if sum(A)+sum(B)<k*len(A):
            return "NO"
        
        sort_A=sorted(A)
        sort_B=sorted(B,reverse=True)
        
        for i in range(len(A)):
            if sort_A[i]+sort_B[i]<k:
                return 'NO'
                
        return 'YES'
    
  • + 0 comments

    Python Soln

    def twoArrays(k, A, B):
        A.sort()
        B.sort(reverse=True)
        
        for i in range(len(A)):
            if A[i] + B[i] < k:
                return "NO"
        return "YES"