Easy to Understand , Bits 100% 💪
Intuition\n\n\nLet\u2019s dive into solving this problem with a clear and engaging explanation!\n\n## Problem Description\n\nYou\u2019re given two positive inte
# Intuition\n\n\nLet\u2019s dive into solving this problem with a clear and engaging explanation!\n\n## Problem Description\n\nYou\u2019re given two positive integers, `n` and `k`. The goal is to transform `n` into `k` by flipping some of the `1s` in `n` to `0`s. Your task is to determine how many changes are needed, or return `-1` if it\u2019s impossible to make `n` equal to `k` by only changing `1s` in `n` to `0`s.\n\n## Approach\n\n1. **Identify Necessary Changes :**\n   - To determine how many bit changes are needed, you need to compare the binary representations of `n` and `k`. Specifically, you need to determine which bits in `n` need to be flipped to match `k`.\n\n2. **Calculate Differences :**\n   - Use XOR to find the bit positions where `n` and `k` differ. This is because XOR (`^`) will give you a result where each bit set to `1` represents a position where `n` and `k` have different bits.\n\n3. **Check Feasibility and Count Changes :**\n   - For each bit position that is different (i.e., where XOR result is `1`):\n     - **If `n` has a `0` at this position and `k` has a `1`**,mean you cannot make `n` equal to `k` by flipping only `1s` bit of `n`\n     - **If `n` has a `1` at this position and `k` has a `0`**,mean you need to flip this `1` to `0`. Count these necessary flips.\n\n4. **Return the Result :**\n   - After processing all bit positions, return the count of flips needed. If an impossible situation is encountered, return `-1`.\n\n## Detailed Approach\n\n1. **Compute XOR :**\n   - Compute the XOR of `n` and `k`. The result, `_xor = n ^ k`, highlights the bits where `n` and `k` differ.\n\n2. **Count Flips :**\n   - Initialize `count` to `0`.\n   - Iterate through each bit of `_xor`:\n     - If `_xor & 1` (i.e., the bit in XOR result is `1`):\n       - **Check the corresponding bits in `n` and `k`:**\n         - **If `n` has `0` and `k` has `1`** at this position, return `-1` because it\u2019s impossible to flip bits in `n` to achieve this.\n         - **If `n` has `1` and `k` has `0`**, increment `count` because you need to flip this `1` in `n`.\n\n3. **Shift Bits :**\n   - Right shift `n`, `k`, and `_xor` to process the next bit.\n\n4. **Return Result :**\n   - Return the total count of flips.\n\nCertainly! Let\u2019s walk through another example to demonstrate the algorithm.\n\n## Complexity\n\n- **Time Complexity :** O(32) \n  \n- **Space Complexity :** O(1)\n\n# Code\n```cpp []\nclass Solution\n{\npublic:\n    int minChanges(int n, int k)\n    {\n        if (n == k)\n            return 0;\n\n        int _xor = n ^ k;\n\n        int count = 0;\n\n        while (_xor)\n        {\n            if (!(n & 1) && (k & 1) && (_xor & 1))\n                return -1;\n            else if ((n & 1) && !(k & 1) && (_xor & 1))\n                count++;\n\n            n >>= 1;\n            k >>= 1;\n            _xor >>= 1;\n        }\n\n        return count;\n    }\n};\n```
['C++']