Explained - Bit wise manipulation || Very simple and easy understand
\n# Intuition \n- By analysing few cases I observed following:\n  - as all are prime except 2, all numbers are odd ( so the last bit is set)\n  - as all numbers
\n# Intuition \n- By analysing few cases I observed following:\n  - as all are prime except 2, all numbers are odd ( so the last bit is set)\n  - as all numbers are odd => trivial ans is n-1. Where n-1 one has last bit set to zero and (n-1) | n will be always eqaul to n\n  - Now we need to optimise further to find  a smaller number that satisfy given condition\n  - if all bits are not set then the highest bit must be set in the answer as well.( the answer must be number whose largest bit is same as that of given number)\n\n- Finally concluded with the following two cases :\n  1. when all bits are one (that is the number is in the form n^2 -1 ) => then answer will be n/2\n  2. when all bits are not one => \n      - then in the answer the highest bit must be set( Need to find the largest number less than current number which is a power of 2 and add this value to answer)\n      - then set the highest bit to zero. ( subtract largest number less than current number which is smaller than 2)\n      Other way of thinking this is => \n      \nLets take an example of n = 13 ( 1101) and initialize ans with 0\nNow n is not a number whose all bits are set, so we remove the highest bit and the same is added to ans temporary variable.\nor you can think up lik find the largest number which is power of 2 and less than n (that is 8 here ) and subtract this from the number ( this will set the higest bit zero) and add this number to the ans as well.\nSo now n = 13 - 8 = 5 and ans = 0 + 8 = 8\n\nIn next iteration n = 5 is again not in the form which has all bits set, so we repeat above steps\nfind the largest power of 2 which is smaller than n = 5 that is 4\nNow subtract 4 from n and add it to the ans => n = 5 - 4 = 1 and ans = 8 + 4 = 12\n\nIn next iteration we find that n = 1 is in the form where all the bits are set. So we add n/2 to ans and set n to zero\nn = 0 and ans = 12 + 1/2 = 12\n      \nHence here our answer is 12\n\n# Approach \n- Iterate over each number in the nums\n- if n is equal to 2 which is a even number and we will consider a special case here. So return the answer as -1\n- if not equal to 2 then do the following steps:\n - initialize temp answer sum to zero and copy value of n to a temp var t\n - now if t is greater than 0 then repeat :\n   - if it is in the form where all bits are set(or we can check if n+1 is a power of 2 or not) then sum qill be equal to t/2 and set t to zero\n   - if all bits are not set then find the largest number which is power of 2 and less than t. Add the number to sum and subtract it from t\n\n# Code\n```\nclass Solution {\npublic:\n    bool isPowerOfTwo(int n) {\n        return ((n & (n - 1)) == 0);\n    }\n    \n    int largestPowerOfTwoLessThan(int n) {\n        if (n <= 1) return 0;\n        int exponent = floor(log2(n));\n        return 1 << exponent; // Equivalent to pow(2, exponent)\n    }\n    \n    vector<int> minBitwiseArray(vector<int>& nums) {\n        vector<int> ans;\n        for(auto n: nums){\n            if(n == 2) ans.push_back(-1);\n            else{\n                int t = n, sum = 0;\n                while(t > 0){\n                    if(isPowerOfTwo(t+1)){\n                        sum += t/2;\n                        t = 0;\n                    }else{\n                        int d = largestPowerOfTwoLessThan(t);\n                        sum += d;\n                        t -= d;\n                    }\n                }\n                ans.push_back(sum);\n            }\n        }\n        return ans;\n    }\n};\n```\n\n---\n\n\n<b>Here is an article of my interview experience - A Journey to FAANG Company, I recomand you to go through this to know which all resources I have used & how I cracked interview at Amazon:\nhttps://leetcode.com/discuss/interview-experience/3171859/Journey-to-a-FAANG-Company-Amazon-or-SDE2-(L5)-or-Bangalore-or-Oct-2022-Accepted\n\n---
['C++']