Split Array With Same Average

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
Inputnums = [1,2,3,4,5,6,7,8]
Outputtrue
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
Inputnums = [3,1]
Outputfalse
There is no way to split [3, 1] into two non-empty arrays with the same average.

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate