> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Fair Candy Swap Python Solution with Tests

> Tested Python solution for LeetCode 888 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 888, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/fair-candy-swap/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 888   # by problem number
lcpy gen -s fair_candy_swap   # by problem name
```

## Problem

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 `i`th box of candy that Alice has and `bobSizes[j]` is the number of candies of the `j`th box of candy that Bob has.

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.

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.

### Examples

```
Input: aliceSizes = [1,1], bobSizes = [2,2]
Output: [1,2]
```

```
Input: aliceSizes = [1,2], bobSizes = [2,3]
Output: [1,2]
```

```
Input: aliceSizes = [2], bobSizes = [1,3]
Output: [2,3]
```

### Constraints

* `1 <= aliceSizes.length, bobSizes.length <= 10^4`
* `1 <= aliceSizes[i], bobSizes[j] <= 10^5`
* Alice and Bob have a different total number of candies.
* There will be at least one valid answer for the given input.

**Follow up:** Can you solve it in `O(n)` time complexity?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/fair_candy_swap/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/fair_candy_swap/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m)
    # Space: O(m)
    def fair_candy_swap(self, alice_sizes: list[int], bob_sizes: list[int]) -> list[int]:
        delta = (sum(alice_sizes) - sum(bob_sizes)) // 2
        bob_set = set(bob_sizes)
        for x in alice_sizes:
            y = x - delta
            if y in bob_set:
                return [x, y]
        return []  # unreachable: a valid answer is guaranteed
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) | O(m) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.