To check whether a list of non-negative integers is balanced, one can imagine putting respective weights on a board and then try to balance the board on a pivot such that the summarized relative weights left and right of the pivot are the same. The relative weight is given by multiplying the weight with its distance to the pivot (see law of the lever).
(Source: wikipedia)
This image corresponds to a list [100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]
. This list is balanced because the 5
has a distance of 20 to the pivot, the 100
a distance of 1 and 5*20 = 100 = 100*1
.
Examples
3 1 5 7
#########
^
In this case the pivot is directly under the 5
, the 3
has distance 2 and the 1
and 7
have distance 1. So both sides left and right of the pivot sum up to 7
(3*2 + 1*1
on the left and 7*1
on the right) and therefore the list [3, 1, 5, 7]
is balanced.
Note, however, that the pivot does not have to be placed under one of the list elements, but might also be placed in-between two list elements:
6 3 1
#######
^
In this case the distances become 0.5, 1.5, 2.5, ...
and so on. This list is also balanced because 6*0.5 = 3 = 3*0.5 + 1*1.5
.
The pivot can only be placed exactly below one number or exactly in the middle between two numbers, and not e.g. at two-thirds between two numbers.
Task
Given a list of non-negative integers in any reasonable format, output a truthy
value if the list can be balanced and a falsy
value otherwise.
You can assume that the input list contains at least two elements and that at least one element is non-zero.
This is a code-golf challenge, so the answer with the fewest amount of bytes in each language wins.
Truthy Testcases
[1, 0]
[3, 1, 5, 7]
[6, 3, 1]
[100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]
[10, 4, 3, 0, 2, 0, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[7, 7, 7, 7]
Falsy Testcases
[1, 2]
[3, 6, 5, 1, 12]
[0, 0, 2, 0, 1, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[6, 3, 2, 4, 0, 1, 2, 3]
[4, 0, 0, 2, 3, 5, 2, 0, 1, 2, 3, 0, 0, 1, 2, 4, 3, 1, 3, 0, 0, 2]
[100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]
A lot of related challenges where found while this challenge was sand-boxed: Is it a balanced number?, Equilibrium index of a sequence, Balance a set of weights on a seesaw, Balancing Words, Will I tip over? and Where does the pivot belong?
You can assume that the input list contains at least two elements and that at least one element is non-zero.
\$\endgroup\$