|
| 1 | +--- |
| 2 | +id: fair-candy-swap |
| 3 | +title: Fair Candy Swap |
| 4 | +sidebar_label: 888- Fair Candy Swap |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Hash Table |
| 8 | + - Sorting |
| 9 | +description: Find one pair of candy boxes, one from Alice and one from Bob, to swap so they both end up with the same total number of candies. |
| 10 | +sidebar_position: 0888 |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and bobSizes where `aliceSizes[i]` is the number of candies of the ith box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has. |
| 16 | + |
| 17 | +Since they are friends, they would like to exchange one candy box each so that after the exchange, they both have the same total amount of candy. The total amount of candy a person has is the sum of the number of candies in each box they have. |
| 18 | + |
| 19 | +Return an integer array answer where `answer[0]` is the number of candies in the box that Alice must exchange, and `answer[1]` is the number of candies in the box that Bob must exchange. If there are multiple answers, you may return any one of them. It is guaranteed that at least one answer exists. |
| 20 | + |
| 21 | +### Example 1 |
| 22 | + |
| 23 | +- **Input:** `aliceSizes = [1,1], bobSizes = [2,2]` |
| 24 | +- **Output:** `[1,2]` |
| 25 | + |
| 26 | + |
| 27 | +### Constraints |
| 28 | + |
| 29 | +- `1 <= aliceSizes.length, bobSizes.length <= 104` |
| 30 | +- `1 <= aliceSizes[i], bobSizes[j] <= 105` |
| 31 | + |
| 32 | +## Approach |
| 33 | + |
| 34 | +The solution first calculates the total number of candies each Alice and Bob have. Then, it sorts Bob's candy boxes for efficient searching. It iterates over Alice's candy boxes, checking if swapping a box with Bob can balance their total candies. For each box of Alice, it calculates the required box from Bob and uses binary search to find it in Bob's sorted array. If a valid swap is found, it updates the answer and returns it. |
| 35 | + |
| 36 | +#### Java |
| 37 | +```Java |
| 38 | +class Solution { |
| 39 | + public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) { |
| 40 | + Arrays.sort(bobSizes); |
| 41 | + int aliceSum=0; |
| 42 | + int bobSum=0; |
| 43 | + for(int i=0;i<aliceSizes.length;i++){ |
| 44 | + aliceSum+=aliceSizes[i]; |
| 45 | + } |
| 46 | + for(int i=0;i<bobSizes.length;i++){ |
| 47 | + bobSum+=bobSizes[i]; |
| 48 | + } |
| 49 | + int [] ans=new int [2]; |
| 50 | + |
| 51 | + for(int i=0;i<aliceSizes.length;i++){ |
| 52 | + if((bobSum-aliceSum+(2*aliceSizes[i]))%2!=0){ |
| 53 | + break; |
| 54 | + } |
| 55 | + if(Arrays.binarySearch(bobSizes,(bobSum-aliceSum+(2*aliceSizes[i]))/2)>=0){ |
| 56 | + ans[0]=aliceSizes[i]; |
| 57 | + ans[1]=(bobSum-aliceSum+(2*aliceSizes[i]))/2; |
| 58 | + } |
| 59 | + else if(aliceSizes[i]==(bobSum-aliceSum+(2*aliceSizes[i]))/2){ |
| 60 | + ans[0]=aliceSizes[i]; |
| 61 | + ans[1]=aliceSizes[i]; |
| 62 | + } |
| 63 | + } |
| 64 | + return ans; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +- Time Complexity |
| 70 | +The time complexity is $o(n)$. |
| 71 | + |
| 72 | +- Space Complexity |
| 73 | +The space complexity is $O(1)$. |
0 commit comments