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 * 1040 <= arr[i] <= 109When 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)Instead of recalculating every possible combination from scratch, the optimal approach builds results progressively from left to right. It realizes that as you consider larger groups of numbers, the combined values can only grow or stay the same, creating a very small number of unique totals to remember.
Here's how the algorithm would work step-by-step:
class Solution:
def subarrayBitwiseORs(self, array_of_numbers: list[int]) -> int:
master_collection = set()
current_active_totals = set()
# We iterate through each number to dynamically build all possible subarray OR combinations.
for current_number in array_of_numbers:
next_active_totals = {current_number | previous_total for previous_total in current_active_totals}
next_active_totals.add(current_number)
current_active_totals = next_active_totals
master_collection.update(current_active_totals)
# Returning the total count of distinct values accumulated across all valid subarrays.
return len(master_collection)| Case | How to Handle |
|---|---|
| Empty array or null input | Return zero immediately as no subarrays can be formed from an empty input. |
| Array with a single element | Return one because only a single subarray exists, resulting in one unique bitwise OR value. |
| Maximum length array with large constraints | Utilize the property that bitwise OR results only grow, limiting the number of unique values per ending index to at most thirty-two to pass within time limits. |
| Array containing all identical values | The algorithm efficiently collapses overlapping subarray results, resulting in just a single unique bitwise OR value added to the global set. |
| Array containing zeros | Zero acts as an identity element for bitwise OR operations, which the cumulative reduction handles naturally without special conditional branches. |
| Inputs with negative integers | Negative numbers in two's complement representation have their sign bit set, which propagates correctly through standard bitwise OR operations. |
| Array with alternating or maximally varied bits | The running set of results will quickly reach maximum bit capacity and stabilize, preventing unnecessary duplicate computations in subsequent iterations. |
| Language-specific integer overflow | Ensure standard integer types are used that conform to the bit-width requirements of the bitwise operations without causing unexpected sign extensions. |