forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
50 lines (43 loc) · 1.37 KB
/
Solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Problem: https://www.hackerrank.com/challenges/two-arrays
//JavaScript
/*
Initial Thoughts:
We can sort array A ascending and sort array B descending,
and then for every i we check if the condition A[i] + B[i] >= k
holds true (or not) for each index i. If the condition fails
on the sorted arrays, then there exists no permutation of
A and B satisfying the inequality.
Time Complexity: O(n log(n)) //We must sort the input arrays
Space Complexity: O(n) //We must store the input arrays
*/
function processData(input) {
let q = input[0], setup, n, k, a, b;
for(let i = 0; i < q; i++) {
setup = input[i * 3 + 1].split(' ').map(Number);
n = setup[0];
k = setup[1];
a = input[i * 3 + 2].split(' ').map(Number);
b = input[i * 3 + 3].split(' ').map(Number);
console.log(Permutable(a,b, n, k) ? "YES" : "NO");
}
}
function Permutable(a,b, n, k) {
a.sort((a,b) => a - b);
b.sort((a,b) => b - a);
for (let i = 0; i < n; i++) {
if (a[i] + b[i] < k) {
return false;
}
}
return true;
}
/////////////// ignore below this line ////////////////////
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input.split("\n"));
});