Largest Distinctly Sum-Free Partition
MATL, 47 43 bytes
nW:qB!ts2#Sw2>)PZ)"1G@)XJ2XN!"@sJ@X-m~*]?J.
Try it online!
Explanation
This uses two loops: an outer loop to generate all possible subsets, and an inner loop to take all pairs of elements and see if the sum equals any other element of the subset.
Subsets of at least 3 elements are tested in order of decreasing number of elements. The code stops as soon as a valid subset is found.
% Take input implicitly
nW:q % Generate [0 1 ... 2^n-1] where n is input length
B! % Convert to binary. Gives a matrix. Each number corresponds to a column.
% This will be used to select the elements of each subset
ts % Duplicate. Sum of each column
2#S % Sort. Output the sorted array and the indices of the sorting. Each index
% corresponds to one possible subset
w2> % Swap. Logical index of values that exceed 2. This is used to pick only
% subsets of more than 2 elements
) % Keeps only indices of subsets that have at least 3 elements
P % Flip. This moves subsets with more elements to the front. As soon as a
% subset fulfills the condition the outer loop will be exited, so we need
% to start with the bigger subsets
Z) % Index into columns of binary matrix. Each column is the binary pattern
% defining a subset with at least 3 elements, starting with bigger subsets
" % For each column. Each iteration corresponds to a subset
1 % Push 1
G@) % Pick actual elements of each subset (logical index into input)
XJ % Copy to clipboard J
2XN! % All pairs of 2 elements from that subset. Each pair is a column
" % For each column. Each iteration corresponds to a pair of elements
@s % Sum of those two elements
J@X- % Array with the other elements (obtained with set difference)
m~ % True if the sum of the two elemens is not a member of the array
* % Multiply. Corresponds to logical AND
] % End for
? % If result is true: no sum of two elements equalled any element
J % Push found subset (will be displayed implicitly)
. % Exit loop
% End if implicitly
% End for implicitly
% Display stack implicitly
Python, 137 bytes
A naïve approach. Loops over all subsets of the input containing at least 3 values, checking the property for each one. Returns []
when no result is found or [S]
if at least one result is found (where S
is some tuple).
from itertools import*
lambda a:[c for n in range(3,len(a)+1)for c in combinations(a,n)if all(x+y-z for(x,y,z)in permutations(c,3))][-1:]
Ruby, 107 bytes
Input is an array. Collects one qualifying subset for each subset size from 3
to the input length, then returns the biggest one out of the ones found. Returns nil
if no result is found.
Because of the conflicting spec, there are two solutions that currently have the same bytecount.
Using the first definition: ({ x + y | x, y ∈ A } ∩ A = ∅
)
->s{((3..s.size).map{|i|s.combination(i).find{|e|e.combination(2).map{|a,b|a+b}&e==[]}}-[p])[-1]}
Using the second definition (∀ x, y, z ∈ A: x + y ≠ z
)
->s{((3..s.size).map{|i|s.combination(i).find{|e|e.permutation(3).all?{|a,b,c|a+b!=c}}}-[p])[-1]}