Skip to content

Commit 271fb95

Browse files
committed
added totalHammingDistance
1 parent 121b458 commit 271fb95

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [C++](./algorithms/cpp/totalHammingDistance/totalHammingDistance.cpp)|Medium|
1112
|415|[Add Strings](https://leetcode.com/problems/add-strings/) | [C++](./algorithms/cpp/addStrings/AddStrings.cpp)|Easy|
1213
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [C++](./algorithms/cpp/thirdMaximumNumber/ThirdMaximumNumber.cpp)|Easy|
1314
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/) | [C++](./algorithms/cpp/arithmeticSlices/ArithmeticSlices.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Source : https://leetcode.com/problems/total-hamming-distance/
2+
// Author : Calinescu Valentin
3+
// Date : 2017-01-09
4+
5+
/***************************************************************************************
6+
*
7+
* The Hamming distance between two integers is the number of positions at which the
8+
* corresponding bits are different.
9+
*
10+
* Now your job is to find the total Hamming distance between all pairs of the given
11+
* numbers.
12+
*
13+
* Example:
14+
* Input: 4, 14, 2
15+
*
16+
* Output: 6
17+
*
18+
* Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
19+
* showing the four bits relevant in this case). So the answer will be:
20+
* HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
21+
*
22+
* Note:
23+
* Elements of the given array are in the range of 0 to 10^9
24+
* Length of the array will not exceed 10^4.
25+
***************************************************************************************/
26+
27+
/*
28+
* Solution 1 - O(N)
29+
*
30+
* The total Hamming Distance is equal to the sum of all individual Hamming Distances
31+
* between every 2 numbers. However, given that this depends on the individual bits of
32+
* each number, we can see that we only need to compute the number of 1s and 0s for each
33+
* bit position. For example, we look at the least significant bit. Given that we need to
34+
* calculate the Hamming Distance for each pair of 2 numbers, we see that the answer is
35+
* equal to the number of 1s at this position * the number of 0s(which is the total number
36+
* of numbers - the number of 1s), because for each 1 we need to have a 0 to form a pair.
37+
* Thus, the solution is the sum of all these distances at every position.
38+
*/
39+
class Solution {
40+
public:
41+
int totalHammingDistance(vector<int>& nums) {
42+
long long solution = 0;
43+
int ones[31];
44+
for(int i = 0; i < 31; i++)
45+
ones[i] = 0;
46+
for(vector<int>::iterator it = nums.begin(); it != nums.end(); ++it)
47+
{
48+
for(int i = 0; (1 << i) <= *it; i++) //i is the position of the bit
49+
if((1 << i) & *it)//to see if the bit at i-position is a 1
50+
ones[i]++;
51+
}
52+
for(int i = 0; i < 31; i++)
53+
solution += ones[i] * (nums.size() - ones[i]);
54+
return solution;
55+
}
56+
};

0 commit comments

Comments
 (0)