Fair Candy Swap Lab
In this lab, you will be solving a problem related to Alice and Bob's fair candy swap. The main objective is to implement a function fairCandySwap
that takes two integer arrays, aliceSizes
and bobSizes
, representing the number of candies in each box that Alice and Bob have, respectively. Your task is to find an integer array answer
where answer[0]
corresponds to the number of candies in the box that Alice must exchange, and answer[1]
denotes the number of candies in the box that Bob must exchange. After the exchange, both Alice and Bob should have the same total amount of candy. You can assume that at least one valid answer exists for the given input.
Here's a more detailed description of the problem:
- Alice and Bob have a different total number of candies.
- You are given two integer arrays:
aliceSizes
is an array wherealiceSizes[i]
is the number of candies of theith
box of candy that Alice has.bobSizes
is an array wherebobSizes[j]
is the number of candies of thejth
box of candy that Bob has.
- They want 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.
- Find an integer array
answer
whereanswer[0]
is the number of candies in the box that Alice must exchange, andanswer[1]
is the number of candies in the box that Bob must exchange. If multiple answers exist, you can return any one of them.
Example:
Input: aliceSizes = [1, 1], bobSizes = [2, 2]
Output: [1, 2]
Input: aliceSizes = [1, 2], bobSizes = [2, 3]
Output: [1, 2] or [2, 3]
Input: aliceSizes = [2], bobSizes = [1, 3]
Output: [2, 3]
Constraints:
- 1 <= aliceSizes.length, bobSizes.length <= 104
- 1 <= aliceSizes[i], bobSizes[j] <= 105
- Alice and Bob have a different total number of candies.
- There will be at least one valid answer for the given input.
Challenges
- Implement the
fairCandySwap
function that takesaliceSizes
andbobSizes
as parameters, and returns theanswer
array. - Export the
fairCandySwap
function using ESM syntax.