Count Good Subarrays
You are given an integer array nums.
A contiguous subarray is considered good if the bitwise OR of all its elements is equal to at least one element present inside that subarray.
Return the total number of good subarrays within nums.
(Note: The bitwise OR of two integers a and b is denoted as a | b).
Example 1:
Input: nums = [5, 5, 5]
Output: 6
Explanation:
Every subarray consisting of identical numbers will have a bitwise OR equal to that number. Since all 6 possible subarrays evaluate to 5 (which is inside every subarray), they are all good.
Example 2:
Input: nums = [1, 2, 4]
Output: 3
Explanation:
The bitwise OR of any multiple elements here creates a new number not present in the subarray (e.g., 1 | 2 = 3; 2 | 4 = 6; 1 | 2 | 4 = 7). Therefore, only the 3 single-element subarrays [1], [2], and [4] are good.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [1, 2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- 0 <= nums[i] <= 109