Skip to content

Commit 21604d9

Browse files
committed
Exercise 07: Code Challenges 1 to 8
1 parent 6bc3baf commit 21604d9

8 files changed

+845
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Problem Name - Playing With Bits
3+
4+
Raman likes to play with bits. One day Raman decides to assign a task to his student Sanya.
5+
You have to help Sanya to complete this task.
6+
Task is as follows - Raman gives Q queries each query containing two integers a and b.
7+
Your task is to count the no of set-bits in for all numbers between a and b (both inclusive)
8+
9+
Input Format: Read Q - No of Queries, Followed by Q lines containing 2 integers a and b.
10+
11+
Constraints: Q,a,b are integers.
12+
13+
Output Format: Q lines, each containing an output for your query.
14+
15+
Sample Input: 2
16+
1 1
17+
10 15
18+
19+
Sample Output: 1
20+
17
21+
22+
*/
23+
24+
25+
#include <iostream>
26+
using namespace std;
27+
28+
// function to count set bits
29+
int countBits(int n)
30+
{
31+
int count=0;
32+
while(n)
33+
{
34+
n = n & (n-1);
35+
count++;
36+
}
37+
return count;
38+
}
39+
40+
// function to count set bits between range I to J
41+
int countSetBitIToJ(int start, int end)
42+
{
43+
int count = 0;
44+
for(int i=start; i <= end; i++)
45+
{
46+
count += countBits(i);
47+
}
48+
return count;
49+
}
50+
51+
52+
// function to drive code
53+
int main()
54+
{
55+
int testcase;
56+
cout << "Enter total testcases: ";
57+
cin >> testcase;
58+
59+
while(testcase--)
60+
{
61+
int i, j;
62+
cout << "Enter Range [i & j]: ";
63+
cin >> i >> j;
64+
65+
cout << "Total Set Bits: ";
66+
cout << countSetBitIToJ(i,j) << endl;
67+
}
68+
69+
return 0;
70+
}
71+
72+
/*
73+
74+
OUTPUT:
75+
Enter total testcases: 2
76+
77+
Enter Range [i & j]: 1 1
78+
Total Set Bits: 1
79+
80+
Enter Range [i & j]: 10 15
81+
Total Set Bits: 17
82+
83+
84+
APPROACH:
85+
86+
This is a quite simple problem to tackle. Just loop through all the numbers between a and b and
87+
calculate the no of set bits using either brian kernighan algo or the inbuilt function for
88+
counting set bits
89+
*/
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Problem Name - Unique Number - I
3+
4+
We are given an array containg n numbers. All the numbers are present twice except for one number
5+
which is only present once. Find the unique number without taking any extra spaces and in linear time.
6+
(Hint - Use Bitwise)
7+
8+
Input Format: First line contains the number n.
9+
Second line contains n space separated number.
10+
11+
Constraints: n < 10^5
12+
13+
Output Format: Output a single line containing the unique number
14+
15+
Sample Input: 7
16+
1 1 2 2 3 3 4
17+
18+
Sample Output: 4
19+
20+
Explanation: 4 is present only once
21+
*/
22+
23+
24+
#include <iostream>
25+
using namespace std;
26+
27+
int main()
28+
{
29+
int total_num, num, ans=0;
30+
31+
cout << "Enter total numbers: ";
32+
cin >> total_num;
33+
34+
cout << "Enter numbers: ";
35+
for(int i=0; i<total_num; i++)
36+
{
37+
cin >> num;
38+
// Using bitwise XOR Operator to solve, It helped to not use any storage
39+
ans = ans^num;
40+
}
41+
42+
cout<<"Unique No. is : "<< ans << endl;
43+
44+
return 0;
45+
}
46+
47+
/*
48+
49+
OUTPUT:
50+
51+
Enter total numbers: 7
52+
Enter numbers: 1 1 2 2 3 3 4
53+
Unique No. is : 4
54+
55+
56+
APPROACH:
57+
58+
Use property of xor function to solve this problem.
59+
60+
Concept :
61+
If we take XOR of zero and some bit, it will return that bit: a^0 = a
62+
If we take XOR of two same bits, it will return 0: a^a=0
63+
a^b^a = (a^a)^b = 0^b = b
64+
So we can XOR all bits together to find the unique number.
65+
*/
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Problem Name - XOR Profit Problem
3+
4+
We are given two coins of value x and y. We have to find the maximum of value of a XOR b
5+
where x <= a <= b <= y.
6+
7+
Input Format: We are given two integers x and y
8+
9+
Constraints: l <= r <= 1000
10+
11+
Output Format: Print the maximum value of a XOR b
12+
13+
Sample Input: 5
14+
6
15+
16+
Sample Output: 3
17+
18+
Explanation: If a and b are taken to be 5. Then a xor b = 0
19+
If a and b are taken to be 6. Then a xor b = 0
20+
If a is 5 and b is 6. Then a xor b is 3.
21+
*/
22+
23+
24+
#include <iostream>
25+
using namespace std;
26+
27+
/*
28+
function to find maximum xor value between range x & y
29+
Approach 1: A brute force solution is to generate all pairs. find their XOR values and finally return
30+
the maximum XOR value
31+
*/
32+
int max_xor_value(int x, int y)
33+
{
34+
int result = 0;
35+
for(int i=x; i<=y; i++)
36+
{
37+
for(int j=x+1; j<=y; j++)
38+
{
39+
result = max(i^j, result);
40+
}
41+
}
42+
return result;
43+
}
44+
45+
46+
// Approach 2: A efficient solution is to consider pattern of binary values from L to R.
47+
int max_xor_value_optimised(int x, int y)
48+
{
49+
int num = x^y;
50+
51+
int msb = 0;
52+
while(num)
53+
{
54+
msb++;
55+
num = num>>1;
56+
}
57+
58+
int result = 1;
59+
while(msb)
60+
{
61+
result = result<<1;
62+
msb--;
63+
}
64+
return result-1;
65+
}
66+
67+
// function to drive code
68+
int main()
69+
{
70+
int x, y;
71+
72+
cout << "Enter Coin values: ";
73+
cin >> x >> y;
74+
75+
76+
cout<<"Max XOR Value: ";
77+
// cout << max_xor_value(x,y); // Approach 1 (Brute Force)
78+
cout << max_xor_value_optimised(x,y); // Approach 2 (considering pattern of binary values)
79+
80+
cout << endl;
81+
return 0;
82+
}
83+
84+
/*
85+
86+
OUTPUT:
87+
88+
Case 1:
89+
Enter Coin values: 5 6
90+
Max XOR Value: 3
91+
92+
Case 2:
93+
Enter Coin values: 5 10
94+
Max XOR Value: 15
95+
96+
97+
APPROACH:
98+
99+
- Brute Force Approach
100+
A simple solution is to generate all pairs, find their XOR values and finally return the
101+
maximum XOR value.
102+
103+
- Optimised Approach
104+
An efficient solution is to consider pattern of binary values from L to R.
105+
106+
We can see that first bit from L to R either changes from 0 to 1 or it stays 1
107+
i.e. if we take the XOR of any two numbers for maximum value their first bit will be fixed which
108+
will be same as first bit of XOR of L and R itself.
109+
110+
After observing the technique to get first bit, we can see that if we XOR L and R,
111+
the most significant bit of this XOR will tell us the maximum value we can achieve
112+
i.e. let XOR of L and R is 1xxx where x can be 0 or 1 then maximum XOR value we can get is 1111
113+
because from L to R we have all possible combination of xxx and it is always possible to choose
114+
these bits in such a way from two numbers such that their XOR becomes all 1.
115+
116+
*/
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Problem Name - Count Set Bits
3+
4+
count number of 1s in binary representation of an integer
5+
6+
Input Format: Input N = Number of Test Cases, followed by N numbers
7+
8+
Output Format: Number of Set Bits in each number each in a new line
9+
10+
Sample Input: 3
11+
5
12+
4
13+
15
14+
15+
Sample Output: 2
16+
1
17+
4
18+
19+
Explanation: Convert Binary to Decimal first and then count number of 1's present in all digits.
20+
*/
21+
22+
23+
#include <iostream>
24+
using namespace std;
25+
26+
// function to count set bits
27+
int coutSetBits(int n)
28+
{
29+
int count = 0;
30+
while(n)
31+
{
32+
n = n & (n-1); // removing the last set bits
33+
count++; // count is the no. of times loop run
34+
}
35+
return count;
36+
}
37+
38+
39+
// function to drive code
40+
int main()
41+
{
42+
int testcase, num;
43+
44+
cout << "Enter total testcases: ";
45+
cin >> testcase;
46+
47+
while(testcase--)
48+
{
49+
cout << "Enter Number: ";
50+
cin >> num;
51+
52+
cout << "Total Set Bits: ";
53+
cout << coutSetBits(num) << endl;
54+
}
55+
56+
return 0;
57+
}
58+
59+
/*
60+
61+
OUTPUT:
62+
63+
Enter total testcases: 3
64+
65+
Enter Number: 5
66+
Total Set Bits: 2
67+
68+
Enter Number: 4
69+
Total Set Bits: 1
70+
71+
Enter Number: 15
72+
Total Set Bits: 4
73+
74+
75+
APPROACH:
76+
77+
With bitwise operations, we can use an algorithm whose running time depends on the
78+
number of ones present in the binary form of the given number.
79+
80+
int count_set_nits (int n)
81+
{
82+
while( n > 0)
83+
{
84+
n = n&(n-1);
85+
count++;
86+
}
87+
return count;
88+
}
89+
90+
How does this algorithm work?
91+
92+
We can observe that there is a relationship between a number N and N-1.
93+
N-1 will have all the bits same as x except for the rightmost 1 in x and all the bits to
94+
the right of rightmost 1 in x.
95+
So as in x-1, the rightmost 1 and bits right to it is flipped, then by performing x&(x-1),
96+
and storing it in x, will reduce x to a number containing number of ones(in its binary form)
97+
less than the previous state of x, thus increasing the value of count in each iteration.
98+
This is also known as Brian Kernighan Algorithm.
99+
*/

0 commit comments

Comments
 (0)