@@ -0,0 +1,1482 @@
+package com.freetymekiyan.algorithms.level.medium;%0A%0A/**%0A * 477. Total Hamming Distance%0A * %3Cp%3E%0A * The Hamming distance between two integers is the number of positions at which the corresponding bits are different.%0A * %3Cp%3E%0A * Now your job is to find the total Hamming distance between all pairs of the given numbers.%0A * %3Cp%3E%0A * Example:%0A * Input: 4, 14, 2%0A * %3Cp%3E%0A * Output: 6%0A * %3Cp%3E%0A * Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just%0A * showing the four bits relevant in this case). So the answer will be:%0A * HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.%0A * Note:%0A * Elements of the given array are in the range of 0 to 10%5E9%0A * Length of the array will not exceed 10%5E4.%0A * %3Cp%3E%0A * Related Topics: Bit Manipulation%0A * Similar Questions: (E) Hamming Distance%0A */%0Apublic class TotalHammingDistance %7B%0A%0A    /**%0A     * Count each bit in the 32-bit ints how many 1s, a and 0s, b.%0A     * The total hamming distance of this bit is then a * b.%0A     * Go through all the 32-bit to get total hamming distance of each.%0A     * Then they add up to the total hamming distance of the numbers.%0A     */%0A    public int totalHammingDistance(int%5B%5D nums) %7B%0A        int d = 0;%0A        for (int i = 0; i %3C 32; i++) %7B%0A            int bitCount = 0;%0A            for (int num : nums) %7B%0A                bitCount += ((num %3E%3E i) & 1);%0A            %7D%0A            d += bitCount * (nums.length - bitCount);%0A        %7D%0A        return d;%0A    %7D%0A%7D

add 477. Total Hamming Distance