Bitwise ORs of Subarrays - Complete Solution Guide
Bitwise ORs of Subarrays is LeetCode problem 898, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks us to find the number of *distinct* bitwise OR results of all possible non-empty subarrays of a given integer array `arr`. A subarray is a contiguous sequence of elements within the array. The bitwise OR of a subarray is calculated by performing the bitwise OR operation on all the elements within that subarray. We need to consider all possible subarrays, calculate their bitwise OR, and then count the number of unique results.
Solution Approach
The provided solutions use a dynamic programming approach by maintaining a set `cur` that represents the distinct bitwise OR values of subarrays ending at the current index. For each element `x` in the array, a new set `nextCur` is created. `nextCur` includes the element `x` itself, and also all the bitwise ORs of `x` with the values present in the `cur` set (i.e., the ORs of the subarrays ending at the previous index). The `cur` set is then updated to `nextCur`, and all elements in `cur` are added to a result set `res`. The size of `res` gives the count of distinct bitwise OR values.
Step-by-Step Algorithm
- Step 1: Initialize an empty set `res` to store the distinct bitwise OR values.
- Step 2: Initialize an empty set `cur` to store the distinct bitwise OR values of subarrays ending at the current index.
- Step 3: Iterate through the input array `arr`.
- Step 4: For each element `x` in `arr`:
- Step 4.1: Create a new set `nextCur` and add `x` to it.
- Step 4.2: Iterate through the `cur` set and add the bitwise OR of `x` and each element `y` in `cur` to `nextCur`.
- Step 4.3: Update `cur` to `nextCur`.
- Step 4.4: Add all elements in `cur` to `res`.
- Step 5: Return the size of `res`.
Key Insights
- Insight 1: The bitwise OR operation is monotonic, meaning that as you add more elements to a subarray and OR them together, the result can only stay the same or increase (in terms of the numerical value, as more bits can be set to 1).
- Insight 2: For each element in the array, we only need to keep track of the distinct OR values that can be obtained by ending the subarray at that element. This is because any OR value obtained earlier can be extended to include the current element.
- Insight 3: The maximum number of distinct bitwise OR values ending at a given index is limited by the number of bits in the input elements (because each new OR operation can only add new bits to the result. In the constraints, the elements are limited to 10^9, which requires 30 bits. Thus the size of the set 'cur' is bounded by 30, but it's simpler to just iterate through all ORs ending at the previous index). While the set can theoretically reach n, in practice it is much smaller.
- Insight 4: Using a set allows us to efficiently store and check for distinct values.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Dynamic Programming, Bit Manipulation.
Companies
Asked at: tcs.