import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
class Solution {
public int solution(int[] queue1, int[] queue2) {
int answer = 0;
long sum1 = Arrays.stream(queue1).sum();
long sum2 = Arrays.stream(queue2).sum();
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> q2 = new LinkedList<>();
for (int num : queue1) {
q1.add(num);
}
for (int num : queue2) {
q2.add(num);
}
long sum = sum1 + sum2;
if (sum % 2 != 0)
return -1;
while (true) {
if (answer >= (queue1.length) * 4)
return -1;
if (sum1 > sum2) {
int element = q1.poll();
sum1 -= element;
sum2 += element;
q2.add(element);
answer++;
} else if (sum1 < sum2) {
int element = q2.poll();
sum1 += element;
sum2 -= element;
q1.add(element);
answer++;
} else {
break;
}
}
return answer;
}
}
Leave a comment