String & Array Logic | Beats 100% in Time and Space |
Tried to make it begineer friendly by only using bitwise AND for the base case, rest is all using Strings and Arrays.\n\n\n\nclass Solution {\n    public int mi
Tried to make it begineer friendly by **only using bitwise AND for the base case**, rest is all using Strings and Arrays.\n![image](https://assets.leetcode.com/users/images/598077bc-7fbb-4531-948d-65a15d66fbb5_1721564764.9214995.png)\n\n```\nclass Solution {\n    public int minChanges(int n, int k) {    \n        \n\t\t// Base case if we are unable to convert N into K\n        if ((n & k) != k){\n            return -1;\n        }\n        \n        String binaryN = Integer.toBinaryString(n);\n        String binaryK = Integer.toBinaryString(k);\n        \n        int maxLength = Math.max(binaryN.length(), binaryK.length());\n\n\t\t// If length of binary K is less than N, add preceeding 0s to make them equal.\n        while (binaryK.length() < maxLength) {\n            binaryK = "0" + binaryK;\n        }\n        \n        char[] nArray = binaryN.toCharArray();\n        char[] kArray = binaryK.toCharArray();\n        \n        int count = 0;\n        \n\t\t// Increase count whenever binary representation of N is 1 and K is 0\n        for (int i = 0; i < nArray.length; i++) {\n            if (nArray[i] != kArray[i] && nArray[i]==\'1\') {\n                count++;\n            }\n        }\n        return count;\n    }\n}\n```
['Array', 'String', 'Java']