-
Notifications
You must be signed in to change notification settings - Fork 10
/
01_playing_with_bits.cpp
89 lines (66 loc) · 1.79 KB
/
01_playing_with_bits.cpp
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
/*
Problem Name - Playing With Bits
Raman likes to play with bits. One day Raman decides to assign a task to his student Sanya.
You have to help Sanya to complete this task.
Task is as follows - Raman gives Q queries each query containing two integers a and b.
Your task is to count the no of set-bits in for all numbers between a and b (both inclusive)
Input Format: Read Q - No of Queries, Followed by Q lines containing 2 integers a and b.
Constraints: Q,a,b are integers.
Output Format: Q lines, each containing an output for your query.
Sample Input: 2
1 1
10 15
Sample Output: 1
17
*/
#include <iostream>
using namespace std;
// function to count set bits
int countBits(int n)
{
int count=0;
while(n)
{
n = n & (n-1);
count++;
}
return count;
}
// function to count set bits between range I to J
int countSetBitIToJ(int start, int end)
{
int count = 0;
for(int i=start; i <= end; i++)
{
count += countBits(i);
}
return count;
}
// function to drive code
int main()
{
int testcase;
cout << "Enter total testcases: ";
cin >> testcase;
while(testcase--)
{
int i, j;
cout << "Enter Range [i & j]: ";
cin >> i >> j;
cout << "Total Set Bits: ";
cout << countSetBitIToJ(i,j) << endl;
}
return 0;
}
/*
OUTPUT:
Enter total testcases: 2
Enter Range [i & j]: 1 1
Total Set Bits: 1
Enter Range [i & j]: 10 15
Total Set Bits: 17
APPROACH:
This is a quite simple problem to tackle. Just loop through all the numbers between a and b and
calculate the no of set bits using either brian kernighan algo or the inbuilt function for
counting set bits
*/