Fair Candy Swap - Problem

Alice and Bob are sharing their Halloween candy collections! They have different amounts of candy in total, but being good friends, they want to make it fair.

You're given two arrays:

  • aliceSizes[] - where aliceSizes[i] is the number of candies in Alice's i-th box
  • bobSizes[] - where bobSizes[j] is the number of candies in Bob's j-th box

Goal: Find exactly one box from Alice and one box from Bob to swap so that after the exchange, both friends have the same total number of candies.

Return: An array [alice_box_size, bob_box_size] representing the sizes of boxes they should exchange.

Note: It's guaranteed that at least one valid answer exists!

Input & Output

example_1.py โ€” Basic Case
$ Input: aliceSizes = [1, 2], bobSizes = [2, 3]
โ€บ Output: [1, 2]
๐Ÿ’ก Note: Alice has 1+2=3 candies, Bob has 2+3=5 candies. If Alice gives her box of 1 candy and receives Bob's box of 2 candies: Alice will have 3-1+2=4 candies, Bob will have 5-2+1=4 candies. Both have equal amounts!
example_2.py โ€” Larger Numbers
$ Input: aliceSizes = [1, 2, 5], bobSizes = [2, 4]
โ€บ Output: [5, 4]
๐Ÿ’ก Note: Alice has 1+2+5=8 candies, Bob has 2+4=6 candies. If Alice gives her box of 5 candies and receives Bob's box of 4 candies: Alice will have 8-5+4=7 candies, Bob will have 6-4+5=7 candies. Perfect balance!
example_3.py โ€” Multiple Solutions
$ Input: aliceSizes = [2, 2], bobSizes = [1, 1]
โ€บ Output: [2, 1]
๐Ÿ’ก Note: Alice has 4 candies, Bob has 2 candies. Any exchange of Alice's 2-candy box for Bob's 1-candy box results in both having 3 candies. Multiple valid answers exist, any one is acceptable.

Constraints

  • 1 โ‰ค aliceSizes.length, bobSizes.length โ‰ค 104
  • 1 โ‰ค aliceSizes[i], bobSizes[i] โ‰ค 105
  • Alice and Bob have different total amounts of candy
  • There will always be at least one valid answer

Visualization

Tap to expand
Fair Candy Swap: Balancing the ScaleAlice15 candies[1, 4, 10]-4Bob9 candies[1, 5, 3]+1Mathematical Solution1. Calculate difference: (15 - 9) รท 2 = 32. Alice gives A, receives B: A - B = 33. For Alice's box = 4: need Bob's box = 4 - 3 = 14. Check: Bob has box of size 1 โœ“Final: Alice 12 โ†โ†’ 12 Bob (Balanced!)
Understanding the Visualization
1
Identify the Imbalance
Alice has 15 candies, Bob has 9 candies. Total difference is 6 candies.
2
Calculate Required Exchange
To balance, Alice needs to give 3 more than she receives: diff = (15-9)/2 = 3
3
Find the Perfect Match
If Alice gives 4 candies, she needs to receive 4-3=1 candy from Bob
4
Verify Balance
After swap: Alice = 15-4+1 = 12, Bob = 9-1+4 = 12 โœ… Perfect balance!
Key Takeaway
๐ŸŽฏ Key Insight: The mathematical relationship `needed_bob_box = alice_box - (alice_total - bob_total)/2` directly gives us the required exchange partner, making hash table lookup the optimal O(n+m) solution!
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 23
89.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen