Skip to content

Commit 4e6c0ac

Browse files
author
shrinathjoshi
committed
Added Solution to day 4 of July Leetcode Challenge
1 parent a9ea45d commit 4e6c0ac

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
/**
3+
* Problem Statement :-
4+
* https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/
5+
*
6+
*
7+
* Additional Reading:-
8+
* https://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/
9+
*
10+
*
11+
*
12+
*/
13+
14+
public class HammingDistance {
15+
public int hammingDistance(int x, int y) {
16+
int count = 0;
17+
18+
int result = x ^ y;
19+
while (result > 0) {
20+
result = (result & result - 1);
21+
count++;
22+
}
23+
return count;
24+
}
25+
26+
public static void main(String[] args) {
27+
int x = 1;
28+
int y = 4;
29+
System.out.println(new HammingDistance().hammingDistance(x, y));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)