Bitwise ORs of Subarrays
We have an array A
of non-negative integers.
For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]]
(with i <= j
), we take the bitwise OR of all the elements in B
, obtaining a result A[i] | A[i+1] | ... | A[j]
.
Return the number of possible results. (Results that occur more than once are only counted once in the final answer.)
Example 1:
Example 2:
Example 3:
Note:
1 <= A.length <= 50000
0 <= A[i] <= 10^9
Explaination:
For example, an array is [001, 011, 100, 110, 101] (in binary).
All the subarrays are:
In each line, we add a new number to each array of the previous line.
Calculate the OR operations for each subarray:
There are O(N^2) numbers in total, which is not acceptable.
After removing duplicate numbers in each line, it becomes:
In each line t
, for any two numbers t[i]
and t[j]
(i < j), t[i]
must have more 1
s than t[j]
. So the length of each line will not exceed 32. So if we remove duplicate numbers, the complexity would not be very large.
The complexity is O(kN), where k is a constant depends on the bitwise length of input numbers. (32 in this problem)
Last updated