KatInterview

934. Bitwise ORs of Subarrays

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the number of distinct values obtained by taking the bitwise OR of all elements in every possible non-empty contiguous subarray of a given array arr.

Solution

Bitwise ORs of Subarrays in Python

class Solution:
    def subarrayBitwiseORs(self, arr: List[int]) -> int:
        # This set stores all unique bitwise ORs found so far.
        results = set()
        # This set stores the bitwise ORs of all subarrays ending at the previous element.
        current = set()

        for x in arr:
            # This set will store the bitwise ORs of all subarrays ending at the current element 'x'.
            # It is equivalent to {val | x for val in current} U {x}.
            next_current = {x | prev_or for prev_or in current}
            next_current.add(x)
            
            # The key insight: the size of 'next_current' (and 'current') is small.
            # It's bounded by the number of bits in an integer (e.g., 32),
            # because the OR values form a non-decreasing sequence.

            # Update 'current' for the next iteration.
            current = next_current
            
            # Add all newly found OR values to the global results set.
            results.update(current)
            
        return len(results)

Bitwise ORs of Subarrays in Java

class Solution {
    public int subarrayBitwiseORs(int[] arr) {
        // This set stores all unique bitwise ORs found so far.
        Set<Integer> results = new HashSet<>();
        // This set stores the bitwise ORs of all subarrays ending at the previous element.
        Set<Integer> current = new HashSet<>();

        for (int x : arr) {
            // This set will store the bitwise ORs of all subarrays ending at the current element 'x'.
            Set<Integer> nextCurrent = new HashSet<>();

            // The subarray containing only 'x' has an OR value of 'x'.
            nextCurrent.add(x);

            // For each OR value of a subarray ending at the previous position,
            // we extend it with the current element 'x'.
            for (int prevOr : current) {
                nextCurrent.add(prevOr | x);
            }

            // The key insight: the size of 'nextCurrent' (and 'current') is small.
            // It's bounded by the number of bits in an integer (e.g., 32),
            // because the OR values form a non-decreasing sequence.

            // Update 'current' for the next iteration.
            current = nextCurrent;

            // Add all newly found OR values to the global results set.
            results.addAll(current);
        }

        return results.size();
    }
}

Bitwise ORs of Subarrays in C++

class Solution {
public:
    int subarrayBitwiseORs(vector<int>& arr) {
        // This set stores all unique bitwise ORs found so far.
        unordered_set<int> results;
        // This set stores the bitwise ORs of all subarrays ending at the previous element.
        unordered_set<int> current;

        for (int x : arr) {
            // This set will store the bitwise ORs of all subarrays ending at the current element 'x'.
            unordered_set<int> next_current;
            
            // The subarray containing only 'x' has an OR value of 'x'.
            next_current.insert(x);
            
            // For each OR value of a subarray ending at the previous position,
            // we extend it with the current element 'x'.
            for (int prev_or : current) {
                next_current.insert(prev_or | x);
            }
            
            // The key insight: the size of 'next_current' (and 'current') is small.
            // It's bounded by the number of bits in an integer (e.g., 32),
            // because the OR values form a non-decreasing sequence.
            
            // Update 'current' for the next iteration.
            current = next_current;
            
            // Add all newly found OR values to the global results set.
            for (int val : current) {
                results.insert(val);
            }
        }

        return results.size();
    }
};

Bitwise ORs of Subarrays in TypeScript

function subarrayBitwiseORs(arr: number[]): number {
    // This set stores all unique bitwise ORs found so far.
    const results = new Set<number>();
    // This set stores the bitwise ORs of all subarrays ending at the previous element.
    let current = new Set<number>();

    for (const x of arr) {
        // This set will store the bitwise ORs of all subarrays ending at the current element 'x'.
        const nextCurrent = new Set<number>();

        // The subarray containing only 'x' has an OR value of 'x'.
        nextCurrent.add(x);

        // For each OR value of a subarray ending at the previous position,
        // we extend it with the current element 'x'.
        for (const prevOr of current) {
            nextCurrent.add(prevOr | x);
        }

        // The key insight: the size of 'nextCurrent' (and 'current') is small.
        // It's bounded by the number of bits in an integer (e.g., 32),
        // because the OR values form a non-decreasing sequence.

        // Update 'current' for the next iteration.
        current = nextCurrent;

        // Add all newly found OR values to the global results set.
        for (const val of current) {
            results.add(val);
        }
    }

    return results.size;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.