Given two integers, the task is to find the hamming distance between two integers. Hamming Distance between two integers is the number of bits that are different at the same position in both numbers.
Examples:
Input: n1 = 9, n2 = 14
Output: 3
9 = 1001, 14 = 1110
No. of Different bits = 3
Input: n1 = 4, n2 = 8
Output: 2
Approach:
- Calculate the XOR of two numbers.
- Count the number of set bits.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int hammingDistance( int n1, int n2)
{
int x = n1 ^ n2;
int setBits = 0;
while (x > 0) {
setBits += x & 1;
x >>= 1;
}
return setBits;
}
int main()
{
int n1 = 9, n2 = 14;
cout << hammingDistance(9, 14) << endl;
return 0;
}
|
C
#include <stdio.h>
int hammingDistance( int n1, int n2)
{
int x = n1 ^ n2;
int setBits = 0;
while (x > 0) {
setBits += x & 1;
x >>= 1;
}
return setBits;
}
int main()
{
int n1 = 9, n2 = 14;
printf ( "%d\n" , hammingDistance(9, 14));
return 0;
}
|
Java
class GFG
{
static int hammingDistance( int n1, int n2)
{
int x = n1 ^ n2;
int setBits = 0 ;
while (x > 0 )
{
setBits += x & 1 ;
x >>= 1 ;
}
return setBits;
}
public static void main(String[] args)
{
int n1 = 9 , n2 = 14 ;
System.out.println(hammingDistance(n1, n2));
}
}
|
Python3
def hammingDistance(n1, n2) :
x = n1 ^ n2
setBits = 0
while (x > 0 ) :
setBits + = x & 1
x >> = 1
return setBits
if __name__ = = '__main__' :
n1 = 9
n2 = 14
print (hammingDistance( 9 , 14 ))
|
C#
class GFG
{
static int hammingDistance( int n1, int n2)
{
int x = n1 ^ n2;
int setBits = 0;
while (x > 0)
{
setBits += x & 1;
x >>= 1;
}
return setBits;
}
static void Main()
{
int n1 = 9, n2 = 14;
System.Console.WriteLine(hammingDistance(n1, n2));
}
}
|
PHP
<?PHP
function hammingDistance( $n1 , $n2 )
{
$x = $n1 ^ $n2 ;
$setBits = 0;
while ( $x > 0)
{
$setBits += $x & 1;
$x >>= 1;
}
return $setBits ;
}
$n1 = 9;
$n2 = 14;
echo (hammingDistance(9, 14));
?>
|
Javascript
<script>
function hammingDistance(n1, n2)
{
let x = n1 ^ n2;
let setBits = 0;
while (x > 0) {
setBits += x & 1;
x >>= 1;
}
return setBits;
}
let n1 = 9, n2 = 14;
document.write(hammingDistance(9, 14));
</script>
|
Note: No. of set bits can be counted using __builtin_popcount() function.
Time Complexity: O(log2x), where x is n1 ^ n2, this can be interpreted as O(1), since an int can hold a maximum of 32 bits
Auxiliary Space: O(1)
Approach 2:
1. Calculate the maximum of both numbers.
2. Check the set bits for both at each position.
C++
#include <bits/stdc++.h>
using namespace std;
int hammingDistance( int x, int y)
{
int ans = 0;
int m = max(x, y);
while (m) {
int c1 = x & 1;
int c2 = y & 1;
if (c1 != c2)
ans += 1;
m = m >> 1;
x = x >> 1;
y = y >> 1;
}
return ans;
}
int main()
{
int n1 = 4, n2 = 8;
int hdist = hammingDistance(n1, n2);
cout << hdist << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int hammingDistance( int x, int y)
{
int ans = 0 ;
int m = Math.max(x, y);
while (m> 0 ) {
int c1 = x & 1 ;
int c2 = y & 1 ;
if (c1 != c2)
ans += 1 ;
m = m >> 1 ;
x = x >> 1 ;
y = y >> 1 ;
}
return ans;
}
public static void main(String args[])
{
int n1 = 4 , n2 = 8 ;
int hdist = hammingDistance(n1, n2);
System.out.println(hdist);
}
}
|
Python3
def hammingDistance(x, y):
ans = 0
m = max (x, y)
while (m):
c1 = x & 1
c2 = y & 1
if (c1 ! = c2):
ans + = 1
m = m >> 1
x = x >> 1
y = y >> 1
return ans
n1 = 4
n2 = 8
hdist = hammingDistance(n1, n2)
print (hdist)
|
C#
using System;
public class GFG
{
static int hammingDistance( int x, int y)
{
int ans = 0;
int m = Math.Max(x, y);
while (m > 0) {
int c1 = x & 1;
int c2 = y & 1;
if (c1 != c2)
ans += 1;
m = m >> 1;
x = x >> 1;
y = y >> 1;
}
return ans;
}
public static void Main( string [] args)
{
int n1 = 4, n2 = 8;
int hdist = hammingDistance(n1, n2);
Console.WriteLine(hdist);
}
}
|
Javascript
<script>
function hammingDistance(x,y)
{
let ans = 0;
let m = Math.max(x, y);
while (m) {
let c1 = x & 1;
let c2 = y & 1;
if (c1 != c2)
ans += 1;
m = m >> 1;
x = x >> 1;
y = y >> 1;
}
return ans;
}
let n1 = 4,n2 = 8;
let hdist = hammingDistance(n1, n2);
document.write(hdist, "</br>" );
</script>
|
Time Complexity: O(log2(max(n1, n2)).
Auxiliary Space: O(1)
Approach#3: Using string manipulation
Algorithm
1. Convert the two given integers to binary strings.
2. Pad the shorter binary string with leading zeros to make both strings of equal length.
3. Compare the two binary strings bit by bit and count the number of differences.
4. The count obtained in step 3 represents the Hamming distance between the two given integers.
C++
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int hammingDistance( int n1, int n2) {
string binStr1 = bitset<32>(n1).to_string();
string binStr2 = bitset<32>(n2).to_string();
int lenDiff = abs ( static_cast < int >(binStr1.length() - binStr2.length()));
if (binStr1.length() < binStr2.length()) {
binStr1 = string(lenDiff, '0' ) + binStr1;
} else {
binStr2 = string(lenDiff, '0' ) + binStr2;
}
int count = 0;
for ( int i = 0; i < binStr1.length(); i++) {
if (binStr1[i] != binStr2[i]) {
count++;
}
}
return count;
}
int main() {
int n1 = 4;
int n2 = 8;
cout << hammingDistance(n1, n2) << endl;
return 0;
}
|
Java
public class Main {
public static int hammingDistance( int n1, int n2) {
String binStr1 = Integer.toBinaryString(n1);
String binStr2 = Integer.toBinaryString(n2);
int lenDiff = Math.abs(binStr1.length() - binStr2.length());
if (binStr1.length() < binStr2.length()) {
binStr1 = "0" .repeat(lenDiff) + binStr1;
} else {
binStr2 = "0" .repeat(lenDiff) + binStr2;
}
int count = 0 ;
for ( int i = 0 ; i < binStr1.length(); i++) {
if (binStr1.charAt(i) != binStr2.charAt(i)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int n1 = 4 ;
int n2 = 8 ;
System.out.println(hammingDistance(n1, n2));
}
}
|
Python3
def hamming_distance(n1, n2):
bin_str1 = bin (n1)[ 2 :]
bin_str2 = bin (n2)[ 2 :]
len_diff = abs ( len (bin_str1) - len (bin_str2))
if len (bin_str1) < len (bin_str2):
bin_str1 = '0' * len_diff + bin_str1
else :
bin_str2 = '0' * len_diff + bin_str2
count = 0
for i in range ( len (bin_str1)):
if bin_str1[i] ! = bin_str2[i]:
count + = 1
return count
n1 = 4
n2 = 8
print (hamming_distance(n1, n2))
|
Javascript
function hamming_distance(n1, n2) {
let bin_str1 = n1.toString(2);
let bin_str2 = n2.toString(2);
let len_diff = Math.abs(bin_str1.length - bin_str2.length);
if (bin_str1.length < bin_str2.length) {
bin_str1 = '0' .repeat(len_diff) + bin_str1;
} else {
bin_str2 = '0' .repeat(len_diff) + bin_str2;
}
let count = 0;
for (let i = 0; i < bin_str1.length; i++) {
if (bin_str1[i] !== bin_str2[i]) {
count += 1;
}
}
return count;
}
let n1 = 4;
let n2 = 8;
console.log(hamming_distance(n1, n2));
|
Time Complexity: O(log n), where n is the maximum of the two integers.
Space Complexity: O(log n), where n is the maximum of the two integers.
Please Login to comment...