const sum = (arr) => {
return arr.reduce((acc, v) => acc + v, 0);
}
function solution(queue1, queue2) {
let sumQ1 = sum(queue1);
let sumQ2 = sum(queue2);
let pointer1 = 0;
let pointer2 = queue1.length;
const half = (sumQ1 + sumQ2) / 2;
const queue = [...queue1, ...queue2];
const end = queue1.length * 4;
for (let count = 0; count < end; count++) {
if (sumQ1 === half) {
return count;
}
if (sumQ1 > half) {
sumQ1 -= queue[pointer1++];
} else {
sumQ1 += queue[pointer2++];
}
}
return -1;
}