Advertisement

Number of Bit Changes to Make Two Integers Equal - LeetCode 3226 Solution

Number of Bit Changes to Make Two Integers Equal - Complete Solution Guide

Number of Bit Changes to Make Two Integers Equal is LeetCode problem 3226, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two positive integers n and k . You can choose any bit in the binary representation of n that is equal to 1 and change it to 0. Return the number of changes needed to make n equal to k . If it is impossible, return -1. Example 1: Input: n = 13, k = 4 Output: 2 Explanation: Initially, the binary representations of n and k are n = (1101) 2 and k = (0100) 2 . We can change the first and fourth bits of n . The resulting integer is n = ( 0 10 0 ) 2 = k . Example 2: Input: n = 21, k = 21

Detailed Explanation

The problem asks you to find the minimum number of bit changes required to transform a positive integer `n` into another positive integer `k`. The only allowed operation is changing a '1' bit in `n` to a '0' bit. If it's impossible to make `n` equal to `k` using this operation, the function should return -1. The input consists of two positive integers, `n` and `k`, and the output is a non-negative integer representing the number of changes or -1 if it's impossible.

Solution Approach

The provided solutions utilize bit manipulation to efficiently determine the minimum number of changes. They iterate through the bits of `n` and `k` (or use bitwise operations on `n ^ k`) and count the number of times a '1' bit in `n` needs to be changed to a '0' bit to match `k`. If a '0' in `n` needs to become a '1' in `k`, it's impossible, and -1 is returned. The Python solution converts to binary strings for clarity, while the Java, C++, and C solutions use bitwise operations for efficiency.

Step-by-Step Algorithm

  1. Step 1: Calculate `n ^ k` (XOR of n and k). This highlights bits that differ between n and k.
  2. Step 2: Iterate through the bits of `n ^ k` (or the bits of n and k simultaneously).
  3. Step 3: For each bit, if it's 1 in `n ^ k`, check if the corresponding bit in `n` is 1. If so, increment the change counter. If the corresponding bit in `n` is 0, it's impossible to achieve `k` from `n`, so return -1.
  4. Step 4: If the loop completes without returning -1, return the change counter.

Key Insights

  • Insight 1: The problem can be efficiently solved using bit manipulation. Comparing the bits of `n` and `k` directly allows for a straightforward approach.
  • Insight 2: The XOR operation (`^`) is crucial. `n ^ k` gives a number where each bit is 1 if the corresponding bits in `n` and `k` are different and 0 if they are the same. This efficiently identifies the bits that need changing.
  • Insight 3: It's impossible to transform `n` into `k` if any bit is 1 in `k` but 0 in `n`. This condition must be checked to correctly handle cases where transformation is not possible.

Complexity Analysis

Time Complexity: O(log(n))

Space Complexity: O(log(n))

Topics

This problem involves: Bit Manipulation.

Companies

Asked at: ThoughtWorks.