public void sub(int index, int[] arr1, List<Integer> arr2) {
int n = arr1.length;
if (index >= n) return; // base case
// don't take the current element
sub(index + 1, arr1, arr2);
// add the current element to the array
arr2.add(arr1[index]);
sub(index + 1, arr1, arr2);
// remove the current element from the array
arr2.remove(arr2.size() - 1);
}
Counting of the total number of ways
public class SubsetGenerator {
public int sub(int index, int[] arr1, List<Integer> arr2) {
int n = arr1.length;
if (index >= n) {
return 1;
}
// Don't take the current element
int l = sub(index + 1, arr1, arr2);
// Add the current element to the list
arr2.add(arr1[index]);
int r = sub(index + 1, arr1, arr2);
// Remove the current element from the list
arr2.remove(arr2.size() - 1);
return l+r;
}
Counting of the total number of ways