Efficient bitwise O(NlogK)
Intuition\nI solved this problem by noticing a pattern in the given examples:\n\n\nInput | Answer | Input | Answer\n-------------------------------\n2          
# Intuition\nI solved this problem by noticing a pattern in the given examples:\n\n```\nInput | Answer | Input | Answer\n-------------------------------\n2           -1      10\n3            1      11       1\n5            4     101     100\n7            3     111      11\n11           9    1011    1001\n13          12    1101    1100\n31          15   11111    1111\n```\n\n- If the binary ends in 01 (like 5 and 13), then the answer is to just subtract 1.\n- If the binary consists of all 1s (like 7 and 31), the answer is to remove the leftmost 1.\n- 11 is the only one that doesn\'t fit into the above. I noticed that the answer involves removing that 2nd 1 from the right (1011 -> 1001). And in general, the approach is to remove the leftmost 1 of the longest chain of 1s starting from the right. So for 11, we remove the 2nd 1 to make it 1001, because if we add a 1, it can be ORed back to 11:\n```\n   1001\nOR 1010\n   ----\n   1011\n```\n**This holds true for all numbers** except for 2.\n\nSome more examples:\n\n`1111111` (127) would become `0111111` | `1000000` (63 | 64).\n```\n   0111111\nOR 1000000\n   -------\n   1111111\n```\n\n`11101111` (239) would become `11100111` | `11101000` (231 | 232).\n```\n   11100111\nOR 11101000\n   --------\n   11101111\n```\n\n`1110001` (113) would become `1110000` | `1110001` (112 | 113).\n```\n   1110000\nOR 1110001\n   -------\n   1110001\n```\n\n# Approach\nIf the number is 2, add -1 to the answer array. Else, start checking the bits from the right. Move left everytime there is a 1. When the first 0 is found, remove the 1 to the right. This minimizes the number as much as possible.\n\n# Code\n```python3 []\nclass Solution:\n    def minBitwiseArray(self, nums: List[int]) -> List[int]:\n        ans = []\n\n        for num in nums:\n            if num == 2:\n                ans.append(-1)\n                continue\n\n            num_copy = num\n            count = 0\n\n            # Count consecutive 1s from the right\n            while num & 1 == 1:\n                count += 1\n                num >>= 1\n            \n            # [count]th bit is the position of the last 1, like 100111 (count = 3)\n            # Subtract the bit                                     ^\n            ans.append(num_copy - 2 ** (count-1))\n        \n        return ans\n```\n``` java []\nclass Solution {\n    public int[] minBitwiseArray(List<Integer> nums) {\n        int[] ans = new int[nums.size()];\n\n        for (int i = 0; i < nums.size(); i++) {\n            int num = nums.get(i);\n\n            if (num == 2) {\n                ans[i] = -1;\n                continue;\n            }\n\n            int numCopy = num;\n            int count = 0;\n\n            // Count consecutive 1s from the right\n            while ((num & 1) == 1) {\n                count++;\n                num >>= 1;\n            }\n\n            // [count]th bit is the position of the last 1, like 100111 (count = 3)\n            // Subtract the bit                                     ^\n            ans[i] = numCopy - (1 << (count - 1));\n        }\n\n        return ans;\n    }\n}\n```\n\n``` cpp []\nclass Solution {\npublic:\n    vector<int> minBitwiseArray(vector<int>& nums) {\n        vector<int> ans(nums.size());\n\n        for (int i = 0; i < nums.size(); i++) {\n            int num = nums[i];\n\n            if (num == 2) {\n                ans[i] = -1;\n                continue;\n            }\n\n            int numCopy = num;\n            int count = 0;\n\n            // Count consecutive 1s from the right\n            while ((num & 1) == 1) {\n                count++;\n                num >>= 1;\n            }\n\n            // [count]th bit is the position of the last 1, like 100111 (count = 3)\n            // Subtract the bit                                     ^\n            ans[i] = numCopy - (1 << (count - 1));\n        }\n\n        return ans;\n    }\n};\n\n```\n``` javascript []\nvar minBitwiseArray = function(nums) {\n    let ans = new Array(nums.length);\n\n    for (let i = 0; i < nums.length; i++) {\n        let num = nums[i];\n\n        if (num === 2) {\n            ans[i] = -1;\n            continue;\n        }\n\n        let numCopy = num;\n        let count = 0;\n\n        // Count consecutive 1s from the right\n        while ((num & 1) === 1) {\n            count++;\n            num >>= 1;\n        }\n\n        // [count]th bit is the position of the last 1, like 100111 (count = 3)\n        // Subtract the bit                                     ^\n        ans[i] = numCopy - (1 << (count - 1));\n    }\n\n    return ans;\n};\n\n```\n\n# Complexity\n- Time complexity: $$O(NlogK)$$ where K is the largest number in `nums`\n- Auxiliary space complexity: $$O(1)$$
['Bit Manipulation', 'C++', 'Java', 'Python3', 'JavaScript']