## Printing all possible subsets of a list

generate all subsets of a list python

sum of all subsets of an array

write a python class to get all possible unique subsets from a set of distinct integers

sum of all subsets of size k

print all subsets with given sum

subsets of 0s

subsets (leetcode)

I have a List of elements (1, 2, 3), and I need to get the superset (powerset) of that list (without repeating elements). So basically I need to create a List of Lists that looks like:

{1} {2} {3} {1, 2} {1, 3} {2, 3} {1, 2, 3}

What is the best (simplicity > efficiency in this case, the list won't be huge) way to implement this? Preferably in Java, but a solution in any language would be useful.

Use bitmasks:

int allMasks = (1 << N); for (int i = 1; i < allMasks; i++) { for (int j = 0; j < N; j++) if ((i & (1 << j)) > 0) //The j-th element is used System.out.print((j + 1) + " "); System.out.println(); }

Here are all bitmasks:

1 = 001 = {1} 2 = 010 = {2} 3 = 011 = {1, 2} 4 = 100 = {3} 5 = 101 = {1, 3} 6 = 110 = {2, 3} 7 = 111 = {1, 2, 3}

You know in binary the first bit is the rightmost.

**Finding all subsets of a given set in Java,** Therefore, what we have to do is just generate the binary numbers from 0 to 2^n – 1, where n is the length of the set or the numbers of elements in the set. C++ program to print all possible subset of a set. // Here variable i will act as a binary counter for ( int i = 0 ; i < count ; i + + ) { // The inner for loop will run n times , As the maximum Output. Enter size of the set 4 Enter Elements of the set 1 2 3 4 1 2 1 2 3 1 3 2 3 1 2 3 4 1 4 2 4 1

import java.io.*; import java.util.*; class subsets { static String list[]; public static void process(int n) { int i,j,k; String s=""; displaySubset(s); for(i=0;i<n;i++) { for(j=0;j<n-i;j++) { k=j+i; for(int m=j;m<=k;m++) { s=s+m; } displaySubset(s); s=""; } } } public static void displaySubset(String s) { String set=""; for(int i=0;i<s.length();i++) { String m=""+s.charAt(i); int num=Integer.parseInt(m); if(i==s.length()-1) set=set+list[num]; else set=set+list[num]+","; } set="{"+set+"}"; System.out.println(set); } public static void main() { Scanner sc=new Scanner(System.in); System.out.println("Input ur list"); String slist=sc.nextLine(); int len=slist.length(); slist=slist.substring(1,len-1); StringTokenizer st=new StringTokenizer(slist,","); int n=st.countTokens(); list=new String[n]; for(int i=0;i<n;i++) { list[i]=st.nextToken(); } process(n); } }

**Printing all possible subsets of a list,** Use bitmasks: int allMasks = (1 << N); for (int i = 1; i < allMasks; i++) { for (int j = 0; j < N; j++) if ((i & (1 << j)) > 0) //The j-th element is used System.out.print((j + 1) + Print sums of all subsets of a given set; Print all possible expressions that evaluate to a target; Number of ways to calculate a target number using only array elements; Array of Vectors in C++ STL; Breadth First Search without using Queue; Must do Math for Competitive Programming; Reverse a singly Linked List in groups of given size | Set 3

A java solution based on Petar Minchev solution -

public static List<List<Integer>> getAllSubsets(List<Integer> input) { int allMasks = 1 << input.size(); List<List<Integer>> output = new ArrayList<List<Integer>>(); for(int i=0;i<allMasks;i++) { List<Integer> sub = new ArrayList<Integer>(); for(int j=0;j<input.size();j++) { if((i & (1 << j)) > 0) { sub.add(input.get(j)); } } output.add(sub); } return output; }

**Print All Subsets of a given set,** Given an array of distinct integers S, return all possible subsets. The set is not necessarily sorted and the total number of subsets of a given set Print all subsets of given size of a set; Total number of Subsets of size at most K; Python Program for Maximum size square sub-matrix with all 1s; Python Program for Median of two sorted arrays of same size; Python | Consecutive Subsets Minimum; Python - Maximum element in consecutive subsets; Python | Find the number of unique subsets with given sum in array

I've noticed that answers are focused on the String list. Consequently, I decided to share more generic answer. Hope it'll be fouund helpful. (Soultion is based on another solutions I found, I combined it to a generic algorithem.)

/** * metod returns all the sublists of a given list * the method assumes all object are different * no matter the type of the list (generics) * @param list the list to return all the sublist of * @param <T> * @return list of the different sublists that can be made from the list object */ public static <T> List<List<T>>getAllSubLists(List<T>list) { List<T>subList; List<List<T>>res = new ArrayList<>(); List<List<Integer>> indexes = allSubListIndexes(list.size()); for(List<Integer> subListIndexes:indexes) { subList=new ArrayList<>(); for(int index:subListIndexes) subList.add(list.get(index)); res.add(subList); } return res; } /** * method returns list of list of integers representing the indexes of all the sublists in a N size list * @param n the size of the list * @return list of list of integers of indexes of the sublist */ public static List<List<Integer>> allSubListIndexes(int n) { List<List<Integer>> res = new ArrayList<>(); int allMasks = (1 << n); for (int i = 1; i < allMasks; i++) { res.add(new ArrayList<>()); for (int j = 0; j < n; j++) if ((i & (1 << j)) > 0) res.get(i-1).add(j); } return res; }

**Print all subsets (power set) of a given set,** Print all subsets (power set) of a given set The power set contains every possible combination of numbers. combinations newlist = [[]] l = [1,2,3] for i in range(1,len(l)+1): c = combinations(l,i) for i in p: newlist.append(list(i)) print(newlist). Step 1: Run a loop till length of the given list. Step 2: Run a loop from i+1 to length of the list to get all the subarrays from i to its right. Step 3: Slice the subarray from i to j. Step 4: Append it to a another list to store it Step 5: Print it at the end. Below is the Python implementation of the above approach:

This is the simple function can be used to create a list of all the possible numbers generated by digits of all possible subsets of the given array or list.

void SubsetNumbers(int[] arr){ int len=arr.length; List<Integer> list=new ArrayList<Integer>(); List<Integer> list1=new ArrayList<Integer>(); for(int n:arr){ if(list.size()!=0){ for(int a:list){ list1.add(a*10+n); } list1.add(n); list.addAll(list1); list1.clear(); }else{ list.add(n); } } System.out.println(list.toString()); }

**Print All the Subsets of a Given Set (Power Set),** Objective: Given a set of numbers, print all the posssible subsets of it including empty set. Power Set: In mathematics, PowerSet of any given set S, PS(S) is set of Print all distinct Subsets of a given Set Given a set S, generate all distinct subsets of it i.e., find distinct power set of set S. A power set of any set S is the set of all subsets of S, including the empty set and S itself.

**Print all distinct Subsets of a given Set,** To print only distinct subsets, we initially sort the subset and exclude all adjacent duplicate elements from the subset along with the current element in case 2. C++ Sum of subsets of all the subsets of an array | O(3^N) Sum of subsets of all the subsets of an array | O(N) Print all subsets of given size of a set; Number of subsets with sum divisible by M | Set 2; Count of subsets with sum equal to X; Partition of a set into K subsets with equal sum; Perfect Sum Problem (Print all subsets with given sum)

**C++ program to print all possible subset of a set,** We can generate all possible subset using binary counter. For example: Consider a set 'A' having elements {a, b, c}. So we will generate binary Print all sub sequences of a given String; Print all sub sequences of a given array; Sum of length of subsets which contains given value K and all elements in subsets… Dynamic programming – Minimum Jumps to reach to end; Print all subsets of an array with a sum equal to zero; Top 40 Interview Problems on Arrays; Remove Duplicates from a string

**Given a set with 'n' elements, print subsets of all possible sizes,** We will iterate over the set elements and find out all possible subsets recursively with the current set element as the first element of the subsets. Once the iteration Problem: Find all the subsets of a given set. The total number of subsets of any given set is equal to 2^ (no. of elements in the set). Starting from right, 1 at ith position shows that the ith element of the set is present as 0 shows that the element is absent.

##### Comments

- You want all subsets of that list. I'd suggest recursion. However, if you are dealing with, say, more than 30-40 elements, you won't be able to deal with the HUGE (over 1TB of data) you have. What is this used for?
- This data structure you're looking for is called a Powerset (the diffence being that it also contains an empty set). It's already been discussed on SO.
- Thanks Zenzen for pointing me in the right direction...I found stackoverflow.com/questions/1670862/….
- Those are not permutations, those are subsets.
- This is very interesting...you're obviously a lot smarter than I am though - give me some time to wrap my mind around this....is N the number of elements in the original list? And am I mapping the objects in my list to integers?
- @Steve - Yes,
`N`

is the number of elements - in your example above`N = 3`

. Think in binary - the bit is 0 if the element is not used and the bit is 1 if the element is used. For example 5 = 101 in binary. This means`1`

and`3`

are used.`= {1, 3}`

- The program is simple. First, we try to get all possible combinations of 1-n digit numbers till the last digit of each list of 1,2,3,....n digit number is n. And then with each combination to extract each of its character(i.e. number) and display the element of the subset stored in the cell index denoted by this character(number).
- It would be better to add the description of the code in the answer itself than in comments. Answer with code only are not considered good answer by the community in general, even the code answers properly the question.