package com.fishercoder.solutions;

/**
 * The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

 Now your job is to find the total Hamming distance between all pairs of the given numbers.

 Example:
 Input: 4, 14, 2

 Output: 6

 Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
 showing the four bits relevant in this case). So the answer will be:
 HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
 Note:
 Elements of the given array are in the range of 0 to 10^9
 Length of the array will not exceed 10^4.
 */
public class _477 {

	public int totalHammingDistance(int[] nums) {
		int r = 0;
		for (int i = 0; i < 32; i++) {
			int one = 0;
			int zero = 0;
			int bit = 1 << i;
			for (int n : nums) {
				if ((n & bit) != 0) {
					one++;
				} else {
					zero++;
				}
			}
			r += one * zero;
		}
		return r;
	}

}

@@ -33,16 +33,50 @@
 s;%0A%0A/**%0A
+ * 477. Total Hamming Distance%0A *%0A
  * The H
@@ -307,17 +307,16 @@
 , 14, 2%0A
-%0A
  Output:
@@ -318,17 +318,16 @@
 tput: 6%0A
-%0A
  Explana
@@ -565,16 +565,17 @@
  2 = 6.%0A
+%0A
  Note:%0A 
@@ -699,17 +699,50 @@
  _477 %7B%0A
-%0A
+%09public static class Solution1 %7B%0A%09
 %09public 
@@ -782,16 +782,17 @@
 ms) %7B%0A%09%09
+%09
 int r = 
@@ -794,16 +794,17 @@
  r = 0;%0A
+%09
 %09%09for (i
@@ -824,24 +824,25 @@
  32; i++) %7B%0A
+%09
 %09%09%09int one =
@@ -844,24 +844,25 @@
 one = 0;%0A%09%09%09
+%09
 int zero = 0
@@ -863,16 +863,17 @@
 ro = 0;%0A
+%09
 %09%09%09int b
@@ -885,16 +885,17 @@
 1 %3C%3C i;%0A
+%09
 %09%09%09for (
@@ -914,16 +914,17 @@
 ) %7B%0A%09%09%09%09
+%09
 if ((n &
@@ -946,15 +946,17 @@
 %09%09%09%09
+%09
 one++;%0A
+%09
 %09%09%09%09
@@ -964,16 +964,17 @@
  else %7B%0A
+%09
 %09%09%09%09%09zer
@@ -986,15 +986,18 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%7D%0A
+%09
 %09%09%09r
@@ -1014,18 +1014,20 @@
 zero;%0A%09%09
+%09
 %7D%0A
+%09
 %09%09return
@@ -1030,14 +1030,18 @@
 turn r;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

refactor 477