Split Array With Same Average Problem
Description
LeetCode Problem 805.
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.
Example 1:
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.
Example 2:
Input: nums = [3,1]
Output: false
Constraints:
- 1 <= nums.length <= 30
- 0 <= nums[i] <= 10^4
Solution
Approach: Meet in the Middle
Trying all 230 subsets is too slow, but 215 subsets of each half is fast. First turn the condition into "a subset sums to 0".
If A has the same average as the whole array, then B (the rest) does too, so we only need a non-empty, proper subset whose average is total / n. A subset of size k with sum s qualifies when s / k = total / n, i.e. s·n - k·total = 0. Replace every value x with x·n - total: a subset's new sum is exactly s·n - k·total, so we need a non-empty proper subset with new sum 0. Everything stays in integers, no floating-point comparisons.
- Split the new values into a left and a right half and list the sums of all subsets of each half.
- If a non-empty subset of one half sums to 0, the answer is true (the other half is not in it, so it is proper).
- Otherwise the subset takes something from both halves: put the sums of the left subsets in a hash set and, for each non-empty right subset with sum
r, look for-r.
The detail people get wrong: the whole array also sums to 0 after the shift, so it must not count. Leave the whole left half out of the hash set. No valid answer is lost: if the whole left half plus a smaller right part R sums to 0, the rest of the right half (non-empty) sums to 0 too, and step 2 already finds it.
Complexity: O(2n/2) time and space, where n is the length of nums.
class Solution:
def splitArraySameAverage(self, nums: List[int]) -> bool:
n = len(nums)
if n == 1:
return False
total = sum(nums)
# After this shift, a subset has the same average as the whole array exactly when its sum is 0.
values = [x * n - total for x in nums]
half = n // 2
left_sums = self.subset_sums(values[:half])
right_sums = self.subset_sums(values[half:])
# A non-empty subset of one half that sums to 0 works on its own.
if 0 in left_sums[1:] or 0 in right_sums[1:]:
return True
# Otherwise combine a left part and a right part. The left part must not be the
# whole left half: together with the whole right half that would be the whole array.
left_options = set(left_sums[1:-1])
for right_sum in right_sums[1:]:
if -right_sum in left_options:
return True
return False
def subset_sums(self, part: List[int]) -> List[int]:
"""Entry `mask` is the sum of the subset picked by the bits of mask (0 = empty, last = whole part)."""
sums = [0]
for value in part:
sums += [s + value for s in sums]
return sums
Now write it from memory
Close the write-up and solve it yourself against the tests.