cud u help me ... i am struggling with this question... to
find all the subsets of a given set for ex.... a,,b,c shud
give all the subsets.... i gt the program in c bt nt able
to get it in java..... help needed ..



cud u help me ... i am struggling with this question... to find all the subsets of a given set for ..

Answer / Devesh Kumar Dubey

To find all the subsets of a given set in Java, you can use bit manipulation techniques. Here's an example code snippet that demonstrates this approach:

```
import java.util.ArrayList;

public class Subset {
static int n = 3; // number of elements in the set
static ArrayList<ArrayList<Integer>> subsets = new ArrayList<>();

public static void main(String[] args) {
ArrayList<Integer> set = new ArrayList<>();
for (int i = 1; i <= n; i++) {
set.add(i);
}
findSubsets(set, (int) Math.pow(2, n) - 1);
for (ArrayList<Integer> subset : subsets) {
System.out.println(subset);
}
}

public static void findSubsets(ArrayList<Integer> set, int k) {
if (k == 0) {
// base case: empty subset
ArrayList<Integer> emptySet = new ArrayList<>();
subsets.add(emptySet);
return;
}

for (int i = set.size() - 1; i >= 0; i--) {
// create a copy of the current subset and add the current element if the corresponding bit in k is set
ArrayList<Integer> newSet = new ArrayList<>(set);
if ((k & (1 << i)) > 0) {
newSet.add(newSet.get(i));
}
// recursively find subsets for the remaining elements and the updated k value
findSubsets(newSet, k - (1 << i));
}
}
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Advanced Java Interview Questions

Explain the purposes of methods wait(), notify(), notifyAll ()?

2 Answers  


How are the elements of a borderlayout organized?

1 Answers  


Write a singleton program?

1 Answers  


1) Scenario: I developed my application on local system and everything is perfect and tested. Now on UAT this application is deployed on clustered server environment. They are logical or physically clustered. But application is not working over there. &#61656; What is the issue with application? &#61656; What are the first things which come in your mind?

1 Answers   L&T,


Can we sent objects using Sockets?

1 Answers  


What are synchronized methods and synchronized statements?

2 Answers   Adobe,


what is the use of Object Factories?

1 Answers  


Explain bind(), rebind(), unbind() and lookup() methods?

1 Answers  


Explain the difference between object state and behavior?

1 Answers  


what is JTS?

1 Answers  


What are the various thread priorities?

1 Answers  


What exceptions are thrown by RMI?

1 Answers  


Categories