a program in java to generate the powerset of a given set
Hello :)
am a fresher to java
i am in need of a java code which generates the powerset of a given set
i.e
suppose the set S be {a,b,c}
the power set would be
{}
{a}
{b}
{c}
{a,b}
{a,c}
{b,c}
{a,b,c}
and also the size of the set may vary
kindly do the needfull
thanks and regards
Shilpa.R
View Answers
March 5, 2010 at 11:31 AM
Hi Friend,
Try the following code:
import java.util.*;
public class PowerSet{
public static void main(String[] args) {
String st[] = {"a", "b", "c"};
LinkedHashSet hashSet = new LinkedHashSet();
int len = st.length;
int elements = (int) Math.pow(2,len);
for (int i = 0; i < elements; i++) {
String str = Integer.toBinaryString(i);
int value = str.length();
String pset = str;
for (int k = value; k < len; k++) {
pset = "0" + pset;
}
LinkedHashSet set = new LinkedHashSet();
for (int j = 0; j < pset.length(); j++) {
if (pset.charAt(j) == '1')
set.add(st[j]);
}
hashSet.add(set);
}
System.out.println(hashSet.toString());
}
}
Thanks
Ads
Related Tutorials/Questions & Answers:
Java program to generate the total count
Java program to
generate the total count I need a
java program which would take a text file as an input and scans through it to
generate the output.
For Ex. : If the text file contains the following information:
There is/are 3
Advertisements
Please provide the java code for the given program.
Please provide the
java code for the
given program.
We need an application for managing an educational institute.
That application should provide...:
StudentAddr:
StudentDob:
Email:
Course
ALL
Java
C
C++
Oracle
Fees:
Duration:
Faculty
ModuleNotFoundError: No module named 'Powerset'
ModuleNotFoundError: No module named '
Powerset' Hi,
My Python
program is throwing following error:
ModuleNotFoundError: No module named '
Powerset'
How to remove the ModuleNotFoundError: No module named '
Powerset
How to store unique values in Java using Set?
Store unique values in
Java using
Set interface
In this section we will discuss about the
Set interface in
Java which is
designed to store unique values. We will show you how to store unique values in
Java using
Set? The
Set interface
How to store unique values in Java using Set?
Store unique values in
Java using
Set interface
In this section we will discuss about the
Set interface in
Java which is
designed to store unique values. We will show you how to store unique values in
Java using
Set? The
Set interface
please help me in coding this given program
please help me in coding this
given program consider the below... are the prime numbers in ascending order
write a
program to find the nth term in this series.
(i m unable to code this
program..please provide solution
please help me in coding this given program
please help me in coding this
given program consider the below... are the prime numbers in ascending order
write a
program to find the nth term in this series.
(i m unable to code this
program..please provide solution
Prime number program in java
Prime number
program in
java
In this example you will learn how to write a
program to
generate and
check prime number in
java. As we know prime...,stc;
String c;
do
{
System.out.println("Press 1 for
generate prime
Java code for set...!!!
Java code for
set...!!! Create 2 classes in same package
Product.java
productId, name, price
ProductImpl.java
create a
set in this and try... not allow the duplicate products to be added into the
set(equal productId).
Hint
java program
java program write a
java program to display array list and calculate the average of
given array
java program
java program Write a
java program to do matrix addition operation
On two
given matrices
java program
java program hi friends how to make a
java program for getting non prime odd numbers in a
given series
java program
java program write a
java program to display array list and calculate the average of
given array
Generate random numbers in Java
Generate random numbers in
Java - How to use the java.util.Random class to
generate desired random number in
Java program?
In this tutorial I will teach you how to write
Java code using the
java.util.Random class to
generate single
java program for
java program for
java program for printing documents,images and cards
How to run a Java program in CMD
a notepad or any other editor and write a small
program
in
Java as
given below... command at your command prompt.
set path=%path%;C:\
Program Files\
Java\jdk1.5.0_09...
discussed how to write and run
Java program in CMD.
Tips to run
Java program
java program
java program . Develop a
program that computes the distance a boat travels across a river,
given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time
a Java program
a
Java program
Write a
Java program to print even numbers from 2 to 1024?
Write a
Java program to print ? My Name is Mirza? 100 times?
Write a
Java program to print Fibonacci Series?
Write a
Java program to reverse a number
Java Program
Java Program A Simple
program of JSP to Display message, with steps to execute that
program
Hi Friend,
Follow these steps:
1)Go to the Control Panel>>System>>Environment Variable
And the
set
java program
java program how to write an addition
program in
java without using arithematic operator
Java Program
Java Program
java program to insert row in excel sheet after identifying an object
java program
java program Write a
program to create an applet and display
The message "welcome to
java
java program
java program write
java program for constructor,overriding,overriding,exception handling
java program
java program
java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers