Fast and simple O(n) solution
Intuition\nThe key to understanding the solution is the realize that for a given number the answer will vary only by a single set bit if at all such a number ex
# Intuition\nThe key to understanding the solution is the realize that for a given number the answer will vary only by a single set bit if at all such a number exists. \n\n# Approach\nLet\'s consider the number 13 for which we want to find the answer.\n\nThe number 13 in binary is 1101.\n\nWe can find variations for the binary representation of 13 by unsetting each set bit in 1101.\n\nThe possible variations include:\n1100 - 12\n1001 - 9\n0101 - 5\n\nWe can find if a bit is set at a position `x` for a number `num` by using: `((num >> x) & 1) == 1` \n\nWe can unset a bit at a position `x` for a number `num` by using: `(1 << x) ^ num` \n\nFor each of these numbers, we can OR with itself +1 and if it results to the original number 13, then we have found a possible answer. We will then pick the minimum of all possible answers. \n\n# Complexity\n- Time complexity:\nO(n)\n\nAlthought for each iteration of n, we loop 32 times to check each position in the binary representation of `nums[i]`, this is not included in the time complexity because it is constant. \n\n- Space complexity:\nO(1)\n\nWe are not including the space we use for creating the answer array.\n\n# Code\n```typescript []\nfunction minBitwiseArray(nums: number[]): number[] {\n    const ans: number[] = [];\n    for (const num of nums) {\n        let min = Infinity;\n        for (let i = 0; i < 32; ++i) {\n            if (((num >> i) & 1) === 1) {\n                const mask = 1 << i;\n                const n = mask ^ num;\n                if ((n | (n + 1)) === num) {\n                    min = Math.min(n, min);\n                }\n            }\n        }\n\n        ans.push(min === Infinity ? -1 : min);\n    }\n\n    return ans;\n};\n```
['TypeScript']