Given an array **A** of non-negative integers, where

**Examples:**

Input: A = [1, 2] Output: 3 Explanation: The possible subarrays are [1], [2], [1, 2]. These Bitwise OR of subarrays are 1, 2, 3. There are 3 distinct values, so the answer is 3. Input: A = [1, 2, 4] Output: 6 Explanation: The possible distinct values are 1, 2, 3, 4, 6, and 7.

**Approach:**

The Naive approach is to generate all possible subarrays and take bitwise OR of all elements in the subarray. Store each result in **set** and return **length of the set**.**Efficient Approach:**

We can make the above approach better. The Naive approach is to calculate all possible result where, **res(i, j) = A[i] | A[i+1] | … | A[j]**. However we can speed this up by taking note of the fact that **res(i, j+1) = res(i, j) | A[j+1]**. At the **kth** step, say we have all of the **res(i, k)** in some set **pre**. Then we can find the next **pre** set **(for k -> k+1)** by using **res(i, k+1) = res(i, k) | A[k+1]**.

However, the number of unique values in this set **pre** is atmost 32, since the list **res(k, k), res(k-1, k), res(k-2, k), …** is **monotone increasing**, and any subsequent values that are different from previous must have more 1’s in it’s binary representation which can have maximum of **32 ones**.

**Below is the implementation of above approach.**

`# Python implementation of the above approach ` ` ` `# function to return count of distinct bitwise OR ` `def` `subarrayBitwiseOR(A): `
` ` ` ` `# res contains distinct values `
` ` `res ` `=` `set` `() `
` ` ` ` `pre ` `=` `{` `0` `} `
` ` ` ` `for` `x ` `in` `A: `
` ` `pre ` `=` `{x | y ` `for` `y ` `in` `pre} | {x} `
` ` `res |` `=` `pre `
` ` ` ` `return` `len` `(res) `
` ` ` ` `# Driver program ` `A ` `=` `[` `1` `, ` `2` `, ` `4` `] `
` ` `# print required answer ` `print` `(subarrayBitwiseOR(A)) `
` ` `# This code is written by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

Output:

6

**Time Complexity:** O(N*log(K)), where N is the length of A, and K is the maximum size of elements in A.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Sum of bitwise OR of all subarrays
- Sum of Bitwise-OR of all subarrays of a given Array | Set 2
- Sum of bitwise AND of all subarrays
- Count even length subarrays having bitwise XOR equal to 0
- Count subarrays having total distinct elements same as original array
- Count of subarrays having exactly K distinct elements
- Count subarrays having each distinct element occuring at least twice
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Number of subarrays have bitwise OR >= K
- Bitwise operations on Subarrays of size K
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Find N distinct numbers whose bitwise Or is equal to K
- Subarrays with distinct elements
- Check if an Array is made up of Subarrays of continuous repetitions of every distinct element

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.