Java Solution using in-built method with Constant Time & Space ✅💯
Intuition1. Binary String Conversion:
We first convert both the integers n and k to their 32-bit binary representation. In Java, integers are typically represen
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
**1. Binary String Conversion:**
- We first convert both the integers n and k to their 32-bit binary representation. In Java, integers are typically represented with 32 bits.
- To ensure that both n and k are represented with exactly 32 bits, the following code is used:
- String s1 = String.format("%32s", Integer.toBinaryString(n)).replaceAll(" ", "0");
- String s2 = String.format("%32s", Integer.toBinaryString(k)).replaceAll(" ", "0");
- Integer.toBinaryString(n) converts the integer n to a binary string.
- String.format("%32s", ...) ensures the binary string is padded to 32 characters (if it's shorter).
- replaceAll(" ", "0") replaces any leading spaces with zeros, ensuring the string is exactly 32 characters long.

**2. Initial Count Setup:**
- A counter count is initialized to 0. This counter will track the number of bit flips (changes from 1 to 0 or 0 to 1) required to convert n to k.

**3. Iterating Over Each Bit:**
- The loop runs from i = 0 to i < 32 (since we're working with 32 bits).
- In each iteration, it compares the corresponding bits of n and k (i.e., s1.charAt(i) and s2.charAt(i)).

**4. Condition Checking:**
- If s1.charAt(i) == '0' && s2.charAt(i) == '1', this means that n has a 0 at this position, but k has a 1. In this case, it is impossible to convert n to k because we cannot turn a 0 into a 1 without additional constraints (e.g., using some operations like bitwise operations). So, the function returns -1 immediately to indicate an impossible transformation.
- If s1.charAt(i) == '1' && s2.charAt(i) == '0', it means that n has a 1 at this position, but k has a 0. This is a valid change because we can flip 1 to 0. The counter count is incremented to track this required bit flip.

**5. Returning the Result:**
- After the loop completes, the function returns the value of count, which represents the number of bit flips required to change n to k.

# Approach
<!-- Describe your approach to solving the problem. -->
**1. Binary String Conversion:**
- We first convert both the integers n and k to their 32-bit binary representation. In Java, integers are typically represented with 32 bits.
- To ensure that both n and k are represented with exactly 32 bits, the following code is used:
- String s1 = String.format("%32s", Integer.toBinaryString(n)).replaceAll(" ", "0");
- String s2 = String.format("%32s", Integer.toBinaryString(k)).replaceAll(" ", "0");
- Integer.toBinaryString(n) converts the integer n to a binary string.
- String.format("%32s", ...) ensures the binary string is padded to 32 characters (if it's shorter).
- replaceAll(" ", "0") replaces any leading spaces with zeros, ensuring the string is exactly 32 characters long.

**2. Initial Count Setup:**
- A counter count is initialized to 0. This counter will track the number of bit flips (changes from 1 to 0 or 0 to 1) required to convert n to k.

**3. Iterating Over Each Bit:**
- The loop runs from i = 0 to i < 32 (since we're working with 32 bits).
- In each iteration, it compares the corresponding bits of n and k (i.e., s1.charAt(i) and s2.charAt(i)).

**4. Condition Checking:**
- If s1.charAt(i) == '0' && s2.charAt(i) == '1', this means that n has a 0 at this position, but k has a 1. In this case, it is impossible to convert n to k because we cannot turn a 0 into a 1 without additional constraints (e.g., using some operations like bitwise operations). So, the function returns -1 immediately to indicate an impossible transformation.
- If s1.charAt(i) == '1' && s2.charAt(i) == '0', it means that n has a 1 at this position, but k has a 0. This is a valid change because we can flip 1 to 0. The counter count is incremented to track this required bit flip.

**5. Returning the Result:**
- After the loop completes, the function returns the value of count, which represents the number of bit flips required to change n to k.

# Complexity
- Time complexity: O(1)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int minChanges(int n, int k) {
        String s1 = String.format("%32s", Integer.toBinaryString(n)).replaceAll(" ", "0");
        String s2 = String.format("%32s", Integer.toBinaryString(k)).replaceAll(" ", "0");
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if (s1.charAt(i) == '0' && s2.charAt(i) == '1') {
                return -1;
            } else if (s1.charAt(i) == '1' && s2.charAt(i) == '0') {
                count++;
            }
        }
        return count;
    }
}
```
['Bit Manipulation', 'Java']