Permuting Two Arrays

Sort by

recency

|

255 Discussions

|

  • + 0 comments

    Scala

     def twoArrays(k: Int, A: Array[Int], B: Array[Int]): String = {
            val arraySize = A.length
            val sortedA = A.sorted
            val sortedB = B.sorted.reverse
           
           for(i <- (0 until arraySize).toArray)    
               if(sortedA(i)+sortedB(i) < k ) return "NO"
           
            return "YES"
        }
    
  • + 0 comments

    Short python solution :

    def twoArrays(k, A, B):
        s_A = sorted(A)
        r_B = sorted(B, reverse=True)
        for i in range(len(s_A)):  # since len(s_A) = len(A) = len(B)
            if s_A[i] + r_B[i] < k:
                return "NO"
        return "YES"
    
  • + 0 comments

    in c#

    public static string twoArrays(int k, List<int> A, List<int> B)
    {
        var minValueInA = A.Order().FirstOrDefault();
        var minValueInACount = A.Count(x => x == minValueInA);
    
        var requiredNum = k - minValueInA;
    
        var numCountInB = B.Count(x => x >= requiredNum);
        if (minValueInACount > numCountInB) {
            return "NO";
        }
        return "YES";
    }
    
  • + 0 comments

    in JS:

    function twoArrays(k, A, B) {
        const n = A.length;
        const sortedA = A.sort((a,b) => a - b);
        const sortedB = B.sort((a,b) => b - a);
        for (let i = 0; i < n; i++) {
            if (!(sortedA[i] + sortedB[i] >= k)){
                return 'NO'
            }
        }
        return 'YES';
    }
    
  • + 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";
    

    }