Prefix & Suffix - Detailed Explaination
\nclass Solution:\n    def maximumOr(self, nums: List[int], k: int) -> int:\n        n, res = len(nums), 0\n        pre, suf = [0] * n, [0] * n\n        # calcu
```\nclass Solution:\n    def maximumOr(self, nums: List[int], k: int) -> int:\n        n, res = len(nums), 0\n        pre, suf = [0] * n, [0] * n\n        # calculate the prefix OR \n        for i in range(n - 1): pre[i + 1] = pre[i] | nums[i]\n        # calculate the suffix OR\n        for i in range(n - 1, 0, -1): suf[i - 1] = suf[i] | nums[i]\n        # iterate each number\n        # we apply k operations on nums[i], i.e. shift k bits to the left\n        # why not applying on multiple numbers? \n        # first in binary format, multiplying a number by 2 is shifting 1 bit to the left\n        # e.g. 0010 (2) -> 0100 (4)\n        # e.g. 0101 (5) -> 1010 (10)\n        # second, in OR operation, we wish there is a 1 as left as possible\n        # which produces the greater value\n        # hence, we apply on the same number to achieve the max value\n        # which produces the max OR value\n        # now we calculate nums[0] | nums[1] | ... | nums[n - 1]\n        # by utilising the prefix OR and suffix OR\n        # the reason is simple\n        # we precompute the result instead of calculate the OR values on each iteration\n        for i in range(n): res = max(res, pre[i] | nums[i] << k | suf[i])\n        return res\n```\n\n**p.s. Join us on the LeetCode The Hard Way Discord Study Group for timely discussion! Link in bio.**
['Prefix Sum', 'Python']