0
$\begingroup$

I would like to write some Mathematica code to find all the subsets of $\{1,2,\ldots,n\}$ subject to the constraint that there should be exactly one pair of consecutive integers in each subset. So for example for $n=4$ the subsets are $\{1,2\}, \{3,4\}, \{2,3\}, \{1,2,4\},\{1,3,4\}$. I need the actual list of all such subsets, not just how many there are. I looked at the options for the Subsets Mathematica function but I did not see anything that would be directly useful.

$\endgroup$
1
1
$\begingroup$
cos = Subsets[Range[4], {2, 4}]
sel = If[Count[#, 1] == 1, True, False] & /@ Differences /@ cos 
Pick[cos, sel]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.