The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, return the Hamming distance between them.
Example 1:
Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.
Example 2:
Input: x = 3, y = 1 Output: 1
Constraints:
0 <= x, y <= 231 - 1
program hammingDistance
implicit none
integer :: x, y, hammingDistance
! Example 1
x = 1
y = 4
print '(A, I0)', 'Hamming distance between ', x, 'and ', y, 'is ', hammingDistance(x, y)
! Example 2
x = 3
y = 1
print '(A, I0)', 'Hamming distance between ', x, 'and ', y, 'is ', hammingDistance(x, y)
contains
function hammingDistance(x, y) result(hammingDist)
implicit none
integer, intent(in) :: x, y
integer :: hammingDist
hammingDist = count(bit_xor(x, y) /= 0)
end function hammingDistance
end program hammingDistance
temp.f95:4:36: 4 | integer :: x, y, hammingDistance | 1 Error: Symbol ‘hammingdistance’ at (1) cannot have a type temp.f95:9:87: 9 | print '(A, I0)', 'Hamming distance between ', x, 'and ', y, 'is ', hammingDistance(x, y) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:14:87: 14 | print '(A, I0)', 'Hamming distance between ', x, 'and ', y, 'is ', hammingDistance(x, y) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:18:54: 18 | function hammingDistance(x, y) result(hammingDist) | 1 Error: PROGRAM attribute of ‘hammingdistance’ conflicts with PROCEDURE attribute at (1) temp.f95:19:21: 19 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:20:35: 20 | integer, intent(in) :: x, y | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:21:30: 21 | integer :: hammingDist | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:47: 23 | hammingDist = count(bit_xor(x, y) /= 0) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:7: 24 | end function hammingDistance | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:7:5: 7 | x = 1 | 1 Error: Symbol ‘x’ at (1) has no IMPLICIT type temp.f95:8:5: 8 | y = 4 | 1 Error: Symbol ‘y’ at (1) has no IMPLICIT type
!-------------------------------------------------------------------------------
! Copyright (c) 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029,
! 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041,
! 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053,
! 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065,
! 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077,
! 2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089,
! 2090, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2099, 2100, 2101,
! 2102, 2103, 2104, 2105, 2106, 2107, 2108, 2109, 2110, 2111, 2112, 2113,
! 2114, 2115, 2116, 2117, 2118, 2119, 2120, 2121, 2122, 2123, 2124, 2125,
! 2126, 2127, 2128, 2129, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137,
! 2138, 2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149,
! 2150, 2151, 2152, 2153, 2154, 2155, 2156, 2157, 2158, 2159, 2160, 2161,
! 2162, 2163, 2164, 2165, 2166, 2167, 2168, 2169, 2170, 2171, 2172, 2173,
! 2174, 2175, 2176, 2177, 2178, 2179, 2180, 2181, 2182, 2183
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x1b): undefined reference to `main' collect2: error: ld returned 1 exit status
def hammingDistance(x: int, y: int) -> int:
xorResult = x ^ y
dist = 0
while xorResult:
dist += xorResult & 1
xorResult >>= 1
return dist
We can start by taking the XOR of x
and y
. A XOR B value will have a bit set only if both A and B bits are different at that position. We then iterate through the bits of the XOR result, counting the number of set bits, which should give us the Hamming distance required.
xorResult
by XORing x
and y
.dist
to 0.xorResult
is not 0, do the following:
a. Add the least significant bit of xorResult
to dist
.
b. Right shift xorResult
by 1.dist
.This algorithm performs bitwise operations and loops through the bits of xorResult
, which has a maximum of 31 bits due to the constraint 0 <= x, y <= 2^31 - 1
. Thus, the time complexity is O(31) = O(1).
int hammingDistance(int x, int y) {
int dist = 0, xorResult = x ^ y;
while (xorResult) {
dist += xorResult & 1;
xorResult >>= 1;
}
return dist;
}
We can start by taking the XOR of x
and y
. A XOR B value will have a bit set only if both A and B bits are different at that position. We then iterate through the bits of the XOR result, counting the number of set bits, which should give us the Hamming distance required.
xorResult
by XORing x
and y
.dist
to 0.xorResult
is not 0, do the following:
a. Add the least significant bit of xorResult
to dist
.
b. Right shift xorResult
by 1.dist
.This algorithm performs bitwise operations and loops through the bits of xorResult
, which has a maximum of 31 bits due to the constraint 0 <= x, y <= 2^31 - 1
. Thus, the time complexity is O(31) = O(1).