JavaScript O(n): apply k to a single element
Intuition\nThe best result appears when we apply k to a single element.\n# Approach\nFor each element, apply all k moves to it, and leave others unchanged. Thus
# Intuition\nThe best result appears when we apply k to a single element.\n# Approach\nFor each element, apply all k moves to it, and leave others unchanged. Thus we get n outcomes, and we just need to select the biggest.\nWe can use an array to record the freqs of each bit when we change none of the elements. When we apply the k moves to a single element x, we just need to subtract its bits from the array, and apply the bits of (x << k) to the array. Finally, we calc the corresponding outcome value based on the array.\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n```\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar maximumOr = function(nums, k) {\n    const size = 46\n    const baseTable = new Array(size).fill(0)\n    for (const [i, val] of nums.entries()) {\n        for (let x = val, j = 0; x !== 0; x >>= 1, j++) {\n            const digit = x & 1\n            baseTable[j] += digit\n        }\n    }\n    \n\n    function getOutcome(index) {\n        const table = [...baseTable]\n        const val = nums[index]\n        for (let x = val, j = 0; x !== 0; x >>= 1, j++) {\n            const digit = x & 1\n            table[j] -= digit\n            table[j + k] += digit\n        }\n        \n        let result = 0\n        for (let i = 0, factor = 1; i < size; i++, factor *= 2) {\n            const digit = Number(Boolean(table[i]))\n            result += digit * factor\n        }\n\n        return result\n    }\n\n\n    let result = 0\n    for (const i of nums.keys()) {\n        const outcome = getOutcome(i)\n        result = Math.max(result, outcome)\n    }\n\n\n    return result\n};\n```
['JavaScript']