> ## 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.

# Split Array With Same Average Python Solution

> Tested Python solution for LeetCode 805 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 805, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Bit Manipulation](/catalog/topics/bit-manipulation), [Bitmask](/catalog/topics/bitmask). [View on LeetCode](https://leetcode.com/problems/split-array-with-same-average/description/).

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

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

## Problem

You are given an integer array `nums`.

You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.

Return `true` if it is possible to achieve that and `false` otherwise.

**Note** that for an array `arr`, `average(arr)` is the sum of all the elements of `arr` over the length of `arr`.

### Examples

```
Input: nums = [1,2,3,4,5,6,7,8]
Output: true
Explanation: We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have an average of 4.5.
```

```
Input: nums = [3,1]
Output: false
```

### Constraints

* 1 \<= nums.length \<= 30
* 0 \<= nums\[i] \<= 10^4

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2 * total_sum) subset-sum over (size, sum) pairs
    # Space: O(n * total_sum)
    def split_array_same_average(self, nums: list[int]) -> bool:
        n = len(nums)
        total = sum(nums)
        if n == 1:
            return False
        # avg(A) == avg(B) implies avg(A) == avg(nums); check each size k
        # for a subset whose sum equals k * total / n
        sums_by_size: list[set[int]] = [set() for _ in range(n + 1)]
        sums_by_size[0].add(0)
        for num in nums:
            for k in range(n - 1, 0, -1):
                for s in sums_by_size[k - 1]:
                    sums_by_size[k].add(s + num)
        for k in range(1, n // 2 + 1):
            if total * k % n == 0 and total * k // n in sums_by_size[k]:
                return True
        return False
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 \* total\_sum) subset-sum over (size, sum) pairs | O(n \* total\_sum) |

## Tags

[NeetCode All](/catalog/neetcode).


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