category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

461. Hamming Distance

In layman terms hammering distance is the no. of times when both the no.s had different bits.

  1: 0 0 0 1
  4: 0 1 0 0

there were 2 places where both bits were different.

XOR method

Implementation ```cpp int hammingDistance(int x, int y) { int temp = x ^ y; int ans = 0; while (temp) { ans += (temp & 1); temp = temp >> 1; } return ans; } ```

Brian-Kernighan algo

Implementation ```java class Solution { public int hammingDistance(int x, int y) { int n = x ^ y; int count = 0; while (n > 0) { count++; int rsb = n & (-n); n &= ~(rsb); // subtracting rsb from original no, n -= rsb will also do. } return count; } } ```