Finding subsets with a certain constraint with Mathematica

32 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER
cos = Subsets[Range[4], {2, 4}]
sel = If[Count[#, 1] == 1, True, False] & /@ Differences /@ cos 
Pick[cos, sel]