Permuting Two Arrays

Sort by

recency

|

268 Discussions

|

  • + 0 comments
    In Java:
    
        public static String twoArrays(int k, List<Integer> A, List<Integer> B) {
    
        // Ordering A in ascending order
       A.sort((a, b) -> Integer.compare(a, b));
       // Ordering B in descending order
       B.sort((a, b) -> Integer.compare(b, a));
    
       for(int i=0;i<A.size();i++){
        if(A.get(i) + B.get(i) < k){
        return "NO";
        }
    
       }
    

    return "YES";

    }
    
  • + 1 comment

    Koi samjhay ga ke is problem ka kia mtlb hai?

  • + 0 comments

    Kotlin Solution

    fun twoArrays(k: Int, A: Array<Int>, B: Array<Int>): String {
        A.sort()
        B.sortDescending()
        for(i in A.indices){
            if(A[i]+ B[i] < k){
                return "NO"
            }
        }
        return "YES"
    }
    
  • + 0 comments

    Python solution:

    def twoArrays(k, A, B):
        # Write your code here
        for a in A:
            low = k-a
            bs = [b for b in B if b >=low]
            if bs: #if bs not empty
                B.remove(min(bs))
            else: # if bs empty
                return 'NO'
        return 'YES'
    
  • + 0 comments

    simple very pythonic solution:

    def twoArrays(k, A, B):
        # Write your code here
        A.sort()
        B.sort(reverse=True)
        comprobations = [x + y >= k for x,y in zip(A, B)]
        return "YES" if all(comprobations) else "NO"