Interview Question
Country: United States
public class PowetSet {
public static void main(String[] args) {
char set[] = { 'a', 'b', 'c' };
printPowerSet(set, 3);
}
static void printPowerSet(char set[], int set_size) {
/*
* set_size of power set of a set with set_size n is (2**n -1)
*/
int pow_set_size = (int) Math.pow(2, set_size);
int counter, j;
/* Run from counter 000..0 to 111..1 */
for (counter = 0; counter < pow_set_size; counter++) {
for (j = 0; j < set_size; j++) {
/*
* Check if jth bit in the counter is set If set then pront jth
* element from set
*/
int value = 1 << j;
// System.out.println("value" + value);
int res = counter & (value);
//System.out.println("res ::" + res);
// Take any of the below if condition.. In both the case it'll work
//if (res != 0)
if (res == 0)
System.out.print(
// "set[j]::" +
set[j]
// + "\t" + ":: j :: "
// + j + "\t"
);
}
System.out.print("\t");
}
}
}
public class PowerSet{
public static void main(String[] args) {
PowerSet main = new PowerSet();
main.printPowerSet("ABCDE");
}
//print powerset
public void printPowerSet(String set){
int length=set.length();
for(int i=0;i<length;i++){
System.out.println(set.substring(i, i+1));
for(int j=i+1;j<length;j++){
for(int k=j;k<length;k++){
System.out.println(set.substring(i, j)+set.substring(k,k+1));
}
}
}
}
}
public class PowerSet {
public static void main(String[] args) {
PowerSet main = new PowerSet ();
main.printPowerSet("ABC");
}
//print powerset
public void printPowerSet(String set){
int length=set.length();
for(int i=0;i<length;i++){
System.out.println(set.substring(i, i+1));
for(int j=i+1;j<length;j++){
for(int k=j;k<length;k++){
System.out.println(set.substring(i, j)+set.substring(k,k+1));
}
}
}
}
}
public class PowetSet {
- UniqueT July 07, 2015public static void main(String[] args) {
char set[] = { 'a', 'b', 'c' };
printPowerSet(set, 3);
}
static void printPowerSet(char set[], int set_size) {
/*
* set_size of power set of a set with set_size n is (2**n -1)
*/
int pow_set_size = (int) Math.pow(2, set_size);
int counter, j;
/* Run from counter 000..0 to 111..1 */
for (counter = 0; counter < pow_set_size; counter++) {
for (j = 0; j < set_size; j++) {
/*
* Check if jth bit in the counter is set If set then pront jth
* element from set
*/
int value = 1 << j;
// System.out.println("value" + value);
int res = counter & (value);
//System.out.println("res ::" + res);
// Take any of the below if condition.. In both the case it'll work
//if (res != 0)
if (res == 0)
System.out.print(
// "set[j]::" +
set[j]
// + "\t" + ":: j :: "
// + j + "\t"
);
}
System.out.print("\t");
}
}
}