Permuting Two Arrays

Sort by

recency

|

140 Discussions

|

  • + 0 comments

    JS Code

    function twoArrays(k, A, B) { A = A.sort((a,b)=>a-b); B = B.sort((a,b)=>b-a); console.log('Inputs'+k, A,B); for(let i=0; i < A.length; i++){ if(A[i] + B[i] < k){ return 'NO'; } } return 'YES';

    }

  • + 0 comments
    import java.io.*;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.stream.IntStream;
    
    public class TwoArray {
    
        public static void main(String[] args) throws IOException {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
                short q = Short.parseShort(br.readLine().trim());
                while (q-- > 0) {
                    String[] nkInput = br.readLine().trim().split(" ");
                    short n = Short.parseShort(nkInput[0]);
                    int k = Integer.parseInt(nkInput[1]);
                    int[] a = Arrays.stream(br.readLine().trim().split(" ")).
                            mapToInt(Integer::parseInt).sorted().toArray();
                    int[] b = Arrays.stream(br.readLine().trim().split(" ")).
                            mapToInt(Integer::parseInt).boxed().sorted(Comparator.reverseOrder()).mapToInt(Integer::intValue).toArray();
                    bw.write(compare(a, b, n, k));
                    bw.newLine();
                }
    
            }
        }
    
        private static String compare(int[] a, int[] b, short n, int k) {
            return IntStream.range(0, n)
                    .allMatch(i -> a[i] + b[i] >= k) ? "YES" : "NO";
    
        }
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def permuting_two_arrays(k, A, B):
        A = sorted(A)
        B = sorted(B, reverse=True)
    
        for index in range(0,len(A)):
            if (A[index] + B[index]) < k:
                return "NO"
    
        return "YES"
    
  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    pub fn permuting_two_arrays(k: i32, A: &[i32], B: &[i32]) -> String {
        //Time complexity: O(n*log(n))
        //Space complexity (ignoring input): O(n)
        let mut A = A.to_vec();
        A.sort_unstable();
        let mut B = B.to_vec();
        B.sort_unstable_by(|x, y| y.cmp(x));
        for (index, a_value) in A.iter().enumerate() {
            let b_value = B[index];
            if a_value + b_value < k {
                return "NO".to_string();
            }
        }
    
        "YES".to_string()
    }
    
  • + 0 comments
    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"