Complete the code to start the loop for generating subsets using bitmask.
for (int mask = [1]; mask < (1 << nums.length); mask++) { // generate subset }
The bitmask starts from 0 to represent the empty subset and goes up to 2^n - 1.
Complete the code to check if the j-th element is included in the current subset mask.
if ((mask & (1 << [1])) != 0) { subset.add(nums[j]); }
We check the j-th bit of mask to see if the element at index j is included.
Fix the error in the inner loop to iterate over all elements for subset generation.
for (int [1] = 0; j < nums.length; j++) { if ((mask & (1 << j)) != 0) { subset.add(nums[j]); } }
The loop variable must be j to match the condition and indexing inside the loop.
Fill both blanks to correctly initialize and add the subset to the result list.
List<Integer> subset = new ArrayList<>();
// add elements to subset
result.[1](subset);We add the generated subset to the result list using add method.
Fill all three blanks to complete the subsets generation method using bitmask.
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
int n = nums.length;
for (int mask = [1]; mask < (1 << n); mask++) {
List<Integer> subset = new ArrayList<>();
for (int j = 0; j < n; j++) {
if ((mask & (1 << [2])) != 0) {
subset.add(nums[[3]]);
}
}
result.add(subset);
}
return result;
}The outer loop starts from 0. The bit check uses j, and the element added is nums[j].
