Show problems as
Classic problem #805HardArrayHash TableMathDynamic ProgrammingBit ManipulationMeet in the MiddleBitmask

Split Array With Same Average

Real-world version: Splitting the Tray Into Two Equal Averages
Tracked in this browser only
Read Split Array With Same Average on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tray holds the weights weights. Put every weight into one of two bins, and neither bin may end up empty.

Return true when some way of doing that leaves the two bins with the same average weight.

Examples

Example 1

Input
weights = [1, 2, 3]
Output
true

Putting the 2 in one bin and the 1 and the 3 in the other gives both bins an average of 2.

Example 2

Input
weights = [1, 2, 4]
Output
false

The tray totals 7 across three weights. A bin of one weight would have to hold seven thirds and a bin of two would have to total fourteen thirds, and neither is whole.

Example 3

Input
weights = [5, 5, 5, 5, 5]
Output
true

Every weight is the same, so any split at all leaves both bins on that average.

Constraints

  • 1 <= weights.length <= 30
  • 0 <= weights[i] <= 10^4

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.

Python
def split_array_same_average(weights: list[int]) -> bool:
Java
public boolean splitArraySameAverage(int[] weights)