-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path31_Bitwise_ORs_of_Subarrays.cpp
More file actions
69 lines (49 loc) · 1.98 KB
/
Copy path31_Bitwise_ORs_of_Subarrays.cpp
File metadata and controls
69 lines (49 loc) · 1.98 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
61
62
63
64
65
66
67
68
69
// 898. Bitwise ORs of Subarrays
// Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr.
// The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
// A subarray is a contiguous non-empty sequence of elements within an array.
// Example 1:
// Input: arr = [0]
// Output: 1
// Explanation: There is only one possible result: 0.
// Example 2:
// 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.
// Example 3:
// Input: arr = [1,2,4]
// Output: 6
// Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
// Constraints:
// 1 <= arr.length <= 5 * 104
// 0 <= arr[i] <= 109
class Solution
{
public:
int subarrayBitwiseORs(std::vector<int> &arr)
{
// This set will store all unique OR values found across all subarrays.
std::unordered_set<int> result_ors;
// This set stores the distinct ORs of all subarrays ending at the previous position.
std::unordered_set<int> current_ors;
for (int x : arr)
{
// `next_ors` will store the ORs of subarrays ending at the current element `x`.
std::unordered_set<int> next_ors;
// The subarray of just the element x gives an OR of x.
next_ors.insert(x);
// Compute new ORs by extending previous subarrays with the current element x.
for (int y : current_ors)
{
next_ors.insert(x | y);
}
// Add all newly found ORs to the main result set.
result_ors.insert(next_ors.begin(), next_ors.end());
// For the next iteration, the current results become the previous results.
current_ors = next_ors;
}
return result_ors.size();
}
};