Bitwise ORs of Subarrays

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
Inputarr = [0]
Output1
There is only one possible result: 0.
Example 2
Inputarr = [1,1,2]
Output3
The possible subarrays yield bitwise OR results 1, 1, 2, 1, 3, and 3, so there are 3 unique values.

Constraints

  • 1 <= arr.length <= 5 * 10^4
  • 0 <= arr[i] <= 10^9

Asked at 7 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate