C++, bit mask, and I tried to explain
Basics -\n1. All prime numbers are odd except 2.\n2. 2 does not have any solution. Push back -1 for 2.\n3. All odd numbers will always have atleast one solution
# Basics -\n1. All prime numbers are odd except 2.\n2. 2 does not have any solution. Push back -1 for 2.\n3. All odd numbers will always have atleast one solution: (x BITWISEOR (x-1)) = x. This works because all odd numbers have lowest bit 1 and the number just before that will be the same number but wit lowest bit as 0. Example 7 -> 111, 7-1=6 -> 110\n\n# Intuition\nI was able to get the idea after my intial solution failed for test case [47] (answer is 39)\n\n47 -> 10`1`111\n\n39 -> 100111\n40 -> 101000\n\nHere we unset the highest set bit next to first unset bit (from right). In this case first unset bit is the 4th idx from right. So 3rd index (set bit) needs to be unset.\n\nWhy does this work?\n\nFirst we lose 1 bit from our original number 47.\n\nThen we get the same bit back when we do a +1 to 39.\n\n40 has all the same bits as 47 except the lower bits which are preserverd in 37.\n\nWe are esentially dividing the whole binary string in 2 parts - left and right of 1st zero (first 0 from right). \n\nThe lower bits are stored in the smaller number and when we add +1 to a number in binary, all lower set bits become unset and first unset bit from right becomes set, so the bigger number has all higher bits (including the one we unset).\n\nEx: 7 + 1 = 8\n7 ->0111\n1 -> 0001\n8 -> 1000\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> minBitwiseArray(vector<int>& arr) {\n        int n = arr.size();\n\n        vector<int> res;\n\n        for(int curr : arr){\n            if(curr == 2){\n                res.push_back(-1);\n                continue;\n            }\n\n            int mask = 1;\n\n            for(int i = 0; i < 31; i++){\n                if((curr & (1 << i)) == 0){\n                    // found first 0, use mask from last set bit\n                    // assume mask = 00100\n                    // then ~mask becomes 11011\n                    // this way we unset only one bit in curr\n                    int ans = (curr & (~mask));\n                    res.push_back(ans);\n                    break;\n                }\n\n                // keep creating the mask until we find first 0\n                mask = (1 << i);\n            }\n\n        }\n\n        return res;\n    }\n};\n```
['Bit Manipulation', 'Bitmask', 'C++']