O(N) Python3 Using Mask of Unique Bits
Intuition\nThe solution left-shifts a num k times, since that causes the highest possible bit; however, it is unclear which num will need to be shifted.\n\n# Ap
# Intuition\nThe solution left-shifts a num k times, since that causes the highest possible bit; however, it is unclear which num will need to be shifted.\n\n# Approach\nWe will check each num to see which num produces the maximum orred value. Prior to iteration, we calculate uniqueMask, the OR of bits that are set in exactly one num, in order to see which bits are removed by shifting any given num. The value produced is the orred nums, adjusted by the bits most by shifting num.\n\n# Complexity\n- Time complexity:\n$$O(N)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n\n    def maximumOr(self, nums: List[int], k: int) -> int:\n        counts = [0] * 32\n        orredNums = 0\n        for num in nums:\n            orredNums |= num\n            bit = 0\n            while num:\n                if num % 2 == 1:\n                    counts[bit] += 1\n                num //= 2\n                bit += 1\n                \n        # Get the OR of all bits that are in only one num.\n        uniqueMask = 0\n        for i, count in enumerate(counts):\n            if count == 1:\n                uniqueMask |= 1 << i\n        \n        # The best value is produced by left-shifting a num k times.\n        # The left-shifting will remove any bits that num had uniquely\n        # added, so remove these first.\n        bestOrredNumsWithShifts = 0\n        for num in nums:\n            orredNumsWithShifts = orredNums - (num & uniqueMask)\n            orredNumsWithShifts |= num << k\n            bestOrredNumsWithShifts = max(\n                bestOrredNumsWithShifts, orredNumsWithShifts)\n\n        return bestOrredNumsWithShifts\n            \n        \n```
['Python3']