-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_2044.java
36 lines (31 loc) · 930 Bytes
/
Leetcode_2044.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public int countMaxOrSubsets(int[] nums) {
int maxOrValue = 0;
for (int num : nums) {
maxOrValue |= num;
}
return countSubsets(nums, 0, 0, maxOrValue);
}
private int countSubsets(
int[] nums,
int index,
int currentOr,
int targetOr
) {
// Base case: reached the end of the array
if (index == nums.length) {
return (currentOr == targetOr) ? 1 : 0;
}
// Don't include the current number
int countWithout = countSubsets(nums, index + 1, currentOr, targetOr);
// Include the current number
int countWith = countSubsets(
nums,
index + 1,
currentOr | nums[index],
targetOr
);
// Return the sum of both cases
return countWithout + countWith;
}
}