Simple Java || C++ Code ☠️
Code\njava []\nclass Solution {\n    public int[] minBitwiseArray(List<Integer> nums) {\n        int n = nums.size();\n        int[] ans = new int[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 i=0;i<n;i++){\n            int a = nums.get(i);\n            for(int j=0;j<a;j++){\n                if((j | (j+1)) == a){\n                    ans[i] = j;\n                    break;\n                }\n                if(j == a-1){\n                    ans[i] = -1;\n                }     \n            }\n        }\n        return ans;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    vector<int> minBitwiseArray(vector<int>& nums) {\n        int n = nums.size();\n        for(int i=0;i<n;i++){\n            int a = nums[i];\n            for(int j=0;j<a;j++){\n                if((j | (j+1)) == a){\n                    nums[i] = j;\n                    break;\n                }\n                if(j == a-1){\n                    nums[i] = -1;\n                }     \n            }\n        }\n        return nums;\n    }\n};\n```\n\n\n\n###  Explanation:\n\n\n\n1. **Initialization:**\n   - You initialize the length `n` of the input list `nums` using `nums.size()`.\n   - You create an integer array `ans` of size `n` to store the result for each number in the list.\n\n2. **Outer Loop:**\n   - The outer loop iterates over each prime number in `nums`. For each prime number `a`, you attempt to find the smallest `j` such that the bitwise OR of `j` and `j + 1` equals `a`.\n\n3. **Inner Loop (Finding `j`):**\n   - For each prime number `a`, you loop through all possible values of `j` from `0` to `a - 1`. The goal is to find the smallest `j` such that:\n     ```\n     j | (j + 1) == a\n     ```\n     - `j | (j + 1)` performs a bitwise OR between `j` and `j + 1`.\n     - If this condition is satisfied, you store the value of `j` in `ans[i]` and immediately break out of the loop since you\'ve found the smallest `j`.\n\n4. **Handling Edge Cases (No Valid `j`):**\n   - If you finish the inner loop without finding any valid `j`, meaning no value of `j` satisfies `j | (j + 1) == a`, then you set `ans[i]` to `-1`.\n   - This ensures that when it\'s not possible to construct such a value, the result reflects that.\n\n5. **Return the Result:**\n   - After processing all the prime numbers in `nums`, you return the `ans` array, which contains the smallest valid `j` for each prime number, or `-1` if no such `j` exists.\n\n#### Example Walkthrough:\n\n\n**Input:**\n```java\nnums = [5, 7, 11]\n```\n\n- **For `nums[0] = 5`:**\n  - We iterate over `j = 0, 1, 2, 3, 4`:\n    - `0 | (0 + 1) = 0 | 1 = 1`\n    - `1 | (1 + 1) = 1 | 2 = 3`\n    - `2 | (2 + 1) = 2 | 3 = 3`\n    - `3 | (3 + 1) = 3 | 4 = 7`\n    - **When `j = 4`:**\n      - `4 | (4 + 1) = 4 | 5 = 5` (Condition satisfied)\n      - So, `ans[0] = 4`.\n\n- **For `nums[1] = 7`:**\n  - We iterate over `j = 0, 1, 2, 3, 4, 5, 6`:\n    - `0 | (0 + 1) = 1`\n    - `1 | (1 + 1) = 3`\n    - `2 | (2 + 1) = 3`\n    - `3 | (3 + 1) = 7` (Condition satisfied)\n    - So, `ans[1] = 3`.\n\n- **For `nums[2] = 11`:**\n  - We iterate over `j = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10`:\n    - For each `j`, we do not find any value that satisfies `j | (j + 1) = 11`.\n    - Therefore, `ans[2] = -1`.\n\n**Output:**\n```java\nans = [4, 3, -1]\n```\n\n### Time Complexity: **O(n * a)**\n\n### Space Complexity: \n- **O(n)** (for Java code) \n- **O(1)** (for C++ code)
['C++', 'Java']