# Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums. Source

package questions

import _utils.UseCommentAsDocumentation
import utils.shouldBe

/**
* The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
* Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums.
* [Source](https://leetcode.com/problems/total-hamming-distance/)
*/
@UseCommentAsDocumentation
private fun totalHammingDistance_NonOptimal(nums: IntArray): Int {
val pairsCountMap = HashMap<Pair<Int, Int>, Int>(nums.size)
for (i in 0..nums.lastIndex) {
for (j in i + 1..nums.lastIndex) {
val first = nums[i]
val second = nums[j]
if (first != second) {
val pair = first to second
val pairRev = second to first
if (pair in pairsCountMap || pairRev in pairsCountMap) { // [4,14] is same as [14,4] so take just 1
pairsCountMap[pair] = pairsCountMap.getOrDefault(pair, 0) + 1
} else {
pairsCountMap[pair] = pairsCountMap.getOrDefault(pair, 0) + 1
}
}
}
}
var sum = 0
pairsCountMap.keys.forEach {
sum += hammingDistance(it.first, it.second) * pairsCountMap[it]!! // multiply it by count ([4,14] and [14,4])
}
return sum
}

private fun hammingDistance(x: Int, y: Int): Int {
var diff = x.xor(y) // xor = 1 when different else 0
if (diff == 0) return 0 // both are same
var count = 0
while (diff > 0) {
if (diff.and(1) == 1) { // diff AND 1 gives LSB
count++ // count all LSB
}
diff = diff.shr(1) // shift [diff] right
}
return count
}

/**
* [Solution](https://leetcode.com/problems/total-hamming-distance/discuss/96226/Java-O(n)-time-O(1)-Space)
* > For each bit position 1-32 in a 32-bit integer, we count the number of integers in the array which have that bit set.
* Then, if there are n integers in the array and k of them have a particular bit set and (n-k) do not, then that bit contributes k*(n-k) hamming distance to the total.
*/
private fun totalHammingDistance(nums: IntArray): Int {
var total = 0
for (j in 0 until 32) {
var bitCount = 0
for (i in 0..nums.lastIndex) {
bitCount += (nums[i].shr(j).and(1))
}
total += bitCount * (nums.size - bitCount)
}
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}