related and inspired by -- Finding sum-free partitions
A set A
is defined here as being distinctly sum-free if
- 1) it consists of at least three elements,
|A| ≥ 3
, and - 2) its distinct self-sum
A + A = { x + y | x, y in A}
(withx,y
distinct, i.e.,x≠y
) has no elements in common withA
.
(Obsolete - do not use this going forward. Left here only because some answers may have used it. It does not match with the above conditions. Alternatively, the equation x + y = z
has no solution for x,y,z ∈ A
(again with x,y,z
distinct, i.e., x≠y
, x≠z
, y≠z
).)
For a simple example, {1,3,5}
is distinctly sum-free, but {1,3,4}
is not. {1,3}
and {3}
are also not, since they're not at least three elements.
The challenge here is to find the largest distinctly sum-free subset of the given input.
Input
- An unordered set
A
of integers in any convenient format. - The integers could be positive, negative, or zero, but can be assumed to fit in your language's native
[int]
datatype (or equivalent). - The set is guaranteed to have only distinct elements (no multisets here).
- The set is not necessarily sorted.
Output
- The largest subset of
A
(which could beA
itself), that is distinctly sum-free. Output can be in any suitable format. - If no such subset exists, output an empty set or other falsey value.
- If multiple subsets are tied for largest, output any or all of them.
- The subset does not necessarily need be sorted, or in the same order as the input. For example, for input
{1,3,5}
output{5,1,3}
is acceptable.
Additional Rules
- Standard loopholes are forbidden.
- This is code-golf, so all usual golfing rules apply, and the shortest code wins.
Examples
Input -> Output (any or all)
{0} -> {}
{1, 2, 3} -> {}
{1, 3, 5} -> {1, 3, 5}
{1, 2, 3, 4, 5} -> {1, 2, 5} {1, 2, 4} {1, 3, 5} {2, 3, 4} {2, 4, 5} {3, 4, 5}
{-5, 4, 3, -2, 0} -> {-5, 4, 3} {-5, 4, -2} {4, 3, -2}
{-5, 4, 3, -2} -> {-5, 4, 3} {-5, 4, -2} {4, 3, -2}
{-17, 22, -5, 13, 200, -1, 1, 9} -> {-17, 22, -5, 13, 200, -1, 1} {-17, 22, -5, 200, -1, 1, 9} {-17, -5, 13, 200, -1, 1, 9}