8
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}
How does this work? I seem to get unexpected output when running it on TIO. – AdmBorkBork – 2016-05-24T13:33:45.993
Sorry, fixed it now. – Leaky Nun – 2016-05-24T13:47:07.870
1Doesn't work for the input
1,3,5,100
, since it also prints the subset1,3,5
which is not maximal. – Jakube – 2016-05-24T14:19:14.203A working (and much shorter) solution would be:
ef&ttT!@TsM.cT2y
– Jakube – 2016-05-24T14:22:35.3271@Jakube Drop the initial
e
and then post as a separate solution :D – Leaky Nun – 2016-05-24T15:19:55.9371We have to print the largest subset, or all the largest subsets. So the
e
is required. – Jakube – 2016-05-24T15:46:43.103This is still invalid, as Jakube stated. – AdmBorkBork – 2016-05-31T14:09:13.707