Easy  linear O(n*31) solution ---->
Understanding the Problem:\n1. For each number in the array nums, you want to generate a new number that is the "minimum" possible based on certain bitwise oper
# Understanding the Problem:\n1. For each number in the array nums, you want to generate a new number that is the "minimum" possible based on certain bitwise operations.\n\n2. The exact property you\'re trying to maintain here is that if you modify a bit, you want the new number (newnum) to still be closely related to the original number num, specifically such that when you OR it with newnum + 1, it should give back the original number (num).\n\n# In other words:\n\n1. Key Insight: We are trying to find a way to unset one bit of the number while keeping a specific relationship with the original number intact.\n#  Bitwise Thinking:\nTo develop this approach, let\'s think about how numbers work in binary:\n\n1. Each bit in a number represents a power of 2.\n2. By unsetting (turning off) a bit, you are effectively subtracting that power of 2 from the number.\n3. The goal is to find a way to unset a bit and create a new number that behaves predictably when you apply the bitwise OR with (newnum + 1).\n# Step-by-Step Approach:\n1. Loop Through Each Number:\nFor every number in nums, perform the following:\n\n![Screenshot 2024-10-12 223548.png](https://assets.leetcode.com/users/images/d5b094ec-e848-451b-9313-f8aba70fd80e_1728752761.2008612.png)\n\n\n2. You take each number (num) and initialize a flag to track whether you found a valid new number.\n\n![Screenshot 2024-10-12 223405.png](https://assets.leetcode.com/users/images/1fa592a3-cd32-4484-995e-5ddfabc8043f_1728752657.282967.png)\n\n3. You iterate through all the bits of num, starting from the most significant bit (bit 31, for a 32-bit integer).\n4. You check if the bit at position j is set (1), using (num >> j) & if it\'s set, this means you are allowed to try unsetting this bit.\nnewnum is created by unsetting the bit at position j using (num & ~(1 << ((j+1)-1))). This operation clears the bit at position j.\n\n![Screenshot 2024-10-12 223521.png](https://assets.leetcode.com/users/images/21ee6c4e-4838-48fa-aca9-13369a4511b6_1728752729.5552988.png)\n\n\n5. After unsetting the bit, you check if this newnum satisfies the condition (newnum | (newnum + 1)) == num.\n6. This condition ensures that when you OR newnum with newnum + 1, you should get back the original number num. This helps maintain some structural similarity with the original number.\n7. If the condition is satisfied, newnum is valid, and you store it in ans and set the flag to true, indicating success for this number.\nYou then break out of the loop since you have already found the "minimum" number for this element.\n\n8. If no valid newnum is found (i.e., if flag remains false after trying all possible bit manipulations), you append -1 to the answer. This implies there\u2019s no valid number that satisfies the condition for that particular num.\n\n# Return the Result:\n1. Finally, after processing all numbers, the function returns the ans array, which contains the results of the bit manipulations for each number.\n\n# Why This Approach?\n1. Bitwise Manipulation: This approach leverages bitwise operations to explore different possibilities of "unsetting" bits in an attempt to minimize the number while maintaining a key property.\n2. Efficiency: By iterating through the bits starting from the most significant, it quickly finds a valid solution for each number (if it exists), thus avoiding unnecessary computations.\n\n# Complexity\n- Time complexity:\nO(N*31) as one outer loop and o(31) for traversing on bits\n\n- Space complexity:\nO(N)\n\n# code\n```C++ []\nclass Solution {\npublic:\n    vector<int> minBitwiseArray(vector<int>& nums) {\n        \n        int n=nums.size();\n        vector<int>ans;\n        \n        for(int i=0;i<n;i++){\n\n            bool flag=false;\n            int num=nums[i];\n            for(int j=31;j>=0;j--){\n\n                if((num>>j)&1){\n                   int newnum=(num&~(1<<((j+1)-1)));\n                  if((newnum|(newnum+1))==num){\n                    ans.push_back(newnum);\n                    flag=true;\n                    break;\n                }\n                }\n            }\n            if(flag==false){\n                ans.push_back(-1);\n            }\n        }\n        return ans;\n    }\n};\n```\n```java []\nimport java.util.*;\n\nclass Solution {\n    public List<Integer> minBitwiseArray(int[] nums) {\n        List<Integer> ans = new ArrayList<>();\n        int n = nums.length;\n\n        for (int i = 0; i < n; i++) {\n            boolean flag = false;\n            int num = nums[i];\n\n            for (int j = 31; j >= 0; j--) {\n                if ((num >> j & 1) == 1) {\n                    int newnum = (num & ~(1 << ((j + 1) - 1)));\n                    if ((newnum | (newnum + 1)) == num) {\n                        ans.add(newnum);\n                        flag = true;\n                        break;\n                    }\n                }\n            }\n            if (!flag) {\n                ans.add(-1);\n            }\n        }\n        return ans;\n    }\n\n\n```\n```python []\nclass Solution:\n    def minBitwiseArray(self, nums):\n        ans = []\n        n = len(nums)\n\n        for num in nums:\n            flag = False\n            for j in range(31, -1, -1):\n                if (num >> j) & 1:\n                    newnum = (num & ~(1 << ((j + 1) - 1)))\n                    if (newnum | (newnum + 1)) == num:\n                        ans.append(newnum)\n                        flag = True\n                        break\n            if not flag:\n                ans.append(-1)\n        \n        return ans\n```\n```javascript []\nclass Solution {\n    minBitwiseArray(nums) {\n        let ans = [];\n        let n = nums.length;\n\n        for (let i = 0; i < n; i++) {\n            let flag = false;\n            let num = nums[i];\n\n            for (let j = 31; j >= 0; j--) {\n                if ((num >> j) & 1) {\n                    let newnum = (num & ~(1 << ((j + 1) - 1)));\n                    if ((newnum | (newnum + 1)) === num) {\n                        ans.push(newnum);\n                        flag = true;\n                        break;\n                    }\n                }\n            }\n            if (!flag) {\n                ans.push(-1);\n            }\n        }\n\n        return ans;\n    }\n}\n```\n![c2d27b08-ffbe-4bc6-8b29-5fc4d8effd5f_1723775276.9029489.jpeg](https://assets.leetcode.com/users/images/59c07e9a-30ec-4180-b13f-2e4219f2e2cf_1728753173.5884457.jpeg)\n
['Array', 'Bit Manipulation', 'Bitmask', 'C++', 'Java', 'Python3', 'JavaScript']