Practice/Microsoft/Leetcode 898. Bitwise ORs of Subarrays
CodingMust
You are given an array of non-negative integers. Your task is to determine how many unique values can be obtained by computing the bitwise OR operation on all possible contiguous subarrays.
A contiguous subarray is any sequence of consecutive elements from the array. For each such subarray, calculate the bitwise OR of all its elements. Count the total number of distinct results across all subarrays.
Example 1:
Input: arr = [1, 2, 4] Output: 6 Explanation: The subarrays and their OR values are: [1] → 1 [2] → 2 [4] → 4 [1,2] → 3 [2,4] → 6 [1,2,4] → 7 Distinct values: \{1, 2, 3, 4, 6, 7\} = 6 distinct values
Example 2:
Input: arr = [1, 1, 1] Output: 1 Explanation: All subarrays produce OR value of 1
Example 3:
Input: arr = [0, 1, 1, 2] Output: 3 Explanation: Distinct OR values are 0, 1, 2, and 3