Easy Solution | Explained with Example walk through | Simple logic
Approach\n- Loop through the input list of numbers:\nFor each number in the list nums, we call the function minval(num) to find the smallest possible i for that
# Approach\n- Loop through the input list of numbers:\nFor each number in the list nums, we call the function minval(num) to find the smallest possible i for that number.\n\n- In the minval function:\nWe iterate through each bit of num from the most significant bit (MSB) to the least significant bit (LSB).\nFor each bit, we flip that bit (change 1 to 0 or 0 to 1) and check if the modified number satisfies the condition (i\u2223(i+1))==num.\nIf we find such a number i, we return it.\nIf no such number is found after trying all bits, return -1.\n\n# Example\nNumber: 11\nBinary of 11: 1011.\n\nWe will now try to flip each bit of 11 and check if the result satisfies the condition.\n\nFlip the 3rd bit (MSB):\nFlip 1011 \u2192 0011 (3 in decimal).\nCheck: \n(3\u2223(3+1))==11?\n3\u22234=7, which is not equal to 11. So, this doesn\'t work.\n\nFlip the 2nd bit:\nFlip 1011 \u2192 1111 (15 in decimal).\nCheck: \n(15\u2223(15+1))==11?\n15\u222316=31, which is not equal to 11. So, this doesn\'t work.\n\nFlip the 1st bit:\nFlip 1011 \u2192 1001 (9 in decimal).\nCheck: \n(9\u2223(9+1))==11?\n9\u222310=11, which is equal to 11! This works.\n\nSo, the smallest number for 11 is 9.\n\n\nFinal Result:\nFor the input list [11, 13, 31], the smallest values are:\n\n11 \u2192 9\n\nSimilarly all for all other numbers in array.\n\n# Complexity\n- Time complexity: $$O(n)$$\nwhere n is the number of bits in the number\n\n### Upvote if this solution is good \u2B06\uFE0F\n\n\n# Code\n```java []\nclass Solution {\n    public int[] minBitwiseArray(List<Integer> nums) {\n        int n = nums.size();\n        int[] ans = new int[n];\n        for (int j = 0; j < n; j++) {\n            int num = nums.get(j);\n            ans[j] = minval(num);\n        }\n\n        return ans;\n    }\n\n    private int minval(int num) {\n        int og = num;\n        int len = Integer.SIZE - Integer.numberOfLeadingZeros(num);\n        \n        for (int i = len - 1; i >= 0; i--) {\n            int changed = num ^ (1 << i);\n            if ((changed | (changed + 1)) == og) {\n                return changed; \n            }\n        }\n\n        return -1; \n    }\n\n    \n}\n\n```
['Bit Manipulation', 'Bitmask', 'Java']