forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFair Candy Swap
27 lines (27 loc) · 876 Bytes
/
Fair Candy Swap
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
import java.util.*;
public class fair_candy {
public static void main(String[] args){
int[] aliceSizes = {1,1};
int[] bobSizes = {2,2};
System.out.println(Arrays.toString(fairCandySwap(aliceSizes, bobSizes)));
}
public static int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
int sb =0;
int sa = 0;
for(int i =0; i<aliceSizes.length ; i++){
sb = sb+aliceSizes[i];
}
for(int i =0; i<bobSizes.length ; i++){
sa = sa+bobSizes[i];
}
int diff = (sb - sa)/2;
for(int i = 0; i< aliceSizes.length ; i++){
for(int j= 0 ; j< bobSizes.length ; j++){
if(diff == aliceSizes[i] - bobSizes[j]){
return new int[]{aliceSizes[i], bobSizes[j]};
}
}
}
return null;
}
}