image_2021-12-09_02-41-10.png
41.7 KB
#N888. Fair Candy Swap
problem link
#solution
problem link
#solution
class Solution {
public int[] fairCandySwap(int[] a, int[] b) {
int sumA = 0, sumB = 0;
for(int n: a) sumA+=n;
for(int n: b) sumB+=n;
int delta = (sumB - sumA) / 2;
Set<Integer> set = new HashSet<>();
for(int n: b) set.add(n);
for(int n: a)
if(set.contains(n+delta)) return new int[]{n, n+delta};
return new int[0];
}
}