Simple Java Code ☠️
Code\n\nclass Solution {\n    public int minChanges(int n, int k) {\n        if(k>n){\n            return -1;\n        }\n        int count = 0;\n        String
# Code\n```\nclass Solution {\n    public int minChanges(int n, int k) {\n        if(k>n){\n            return -1;\n        }\n        int count = 0;\n        StringBuilder strn = new StringBuilder(Integer.toBinaryString(n));\n        StringBuilder strk = new StringBuilder(Integer.toBinaryString(k));\n        int i=strn.length()-1,j=strk.length()-1;\n        while(i>=0 && j>=0){\n            if(strn.charAt(i)==strk.charAt(j)){\n                i--;j--;\n            }\n            else if(strn.charAt(i)==\'1\' && strk.charAt(j)==\'0\'){\n                count++;\n                i--;j--;\n            }\n            else{\n                return -1;\n            }\n        }\n        while(i>=0){\n            if(strn.charAt(i)==\'1\'){\n                count++;\n            }\n            i--;\n        }\n        return count;\n    }\n}\n```\n\n\n### Explanation\n\n1. **Initial Check:** \n   - If `k` is greater than `n`, it\'s impossible to achieve `k` from `n`, so return `-1`.\n\n2. **Convert to Binary:**\n   - Convert both `n` and `k` to their binary string representations.\n\n3. **Initialize Pointers:**\n   - Use two pointers `i` and `j` starting from the end of `strn` and `strk`.\n\n4. **Compare Bits:**\n   - While both pointers are within range:\n     - If the bits are equal, move both pointers left.\n     - If `strn[i]` is `1` and `strk[j]` is `0`, count this as a change and move both pointers.\n     - If `strn[i]` is `0` and `strk[j]` is `1`, return `-1` as it\u2019s impossible to make `n` into `k`.\n\n5. **Process Remaining Bits of `n`:**\n   - If there are remaining `1`s in `n` (after `strk` is exhausted), count these as changes.\n\n6. **Return Count:**\n   - Return the total count of changes required.\n\n\n\n### Example\nSuppose `n = 29` and `k = 13`.\n\n#### Binary Representation\n- `n = 29` in binary is `11101`\n- `k = 13` in binary is `1101`\n\n### Steps\n\n1. **Initial Check:**\n   - `k` is not greater than `n`, so continue.\n\n2. **Convert to Binary:**\n   - `strn = "11101"`\n   - `strk = "1101"`\n\n3. **Initialize Pointers:**\n   - Start with `i = 4` (end of `strn`) and `j = 3` (end of `strk`).\n\n4. **Compare Bits:**\n   - Compare `strn[4]` (`1`) and `strk[3]` (`1`): equal, move `i` to 3 and `j` to 2.\n   - Compare `strn[3]` (`0`) and `strk[2]` (`0`): equal, move `i` to 2 and `j` to 1.\n   - Compare `strn[2]` (`1`) and `strk[1]` (`1`): equal, move `i` to 1 and `j` to 0.\n   - Compare `strn[1]` (`1`) and `strk[0]` (`1`): equal, move `i` to 0 and `j` to -1.\n\n5. **Remaining Bits of `n`:**\n   - Now, `j` is out of range, but `i = 0`.\n   - `strn[0]` is `1`, so we need to change this to `0`.\n   - Increment `count`.\n\n6. **Return Count:**\n   - Total `count` is `1`.\n\n\n\n### Complexity\n\n- **Time Complexity:** O(max(log n, log k)), because you compare each bit from the binary strings.\n- **Space Complexity:** O(log n + log k), for storing the binary representations as strings.\n\n\n\n
['String', 'Counting', 'Java']