-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2044.java
More file actions
60 lines (47 loc) · 1.51 KB
/
LC2044.java
File metadata and controls
60 lines (47 loc) · 1.51 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* LC2044
*/
import java.util.*;
public class LC2044 {
// p-np login (pick-no pick)
public static int recur(int index, int nums[], int currOr, int targetOr, Integer dp[][]) {
// base case
if (index == nums.length) {
return (currOr == targetOr) ? 1 : 0;
}
// Check if alread slved
if (dp[index][currOr] != null) {
return dp[index][currOr];
}
// pick
int pickCount = recur(index + 1, nums, currOr | nums[index], targetOr, dp);
// no pick
int noPpickCount = recur(index + 1, nums, currOr, targetOr, dp);
return dp[index][currOr] = pickCount + noPpickCount;
}
public static int countMaxOrSubsets(int[] nums) {
// target or
int targetOr = 0;
for (int num : nums) {
targetOr |= num;
}
Integer dp[][] = new Integer[nums.length][targetOr + 1];
return recur(0, nums, 0, targetOr, dp);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array Length : ");
int size = sc.nextInt();
System.out.println();
int arr[] = new int[size];
System.out.println("Enter Array Elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("[%d] : ", i);
arr[i] = sc.nextInt();
}
System.out.println();
int ans = countMaxOrSubsets(arr);
System.out.println(ans);
sc.close();
}
}