# Is there more efficient way to to find maximum pairwise hamming distance in an array of hashes then brute force O(N^2) algorithm?

We have unsorted array of hashes, each hash is a 64bit integer. We need to find the pairwise hamming distance in an array. What state the problem to find maximum Manhattan distance between points in 64Bit space where each dimension has only 0 and 1 state.

The simplest solution is:

• just pick each item one-by-one
• and compare (find the hamming distance) with the rest items starting for this one.

That gives us $$O(N^2)$$ complexity, where $$N$$ is length of list. But can we do better because of nature of Hamming distance and nature of 64bit numbers? Could we use binary operations here?

• Hamming distance satisfies the triangle inequality $d(a,b)+d(b,c) \ge d(a,c)$. You can use this to skip some computations if all you need is the biggest Hamming distance of any pair. Note sure whether this is enough to get below $O(N^2)$ and what a good algorithm using that fact would look like. Commented Feb 1, 2019 at 11:55
• Do you need it exactly? Can you live with decent bounds on it? For instance, it is $O(N)$ to compute the ($\mathbb{F}_2$) mean, and then to find the node furthest from this. Twice this distance gives you a $2$-approximation. Commented Feb 1, 2019 at 19:53
• @stochasticboy321 wouldn't a $2-$ approximate solution be useless if it outputs an guess $\geq n/2$? Commented Feb 3, 2019 at 0:56
• I mean because the max distance is very likely quite close to $n=64$ the bitlength. Commented Feb 3, 2019 at 1:17
• @kodlu I mean then its a little obvious that you would report 64 as the upper bound. Really it depends on the application, although I agree that for fixed string-lengths a $2$-approximation would be kinda weak. In any case, my question really was trying to get at if approximate solutions could be acceptable, and how good they'd need to be. Essentially since bringing in some randomised algorithm might get to some decent approximation at $o(N^2).$ Commented Feb 3, 2019 at 4:03