Fair Candy Swap
Real-world version: Swapping One Crate Each to Even the LoadsThe statement here is our own: the same inputs, outputs and technique.
Two depots hold crates whose weights read mine and yours. The two depots carry different totals.
Swap exactly one of my crates for exactly one of yours so that afterwards both depots carry the same total. At least one such swap is possible.
Return the pair [a, b], where a is the weight of the crate leaving my depot and b the weight of the crate leaving yours. Where several swaps work, return the one whose a is smallest, and among those the one whose b is smallest.
Examples
Example 1
- Input
- mine = [35, 17, 4, 24, 10], yours = [63, 21]
- Output
- [24, 21]
My depot carries ninety and yours eighty-four, so the crate I give up has to be three heavier than the one I take. Handing over 24 for 21 leaves both depots at eighty-seven. My lighter crates, 4, 10 and 17, have no partner three lighter among yours.
Example 2
- Input
- mine = [2, 4, 6], yours = [2, 4, 2]
- Output
- [4, 2]
Mine total twelve and yours eight, so I hand over a crate two heavier than the one I take. Giving up 4 for 2 leaves both at ten. Giving up my 2 would need a crate of nothing to come back.
Example 3
- Input
- mine = [3, 7], yours = [1, 5]
- Output
- [3, 1]
Mine total ten and yours six, so again the outgoing crate is two heavier. Both swaps work here, 3 for 1 and 7 for 5, and the rule asks for the one whose outgoing crate is lighter.
Constraints
- 1 <= mine.length <= 10^4
- 1 <= yours.length <= 10^4
- 1 <= mine[i] <= 10^5
- 1 <= yours[i] <= 10^5
- The two depots carry different totals.
- At least one swap works.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def fair_candy_swap(mine: list[int], yours: list[int]) -> list[int]:public int[] fairCandySwap(int[] mine, int[] yours)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's