Bitwise or of all subsequence sums

December 26, 2022

bit-manipulation

Problem URL: Bitwise or of all subsequence sums

We will iterate over the array and update the prefix sum and bitwise or of all subsequence sums.

class Solution:
    def subsequenceSumOr(self, nums: List[int]) -> int:
        res = prefix = 0
        for num in nums:
            prefix += num
            res = num | prefix | res
        return res

Time complexity: O(n)
Space complexity: O(1)