-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubArraysBitwiseOr.java
More file actions
40 lines (28 loc) · 961 Bytes
/
Copy pathSubArraysBitwiseOr.java
File metadata and controls
40 lines (28 loc) · 961 Bytes
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
import java.util.Set;
import java.util.HashSet;
class SubArraysBitwiseOr{
private static int subArraysBitwiseOr(int nums[]){
//Fix i and run j from i to n-1
//res will strres the unique val
Set<Integer> curr= null, prev= new HashSet<>(), res= new HashSet<>();
for(int currNum: nums){
curr= new HashSet<>();
prev.add(0);
for(int prevNum: prev){
curr.add(prevNum | currNum);
res.add(prevNum | currNum);
}
prev= curr;
}
return res.size();
}
public static void main(String[] args) {
// Input: arr = [1,1,2]
// Output: 3
// Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
// These yield the results 1, 1, 2, 1, 3, 3.
// There are 3 unique values, so the answer is 3.
int nums[]={1,1,2};
System.out.println(subArraysBitwiseOr(nums));
}
}