Permuting Two Arrays

Sort by

recency

|

134 Discussions

|

  • + 0 comments

    Python 3 solutions:

    import functools
    import typing
    
    
    def twoArrays(k: int, A: list[int], B: list[int]) -> typing.Literal["NO", "YES"]:
        A.sort()
        B.sort(reverse=True)
        return "NO" if any(a + b < k for a, b in zip(A, B, strict=True)) else "YES"
    
    
    def twoArrays(k: int, A: list[int], B: list[int]) -> typing.Literal["NO", "YES"]:
        @functools.lru_cache
        def cached_helper(ab: tuple[int, int]) -> bool:
            return sum(ab) < k
    
        A.sort()
        B.sort(reverse=True)
        return "NO" if any(map(cached_helper, zip(A, B, strict=True))) else "YES"
    
    
    
    # 88
    s=sorted;twoArrays=lambda k,A,B:["YES","NO"][any(a+b<k for a,b in zip(s(A),s(B)[::-1]))]
    
  • + 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"
    
  • + 0 comments

    Rust solution:

    fn twoArrays(k: i32, A: &mut [i32], B: &mut [i32]) -> String {
        A.sort();
        B.sort();
        
        for (a, b) in A.iter().zip(B.iter().rev()) {
            if a + b < k {
                return "NO".to_string();
            }
        }
        
        "YES".to_string()
    }
    
  • + 0 comments

    Anyone getting a

    Solution.go:10:5: package slices is not in GOROOT (/usr/local/go/src/slices)

    while trying to solve in Go?

    I'm working with 1.22.1 support for but slices should be there since 1.8, as far as i understand

  • + 0 comments

    Hey folks, I solved this problem by sorting the arrays A and B, ascending and descending, then checking the addition of those values by index with k. Please help me with some other alternative solutions, if any. Here is my code in javascript,

    function sorting(order, arr) {
        return arr.sort((a, b) => order === 'ASC' ? a - b : b - a);
    }
    
    function twoArrays(k, A, B) {
        let sA = sorting('ASC', A);
        let sB = sorting('DES', B);
        let count = 0;
        for (let i = 0; i < sA.length; i++) {
            sA[i] + sB[i] >= k && count++;
        }
        return count !== sA.length ? 'NO' : 'YES';
    }