두 배열 A, B에서 원소 하나씩을 swap했을 때 각 배열의 원소 합이 같은 경우가 있으면 true, 없으면 false
이 또한 Codility의 O(n+m) 솔루션을 분석하여따....
const solution = (A, B) => {
let sumA = A.reduce((a, b) => a+b);
let sumB = B.reduce((a, b) => a+b);
let setA = new Set(A);
let d = sumA-sumB;
if(d%2) return false;
d /= 2;
for(let num of B){
if(num-d > 0 && setA.has(num-d)) return true;
}
return false;
}
중요한건 차를 구할 때 abs를 쓰지 않는것이당!
d의 값은 음수여도 가능 (A의 합이 B의 합보다 작을 경우를 위해)