💡✅🔥 Bitwise operations with detailed explanation
Intuition\nObservations:\n- all prime numbers are odd (except 10_2), so numbers will always be like ...0111 or 111\n- simpliest answer is just num - 1, but we w
# Intuition\nObservations:\n- all prime numbers are odd (except $$10_2$$), so numbers will always be like `...0111` or `111`\n- simpliest answer is just `num - 1`, but we want to minimize it\n- it\'s easy to notice how to find an answer for a number with ones only (like `1111`). We just need to take a number without leading `1`:\n$$111_2 \\lor (111_2 + 1_2) = 111_2 \\lor 1000_2 = 1111_2$$\n- in the examples there is a case with `num = 11` ($$1011_2$$) and `ans = 9` ($$1001_2$$). Approach from the previous point is actually applicable here, just not with the leading `1` but with the rightest `1` before some `0`. For $$1011_2$$ it\'s the 3 digit or the second `1` from the right:\n$$1001_2 \\lor (1001_2 + 1_2) = 1001_2 \\lor 1010_2 = 1011_2$$\n\n# Approach\n1. count the number of trailing `1`\'s in a `num` before `0`\n2. answer will be the `num` without rigthest `1` so we can substruct `num` with $$2^{k-1}$$, where k = number of `1`\'s, for example:\n`num` = $$11_{10}$$ = $$1011_2$$, k = 2, `ans` = $$11_{10} - 2^{(2 - 1)}$$ = $$11 - 2 ^ 1$$= $$9$$.\n\nWe can use bitwise shift to make traversal on number binary  representation easier.\n\n# Complexity\n- Time complexity:\n$$O(n * k)$$, where `n = nums.length` and `k` is a longest binary representation of a number.\n\n- Space complexity:\n$$O(1)$$ in case we don\'t count answer array, $$O(n)$$ otherwise.\n\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar minBitwiseArray = function(nums) {\n    const ans = [];\n\n    for (const n of nums) {\n        let num = n;\n        let ones = 0;\n\n        while(num > 0 && num % 2 === 1) {\n            ones += 1;\n            num >>= 1;\n        }\n\n        if (ones === 0) {\n            ans.push(-1);\n\n            continue;\n        }\n\n        ans.push(n - 2 ** (ones - 1));\n    }\n\n    return ans;\n};\n```
['Array', 'Math', 'Bit Manipulation', 'JavaScript']