Java Program to Implement the Binary Counting Method to Generate Subsets of a Set

This is a java program to generate and print all possible subsets using the method of Binary Counting method. The generations of subsets are done using binary numbers. Let there be 3 elements in the set, we generate binary equivalent of 2^3 = 8 numbers(0-7), where each bit in a number represents the presence/absence of element in the subset. The element is present if bit is 1, absent otherwise. 010 – only second element is present in the subset.

Here is the source code of the Java Program to Implement the Binary Counting Method to Generate Subsets of a Set. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to generate all subsets of given set of numbers using binary counting method
import java.util.Random;
import java.util.Scanner;
 
public class Binary_Counting_Subsets 
{
    public static int[] binary(int N) 
    {
        int[] binary = new int[(int) Math.pow(2, N)];
        for (int i = 0; i < Math.pow(2, N); i++) 
        {
            int b = 1;
            binary[i] = 0;
            int num = i;
            while (num > 0) 
            {
                binary[i] += (num % 2) * b;
                num /= 2;
                b = b * 10;
            }
        }
        return binary;
    }
 
    public static void main(String args[]) 
    {
        Random random = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements in the set: ");
        int N = sc.nextInt();
        int[] sequence = new int[N];
        for (int i = 0; i < N; i++)
            sequence[i] = Math.abs(random.nextInt(100));
 
        System.out.println("The elements in the set : ");
        for (int i = 0; i < N; i++)
            System.out.print(sequence[i] + " ");
 
        int[] mask = new int[(int) Math.pow(2, N)];
        mask = binary(N);
 
        System.out.println("\nThe permutations are: ");
        for (int i = 0; i < Math.pow(2, N); i++) 
        {
            System.out.print("{");
            for (int j = 0; j < N; j++) 
            {
                if (mask[i] % 10 == 1)
                    System.out.print(sequence[j] + " ");
                mask[i] /= 10;
            }
            System.out.println("}");
        }
        sc.close();
    }
}

Output:

$ javac Binary_Counting_Subsets.java
$ java Binary_Counting_Subsets
 
Enter the number of elements in the set: 
5
The elements in the set : 
78 35 5 10 15 
The permutations are: 
{ }
{ 78 }
{ 35 }
{ 78 35 }
{ 5 }
{ 78 5 }
{ 35 5 }
{ 78 35 5 }
{ 10 }
{ 78 10 }
{ 35 10 }
{ 78 35 10 }
{ 5 10 }
{ 78 5 10 }
{ 35 5 10 }
{ 78 35 5 10 }
{ 15 }
{ 78 15 }
{ 35 15 }
{ 78 35 15 }
{ 5 15 }
{ 78 5 15 }
{ 35 5 15 }
{ 78 35 5 15 }
{ 10 15 }
{ 78 10 15 }
{ 35 10 15 }
{ 78 35 10 15 }
{ 5 10 15 }
{ 78 5 10 15 }
{ 35 5 10 15 }
{ 78 35 5 10 15 }

Related posts:

Apache Commons Collections OrderedMap
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Implement Sorted Doubly Linked List
Use Liquibase to Safely Evolve Your Database Schema
Hướng dẫn Java Design Pattern – Transfer Object
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to implement Dynamic Array
Java InputStream to Byte Array and ByteBuffer
How to Use if/else Logic in Java 8 Streams
Map to String Conversion in Java
Hướng dẫn Java Design Pattern – Intercepting Filter
Custom Cascading in Spring Data MongoDB
Introduction to Liquibase Rollback
Java Program to Implement Sieve Of Eratosthenes
A Guide to Queries in Spring Data MongoDB
Performance Difference Between save() and saveAll() in Spring Data
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Implement Sparse Array
Java Program to Find the Connected Components of an UnDirected Graph
Introduction to Spring Security Expressions
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Spring MVC and the @ModelAttribute Annotation
Spring Boot - Tomcat Port Number
Introduction to the Java NIO Selector
Mockito and JUnit 5 – Using ExtendWith
Java Program to Implement Ternary Tree
Compact Strings in Java 9
Java Program to Describe the Representation of Graph using Incidence List
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Uniform-Cost Search
Convert String to Byte Array and Reverse in Java
Tạo số và chuỗi ngẫu nhiên trong Java