-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOf1Bits.java
More file actions
139 lines (106 loc) · 3.33 KB
/
Copy pathNumberOf1Bits.java
File metadata and controls
139 lines (106 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package Algorithms.BitManipulation;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 10 May 2026
* @link 191. Number of 1 Bits <a href="https://leetcode.com/problems/number-of-1-bits/">LeetCode link</a>
* @topics Bit Manipulation, Divide and Conquer
*/
public class NumberOf1Bits {
public static void main(String[] args) {
int n = 11;
System.out.println("number of 1 bits using in-built method: " + hammingWeightUsingInbuiltMethod(n));
System.out.println("number of 1 bits using bit by bit 1: " + hammingWeightUsingBitByBit1(n));
System.out.println("number of 1 bits using bit by bit 2: " + hammingWeightUsingBitByBit2(n));
System.out.println("number of 1 bits using mask: " + hammingWeightUsingMask(n));
System.out.println("number of 1 bits using divide and conquer: " + hammingWeightUsingBitwiseDivideAndConquer(n));
System.out.println("number of 1 bits using string manipulation: " + hammingWeightUsingStringManipulation(n));
}
public static int hammingWeightUsingInbuiltMethod(int n) {
return Integer.bitCount(n);
}
/**
* @TimeComplexity O(32) = O(1) ---> as the input is always 32 bits
* @SpaceComplexity O(1)
*/
public static int hammingWeightUsingBitByBit1(int n) {
int count = 0;
while(n>0){
if ((n&1)==1) count++; // or n%2==1 or count += (n&1)
n>>=1;
}
return count;
}
/**
It's improved version of {@link #hammingWeightUsingBitByBit1}
cause if we check set bits in "1000001" then why we need to check all bits?
so, use n&(n-1)
n = "1100"
n-1 = "1011"
n&(n-1) = "1000" ---> removes rightmost set bit and just loop until n becomes 0
Example walkthrough
n = 13 = 1101
Iteration 1
1101
1100
----
1100
count = 1
Iteration 2
1100
1011
----
1000
count = 2
Iteration 3
1000
0111
----
0000
count = 3
* @TimeComplexity O(1) ---> as time as above {@link #hammingWeightUsingBitByBit1} but this is improved
* @SpaceComplexity O(1)
*/
public static int hammingWeightUsingBitByBit2(int n) {
int sum = 0;
while (n != 0) {
sum++;
n &= (n - 1);
}
return sum;
}
/**
* @TimeComplexity O(1)
* @SpaceComplexity O(1)
*/
public static int hammingWeightUsingMask(int n) {
int bits = 0;
int mask = 1;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
bits++;
}
mask <<= 1;
}
return bits;
}
/**
* @TimeComplexity O(1)
* @SpaceComplexity O(1)
*/
public static int hammingWeightUsingBitwiseDivideAndConquer(int n) {
if (n==0) return 0;
return (n & 1) + hammingWeightUsingBitwiseDivideAndConquer(n >>> 1);
}
/**
* @TimeComplexity O(1)
* @SpaceComplexity O(n)
*/
public static int hammingWeightUsingStringManipulation(int n){
int count=0;
String binary = Integer.toBinaryString(n);
for(char c: binary.toCharArray()) {
if (c=='1') count++;
}
return count;
}
}