## Print all subsets of a set

print all subsets of an array using recursion

print all subsets of a string

print all subsets of given size of a set

find all distinct subsets of a given set

all subsets of a set python

print all subsets of an array using recursion in java

subsets (leetcode)

I need to write a **recursive** function that receives a number and print all subgroups from 1 to n.
I don't care about the order.

For example if number=3 I need the output:

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

but my code gives:

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

Here is my code, I call the method `subGroups(3);`

public static void subGroups(int num) { int[] ar = new int[num]; subGroups(ar, 1, num); } private static int[] insertToArr(int num, int[] arr) { if (num == 0) return arr; arr[num - 1] = num; return insertToArr(num - 1, arr); } private static void subGroups(int[] arr, int start, int end) { if (end <= 0 || start > arr.length) return; else if (start > end) subGroups(arr, start, end - 1); else { if (start != 0) { System.out.print("{"); printAll(start, end); System.out.println("}"); } subGroups(arr, start + 1, end); } } // prints all in in one line recursive private static void printAll(int start, int end) { if (start < end) { System.out.print(start + ","); printAll(start + 1, end); } else { System.out.print(start); } }

How do I get the requested result?

This should work, see how this may be ported to arrays.

import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.IntStream; import static java.util.Collections.emptySet; import static java.util.stream.Collectors.toList; public class Main { public static void powerset(int n) { System.out.println( powerset(IntStream.rangeClosed(1, n).boxed().collect(toList())) ); } public static Set<Set<Integer>> powerset(List<Integer> set) { if (set.isEmpty()) { Set<Set<Integer>> result = new HashSet(); result.add(emptySet()); return result ; } else { Integer element = set.remove(0); Set<Set<Integer>> pSetN_1 = powerset(set); Set<Set<Integer>> pSet_N= new HashSet(); pSet_N.addAll(pSetN_1); for (Set<Integer> s : pSetN_1) { Set<Integer> ss = new HashSet(s); ss.add(element); pSet_N.add(ss); } return pSet_N; } } public static void main(String[] args) throws Exception { powerset(3); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] } }

By the way, you are calculating a power set, a set of all subsets (not groups) of a set

**Finding all subsets of a given set in Java,** Finding all subsets of a given set in Java. Last Updated: 31-01-2020. Problem: Find all the subsets of a given set. Input: S = {a, b, c, d} Output: {}, {a} , {b}, {c}, {d}, � In this tutorial, we will learn how to print all the possible subsets of a set in C++. Now, before moving to the problem which is to print all the possible subsets of a set in C++. Let us understand it with an example, where there were 3 sets {0,1,2} (which means n=3).

If `(start > end)`

is true, then it will be true of the recursive call `subGroups(arr, start, end-1)`

as well, and the recursion will continue along this path until `end`

is too small.

So something here needs to change. Consider that nowhere do you ever reset either `start`

or `end`

.

**Backtracking to find all subsets,** Given a set of positive integers, find all its subsets. of the array, then print the subset or output array or insert the output array into the vector of� Printing all subsets of {1,2,3,n} without using array or loop; Partition of a set into K subsets with equal sum using BitMask and DP; Count of subsets of integers from 1 to N having no adjacent elements; Count number of ways to partition a set into k subsets; Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1

A few hints, and a code skeleton below: `arr`

should be used as an accumulator for the subset being found. It has initial size=0, and the size is incremented whenever an element is added to the current subset.

The initial call should be:

int[] arr = new int[0]; subGroups(arr, 1, num);

since at this point, you have selected 0 elements and you still need to select a subset of elements `{1,...,num}`

.

The function `subGroups`

should be:

/* * generates all subsets of set {start,...,end} , union "arr" */ private static void subGroups(int[] arr, int start, int end) { //terminal condition: print the array if (start>end) { print(arr); } else { //here we have 2 cases: either element "start" is part of the subset, or it isn't // case 1: "start" is not part of the subset // so "arr" is unchanged subGroups(arr, start+1, end); // case 2: "start" is part of the subset // TODO: create another array, containing all elements of "arr", and "start" int[] arrWithStart=copyAndInsert(arr, start); subGroups(arrWithStart, start+1, end); } } private static void print(int[] arr) { // TODO code here } /* * copy and insert: * it should create a new array, with length=arr.length+1, * containing all elements of array "arr", and also "element" */ static int[] copyAndInsert(int[] arr, int element) { // TODO code here }

**Print All Subsets of a given set,** Here we are generating every subset using recursion. The total number of subsets of a given set of size n = 2^n. The total number of possible subset a set can have is 2^n, where n is the number of elements in the 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 number upto 2^n - 1 (as we will include 0 also).

**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� 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 all subsets of S including empty set.

**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++ � 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.

**Facebook Coding Interview Question and Answer #1: All Subsets of ,** Find and print all subsets of a given set! (Given as an array.) Is there any other interview Duration: 10:58
Posted: Oct 2, 2017 Printing all possible subsets of a list it also contains an empty set). It's already been discussed on SO. of all the possible numbers generated by digits of

##### Comments

- You haven't asked a question.
- Please explain the problem with the OP's code and especially your answer. Else it is not helpful to future visitors.