All problems
1018EasyArrayHash TableBinary SearchSorting

Swapping One Crate Each to Even the Loads

Tracked in this browser only
Write code

Trains the technique from

LeetCode 888Fair Candy Swap

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def fair_candy_swap(mine: list[int], yours: list[int]) -> list[int]:
Java
public int[] fairCandySwap(int[] mine, int[] yours)
September 7
Apply