Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to generate all subsets of a given set in Java?

import java.util.List;
import java.util.ArrayList;
public class GeneratingSubsets {
	/*
	 * This code generates all subsets of a given
	 * set having n elements.
	 * 
	 * Time complexity: O(n2^n)
	 * Space complexity: O(2^n)
	 * 
	 */
	public static void main(String[] args) {
		int[] arr = { 1, 8, 7 };
		List<List<Integer>> subsets = generateSubsets(arr);
		/*
		 * Below prints:
		 * [[], [1], [7], [1, 7], [8], [1, 8], [7, 8], [1, 7, 8],
		 * [9], [1, 9], [7, 9], [1, 7, 9], [8, 9], [1, 8, 9], [7,8, 9],
		 * [1, 7, 8, 9]]
		 */
		System.out.println(subsets);
	}

	// Below function generates subsets of array
	private static List<List<Integer>> generateSubsets(int[] arr) {
		// Idea is to count on binary representation of array
		// size. Then, consider all binary representations
		// having 1 valued bits using positions of these bits
		// as indexes into array.
		List<List<Integer>> subsets = new ArrayList<>();
		int[] set = { 1, 7, 8, 9 };
		int n = set.length;
		for (int b = 0; b < (1 << n); b++) {
			List<Integer> subset = new ArrayList<Integer>();
			for (int i = 0; i < n; i++) {
				if ((b & (1 << i)) != 0) {
					subset.add(set[i]);
				}
			}
			subsets.add(subset);
		}
		return subsets;
	}
}
Comment

How to generate all the possible subsets of a set ?

#include<bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);

  int n;
  cin>>n;
 vector<char>v(n);
 for(int i=0;i<n;i++){
     cin>>v[i];
 }
cout<<"All Possible subset:
";
 for(int i=0;i<(1<<n);i++){
     for(int j=0;j<n;j++){
        if(i&(1<<j)){
             cout<<v[j]<<" ";
        }
     }
     cout<<'
';
 }

return 0;
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to create a item in minecraft modding 
Java :: primefaces datepicker validation 
Java :: primefaces calendar min date validation 
Java :: check the string has spaces in it in java 
Java :: containskey java complexity 
Java :: formartted string java 
Java :: JAVA_HOME is not defined correctly. 
Java :: input date in java 
Java :: Unhandled exception: java.lang.InterruptedException 
Java :: java pause program until key pressed 
Java :: how to get float value in json java 
Java :: get first 5 characters of string java 
Java :: java code examples 
Java :: How to create a disjoint-set data structure, in Java? 
Java :: Java Using forName() method 
Java :: How to efficiently find the highest power of 2 dividing a given number, in Java? 
Java :: how to use decimal format java 
Java :: char cannot be converted to string 
Java :: mongoose collection name 
Java :: sorting hashmap by key in java 8 
Java :: java run shell command 
Java :: java 2d array loop 
Java :: system.out.print two dimensional array 
Java :: instantiate matrix java 
Java :: android toast in adapter 
Java :: java game 
Java :: sort list of string lexicographically java 
Java :: fibonacci series in java using recursion 
Java :: sorting an arraylist 
Java :: arrays sort 2d array java 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =