Bit Manipulation approach | TC: O(n) | SC: O(1) | Java solution | Clean Code
Intuition\nLet n = nums.length\nLet\'s assume $k = 1$. We can multiply each number by $2$ one-by-one and for each we will calculate nums[0] | nums[1] | ... | nu
# Intuition\nLet `n = nums.length`\nLet\'s assume $k = 1$. We can multiply each number by $2$ one-by-one and for each we will calculate `nums[0] | nums[1] | ... | nums[n - 1]`. The answer will be maximum among all of this.\nNow let\'s say $k \\gt 1$. For each $k$ we have $n$ choices to pick. But rathar then picking other element, it is always better to pick an element which was giving maximum answer for $k = 1$ and multiply it further for $k - 1$ times and take maximum for answer.\nSo, in general we can multiply each number by $2^k$ one-by-one and for each we will calculate `nums[0] | nums[1] | ... | nums[n - 1]`. The answer will be maximum among all of this.\n\n# Approach\n- Store the total bitcount for each index from $0$ to $63$ in `bits` array. (this array will be used to calculate `nums[0] | nums[1] | ... | nums[n - 1]` in nearly $O(1)$ time)\n- For each `t` in `nums`\n    - Remove the actual bitcount of `t` from `bits` array\n    - Multiply it by $2$\n    - Add the new bitcount in `bits` array. if bitcount at this index is > 0 then add it the `curr` answer\n    - Remove the new bitcount and add the actual one to get back to original state\n- Take maximum of `curr` for each `t` \n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(1)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java\nclass Solution {\n    public long maximumOr(int[] nums, int k) {\n        long bits[] = new long[64];\n        for(int i = 0;i < nums.length;i++){\n            for(int j = 0;j < 64;j++){\n                bits[j] += (((long)nums[i] >> j) & 1);\n            }\n        }\n        long max = 0;\n        for(int i = 0;i < nums.length;i++){\n            long t = nums[i];\n            for(int j = 0;j < 64;j++){\n                bits[j] -= ((t >> j) & 1); // removing actual bitcount\n            }\n            t *= (1L << k);\n            long curr = 0;\n            for(int j = 0;j < 64;j++){\n                bits[j] += ((t >> j) & 1); // adding new bitcount\n                if(bits[j] > 0) curr |= (1L << j);\n                bits[j] -= ((t >> j) & 1); // removing new bitcount\n                bits[j] += (((long)nums[i] >> j) & 1); // adding actual bitcount\n            }\n            max = Math.max(max,curr);\n        }\n        return max;\n    }\n}\n```\n\n# Upvote if you like it \uD83D\uDC4D\uD83D\uDC4D
['Bit Manipulation', 'Java']