-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFairCandySwap.java
More file actions
35 lines (27 loc) · 848 Bytes
/
Copy pathFairCandySwap.java
File metadata and controls
35 lines (27 loc) · 848 Bytes
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
34
package LeetcodePractice;
import java.util.*;
public class FairCandySwap {
public static int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
int sumA = 0, sumB = 0;
for (int a : aliceSizes) sumA += a;
for (int b : bobSizes) sumB += b;
int delta = (sumB - sumA) / 2;
Set<Integer> bobSet = new HashSet<>();
for (int b : bobSizes) {
bobSet.add(b);
}
for (int a : aliceSizes) {
int b = a + delta;
if (bobSet.contains(b)) {
return new int[]{a, b};
}
}
return new int[0];
}
public static void main(String[] args) {
int[] alice = {1, 2};
int[] bob = {2, 3};
int[] result = fairCandySwap(alice, bob);
System.out.println(Arrays.toString(result));
}
}