LeetCode Problem Workspace

Hamming Distance

Calculate the Hamming distance between two integers by counting differing bit positions.

category

1

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Bit Manipulation-driven solution strategy

bolt

Answer-first summary

Calculate the Hamming distance between two integers by counting differing bit positions.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Bit Manipulation-driven solution strategy

Try AiBox Copilotarrow_forward

The problem asks to calculate the Hamming distance between two integers by identifying differing bit positions. A simple approach leverages bitwise XOR to compare their bits. By counting the number of 1s in the result, we determine the Hamming distance.

Problem Statement

The Hamming distance between two integers is the number of bit positions where the integers differ. For example, given integers 1 and 4, the Hamming distance is 2 because their binary representations differ at two positions.

Write a function that takes two integers, x and y, and returns the Hamming distance between them. The input integers are in the range of 0 to 2^31 - 1.

Examples

Example 1

Input: x = 1, y = 4

Output: 2

1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.

Example 2

Input: x = 3, y = 1

Output: 1

Example details omitted.

Constraints

  • 0 <= x, y <= 231 - 1

Solution Approach

Bitwise XOR

To compute the Hamming distance, use the bitwise XOR operator. This operation compares the bits of the two integers, setting each differing bit to 1. Counting the number of 1s in the result gives the Hamming distance.

Iterative Approach

Another approach is to iteratively check each bit of both integers. For each position, compare the bits and count how many positions differ. This method can be more straightforward but less efficient than using XOR.

Optimization with Brian Kernighan's Algorithm

Brian Kernighan’s algorithm is an optimized technique for counting set bits. By repeatedly flipping the rightmost set bit of the XOR result and counting how many times this operation is performed, you can efficiently calculate the Hamming distance.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity depends on the number of bits in the integers. Using XOR or the iterative method has a time complexity of O(log(max(x, y))). The space complexity is O(1) since we only need a fixed amount of space to perform the calculation.

What Interviewers Usually Probe

  • Look for a candidate’s understanding of bitwise operations like XOR.
  • Check if the candidate can implement bit counting techniques like Brian Kernighan’s algorithm.
  • Assess if the candidate can explain and optimize bit manipulation techniques for clarity and efficiency.

Common Pitfalls or Variants

Common pitfalls

  • Candidates may use inefficient methods such as converting integers to binary strings and counting differences, which is slower.
  • Failure to understand how to properly count set bits in the XOR result can lead to incorrect results.
  • Candidates may overlook optimizations such as Brian Kernighan’s algorithm and default to slower iterative methods.

Follow-up variants

  • What if the integers are represented in a different number of bits?
  • How would you modify your approach for signed integers?
  • Can you optimize further for large input ranges?

FAQ

What is the Hamming distance?

The Hamming distance is the number of positions at which the corresponding bits of two integers differ.

How can I solve this problem using bitwise operations?

You can solve this problem using the XOR operator, which compares bits and sets differing bits to 1. Counting the number of 1s in the result gives the Hamming distance.

How do I optimize my solution for larger inputs?

Optimizing the solution involves using efficient algorithms like Brian Kernighan’s algorithm to count set bits in the XOR result, reducing the number of operations.

What are some common mistakes to avoid in this problem?

One common mistake is using a method like converting numbers to binary strings, which is less efficient than bitwise manipulation. Another mistake is not counting the 1s in the XOR result correctly.

Why is the XOR operation useful for Hamming distance?

The XOR operation highlights the positions where two integers differ by setting those bits to 1. This makes it easy to count the differences by simply counting the 1s in the XOR result.

terminal

Solution

Solution 1

#### Python3

1
2
3
class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        return (x ^ y).bit_count()
Hamming Distance Solution: Bit Manipulation-driven solution stra… | LeetCode #461 Easy