Permuting Two Arrays

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