-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem 888.java
30 lines (28 loc) · 920 Bytes
/
Problem 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
class Solution {
public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
Arrays.sort(bobSizes);
int aliceSum=0;
int bobSum=0;
for(int i=0;i<aliceSizes.length;i++){
aliceSum+=aliceSizes[i];
}
for(int i=0;i<bobSizes.length;i++){
bobSum+=bobSizes[i];
}
int [] ans=new int [2];
for(int i=0;i<aliceSizes.length;i++){
if((bobSum-aliceSum+(2*aliceSizes[i]))%2!=0){
break;
}
if(Arrays.binarySearch(bobSizes,(bobSum-aliceSum+(2*aliceSizes[i]))/2)>=0){
ans[0]=aliceSizes[i];
ans[1]=(bobSum-aliceSum+(2*aliceSizes[i]))/2;
}
else if(aliceSizes[i]==(bobSum-aliceSum+(2*aliceSizes[i]))/2){
ans[0]=aliceSizes[i];
ans[1]=aliceSizes[i];
}
}
return ans;
}
}