forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_888.java
33 lines (31 loc) · 862 Bytes
/
_888.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fishercoder.solutions;
import java.util.HashSet;
public class _888 {
public static class Solution1 {
public int[] fairCandySwap(int[] A, int[] B) {
int aSum = 0;
int bSum = 0;
int diff = 0;
int[] ans = new int[2];
for (int bar : A) {
aSum += bar;
}
for (int bar : B) {
bSum += bar;
}
diff = aSum - bSum;
HashSet<Integer> set = new HashSet<>();
for (int bar : A) {
set.add(bar);
}
for (int bar : B) {
if (set.contains(bar + diff / 2)) {
ans[0] = bar + diff / 2;
ans[1] = bar;
break;
}
}
return ans;
}
}
}