-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountTotalSetBits.java
63 lines (48 loc) · 1.84 KB
/
CountTotalSetBits.java
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
package bit_manipulation.medium;
/***
* Problem in GeeksForGeeks: https://practice.geeksforgeeks.org/problems/count-total-set-bits-1587115620/1
*
* You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive).
*
* Example 1:
* Input: N = 4
* Output: 5
*
* Example 2:
* Input: N = 9999999
* Output: 114434624
*/
public class CountTotalSetBits {
public static void main(String[] args) {
int n = 9999999;
System.out.println("Brute Force: " + getCountOfTotalSetBitsBruteForce(n));
System.out.println("Recursion: " + getCountOfTotalSetBitsRecursion(n));
}
private static int getCountOfTotalSetBitsBruteForce(int n) {
int totalNumberOfSetBits = 0;
for (int i = 1; i <= n; i++) {
int number = i, numberOfSetBits = 0;
while (number != 0) {
if ((number & 1) == 1) {
numberOfSetBits++;
}
number >>= 1;
}
totalNumberOfSetBits += numberOfSetBits;
}
return totalNumberOfSetBits;
}
private static int getCountOfTotalSetBitsRecursion(int n) {
if (n == 0) return 0;
int largestPowerOfTwoToTheN = getLargestPowerOfTwoToTheN(n);
int numberOfSetBitsTillLargestPowerOfTwo = largestPowerOfTwoToTheN * (1 << (largestPowerOfTwoToTheN - 1));
int numberOfSetBitsInMsb = n - (1 << largestPowerOfTwoToTheN) + 1;
int remainingSetBits = n - (1 << largestPowerOfTwoToTheN);
int totalSetBits = numberOfSetBitsTillLargestPowerOfTwo + numberOfSetBitsInMsb + getCountOfTotalSetBitsRecursion(remainingSetBits);
return totalSetBits;
}
private static int getLargestPowerOfTwoToTheN(int n) {
int numberOfBits = (int) ((Math.log(n) / Math.log(2)) + 1);
return numberOfBits - 1;
}
}