Permuting Two Arrays

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