Permuting Two Arrays

Sort by

recency

|

350 Discussions

|

  • + 0 comments

    Working through array permutation problems can be tricky, especially when trying to match conditions across both arrays efficiently. I faced a similar challenge while organizing tasks in order, and planning step by step really helped me avoid confusion. This kind of logical thinking reminds me of the attention needed for Plumbing services, and ProsWay Plumbing & HVAC really shows how careful handling of details makes the process smoother. Paying attention to small details ensures everything works without unexpected issues.

  • + 0 comments

    Java:

    public static String twoArrays(int k, List<Integer> A, List<Integer> B) {
        A.sort(Comparator.naturalOrder());
        B.sort(Comparator.reverseOrder());
        for (int i=0; i < A.size(); ++i) {
            if (k > A.get(i) + B.get(i)) {
                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

    My Java solution with o(n log n) time complexity and o(1) space complexity:

    public static String twoArrays(int k, List<Integer> A, List<Integer> B) {
            // goal: order arrs a and b to where a[i] + b[i] >= k
            
            //order arr a ascending, order arr b descending
            Collections.sort(A);
            Collections.sort(B, Collections.reverseOrder());
            
            //check each val
            for(int i = 0; i < A.size(); i++){
                if(A.get(i) + B.get(i) < k) return "NO"; 
            }
            return "YES"; //all vals of A and B were > k when summed together
        }
    
  • + 0 comments

    C++ solution ==========>>>>>>>>>>>>>

    string twoArrays(int k, vector A, vector B) { if(A.size() != B.size()) { return "NO"; } int n = A.size(); sort(A.begin(), A.end()); sort(B.begin(), B.end(), greater());

    for(int i = 0; i < n; i++ ) {
        if(A[i] + B[i] < k) {
            return "NO";
        }
    }
    return "YES";
    

    }