Consider an array of unique integers, with an arbitrary length greater than 2. It is sometimes possible to express elements of the array as the sum of at least two other elements. For example, if our array is [2, 3, 1]
, we can express 3
as the sum 2+1
. However, we can't express either 2
or 1
as the sum of other elements.
Additionally, each integer in the list may only be used once in each sum. For example, with [1, 2, 5]
we can't express 5
as 2+2+1
(or 1+1+1+2
etc.) as we can only use each element once per sum.
Your program should take such array as input, via any convenient method, and output the elements of the input that are expressible as the sum of other elements. The output may be in any order, as may the input.
This is code-golf, so aim to make your code as short as possible, time / space complexity be damned.
Test cases
input -> output
[2, 3, 1] -> [3]
[8, 2, 1, 4] -> []
[7, 2, 1, 4] -> [7]
[7, 2, 1, 4, 6] -> [6, 7]
[0, 1, -1] -> [0]
[4, 2, -2, 0] -> [2, 0]
[0, 1, 2] -> []
Explanation for the last test case and result: For the purposes of this problem statement, zero cannot be considered the sum of a resulting empty list. Zero can only be in the resulting list IFF two or more other elements of the input list can be added to sum to it.
In other words, do not assume that if 0 is in the input, it should always be in the output - you cannot choose an empty subset to cover it. The problem statement explicitly states that any element in the resulting list must be the sum of other elements.
[0, 1, -1]
not[0, 1 -1]
? I can get0
from1 + -1
, I can get1
from0 + 1
, and I can get-1
from0 + -1
. \$\endgroup\$