Untitled
Anonymous
plain_text
02/25/2023 5:28 PM
884 B
14
Indexable
class Solution
{
long findSwapValues(long A[], int n, long B[], int m)
{
long sumA = 0, sumB = 0;
for (int i = 0; i < n; i++) {
sumA += A[i];
}
for (int i = 0; i < m; i++) {
sumB += B[i];
}
if ((sumA - sumB) % 2 != 0) {
return -1;
}
long target = (sumA - sumB) / 2;
Set<Long> set = new HashSet<>();
for (int i = 0; i < n; i++) {
set.add(A[i]);
}
for (int i = 0; i < m; i++) {
if (set.contains(B[i] + target)) {
return 1;
}
}
return -1;
}
}
Editor is loading...