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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find all the unique results you can get by combining numbers in different parts of a list using a special rule. The brute force approach is like trying every single possible part of the list and combining the numbers in it to see what results we get.
Here's how the algorithm would work step-by-step:
def bitwise_ors_of_subarrays_brute_force(numbers):
unique_results = set()
# Iterate through each starting index of the subarray
for start_index in range(len(numbers)):
current_bitwise_or = 0
# Iterate through each ending index of the subarray, beginning at start_index
for end_index in range(start_index, len(numbers)):
current_bitwise_or |= numbers[end_index]
# Add the current bitwise OR to the set of unique results
unique_results.add(current_bitwise_or)
# Converting to list to return
return list(unique_results)
The basic idea is that as you calculate the bitwise OR for growing sections, new bitwise OR values added by each number eventually stop introducing new values. We use this fact to avoid redundant calculations, focusing only on unique results from shorter sections to build longer ones efficiently.
Here's how the algorithm would work step-by-step:
def bitwise_ors_of_subarrays(collection):
unique_bitwise_or_values = set()
current_bitwise_or_values = set()
for number in collection:
new_bitwise_or_values = set()
# Starting point: each element itself.
new_bitwise_or_values.add(number)
unique_bitwise_or_values.add(number)
# Calculate ORs with previous subarray ORs.
for previous_bitwise_or in current_bitwise_or_values:
new_bitwise_or = previous_bitwise_or | number
new_bitwise_or_values.add(new_bitwise_or)
unique_bitwise_or_values.add(new_bitwise_or)
# Update the set of current OR values.
current_bitwise_or_values = new_bitwise_or_values
# Return all unique bitwise OR values.
return list(unique_bitwise_or_values)
Case | How to Handle |
---|---|
Empty input array | Return an empty set since there are no subarrays to process, avoiding null pointer exceptions and ensuring correct behavior. |
Array with a single element | Return a set containing only that single element as the only possible subarray OR value. |
Array with all elements equal to zero | The result should be a set containing only zero since the OR of any subarray will be zero. |
Array with very large numbers (potential integer overflow) | Ensure the data type used to store the OR result (e.g., long) is large enough to accommodate the bitwise OR of the largest possible numbers to prevent overflow. |
Array with all elements being the maximum integer value | The result will likely contain only the maximum integer value; handle memory efficiently since the intermediate set could grow large. |
Array with repeating patterns that create identical ORs | Using a set to store the results ensures that we only store unique OR values, preventing duplicates from skewing the output and optimizing memory usage. |
Very large array causing memory issues | Iterate through the array and compute ORs of subarrays, using a set to store distinct values; avoid storing all subarrays simultaneously to reduce memory footprint. |
Negative numbers in the array (if applicable/allowed) | The bitwise OR operation will still function correctly with negative numbers, and the set will store their unique OR results assuming negative values are permissible in the prompt. |