I'm trying to generate all the subsets of a set containing a specific element.
For example, for the set {1, 2, 3, 4}, with the specific element I want inside each subset being 4, the required answer would be
{{4}, {1, 4}, {2, 4}, {3, 4}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}}
My current code is to do Select[Subsets[Range[n]], MemberQ[#, n] &]
, where for the above example we would have n=4
. However, the code seems inefficient and my computer runs out memory for n>25
. Is there a more efficient way to do so, or is this already the best?
NB: I will only need to work with one of the subsets at any point of time, but the Select
code above doesn't seem to allow me to pick an arbitrary $i^{th}$ subset.