You are given an integer array nums.
A subarray is called good if the bitwise OR of all its elements is equal to at least one element present in that subarray.
Return the number of good subarrays in nums.
Here, the bitwise OR of two integers a and b is denoted by a | b.
Example 1:
Input: nums = [4,2,3]
Output: 4
Explanation:
The subarrays of nums are:
| Subarray | Bitwise OR | Present in Subarray |
|---|---|---|
[4] |
4 = 4 |
Yes |
[2] |
2 = 2 |
Yes |
[3] |
3 = 3 |
Yes |
[4, 2] |
4 | 2 = 6 |
No |
[2, 3] |
2 | 3 = 3 |
Yes |
[4, 2, 3] |
4 | 2 | 3 = 7 |
No |
Thus, the good subarrays of nums are [4], [2], [3] and [2, 3]. Thus, the answer is 4.
Example 2:
Input: nums = [1,3,1]
Output: 6
Explanation:
Any subarray of nums containing 3 has bitwise OR equal to 3, and subarrays containing only 1 have bitwise OR equal to 1.
In both cases, the result is present in the subarray, so all subarrays are good, and the answer is 6.
Constraints:
1 <= nums.length <= 1050 <= nums[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:
The brute force approach to finding good groups of items involves checking every single possible sub-group you can possibly make from the original collection. For each sub-group you create, you test it to see if it meets the definition of being good, and you keep a running tally of how many success stories you find.
Here's how the algorithm would work step-by-step:
def count_good_subarrays(collection): good_subarrays_tally = 0 # Iterate through every possible starting point to examine all continuous slices for start_index in range(len(collection)): for end_index in range(start_index, len(collection)): current_subarray = collection[start_index:end_index + 1] # A subarray is considered good if it contains at least one duplicate pair of items if len(current_subarray) != len(set(current_subarray)): good_subarrays_tally += 1 return good_subarrays_tallyInstead of checking every single possible group one by one, we can look at chunks of items and expand them wisely. Whenever we find a valid group, we realize that extending it further will also keep it valid, allowing us to count many successful groups all at once.
Here's how the algorithm would work step-by-step:
def count_good_subarrays(items_array, target_condition): window_left = 0
total_good_groups = 0
frequency_map = {}
current_pairs = 0
for window_right in range(len(items_array)):
current_item = items_array[window_right]
current_pairs += frequency_map.get(current_item, 0)
frequency_map[current_item] = frequency_map.get(current_item, 0) + 1
# Once condition is met, all extensions to the right are also valid subarrays.
while current_pairs >= target_condition:
total_good_groups += len(items_array) - window_right
left_item = items_array[window_left]
frequency_map[left_item] -= 1
current_pairs -= frequency_map[left_item]
window_left += 1
return total_good_groups| Case | How to Handle |
|---|---|
| Array length is less than 2 | Return zero immediately since it is impossible to form a pair of identical elements. |
| Array size is extremely large up to one hundred thousand elements | Use an O(n) sliding window approach with a hash map to ensure optimal scaling and avoid time limit exceeded errors. |
| All elements in the array are identical | The sliding window correctly calculates combinations using binomial coefficients to count all valid subarrays efficiently. |
| No subarray contains at least k pairs | The algorithm will process the entire array and naturally return a total count of zero. |
| The value of k is zero or negative | All possible subarrays are considered good by definition, so the function should return the total number of possible subarrays n times n plus one divided by two. |
| Target k is larger than the total possible pairs in the entire array | The sliding window expands to the end and returns zero because the condition is never satisfied. |
| Integer overflow from counting too many good subarrays | Use sixty-four-bit integers for the answer accumulator since the maximum number of subarrays can exceed standard thirty-two-bit integer limits. |
| Array contains negative numbers and zeros | The hash map treats negative numbers and zeros the same as positive numbers since equality checks function identically for all integers. |