Skip to content

Commit 4cb5b6f

Browse files
committed
Day-31: Math & Bit manipulation problems
1 parent 5557766 commit 4cb5b6f

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

Diff for: README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 46 |
8-
| Current Streak | 30 |
9-
| Longest Streak | 30 ( August 17, 2015 - September 15, 2015 ) |
7+
| Total Problems | 48 |
8+
| Current Streak | 31 |
9+
| Longest Streak | 31 ( August 17, 2015 - September 16, 2015 ) |
1010

1111
</center>
1212

@@ -55,6 +55,8 @@ Include contains single header implementation of data structures and some algori
5555
| Small function to determine position of right most set bit in a given integer.| [right_most_set_bit.cpp](bit_manipulation/right_most_set_bit.cpp)|
5656
|Given a vector of numbers, only one number occurs odd number of times, find the number.| [find_odd_one_out.cpp](bit_manipulation/find_odd_one_out.cpp)|
5757
| Given two integers, determine if their sum would be interger overflow.| [integerOverflow.cpp](bit_manipulation/integerOverflow.cpp)|
58+
| How many bit flip operation would require to convert number A to B. | [countNumberOfBitFlips.cpp](bit_manipulation/countNumberOfBitFlips.cpp)|
59+
5860
### Cracking the coding interview problems
5961
| Problem | Solution |
6062
| :------------ | :----------: |
@@ -85,6 +87,7 @@ Include contains single header implementation of data structures and some algori
8587
| Problem | Solution |
8688
| :------------ | :----------: |
8789
| Print all the permutations of a string. Example: Permutations of ABC are ABC, ACB, BCA, BAC, CAB, CBA | [string_permutations.cpp] (math_problems/string_permutations.cpp) |
90+
| Euclidean algorithm to find greatest common divisor of two numbers. (Iterative and recursive)|[gcd.cpp](math_problems/gcd.cpp)|
8891

8992
### Stack Problems
9093
| Problem | Solution |

Diff for: bit_manipulation/countNumberOfBitFlips.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Given two numbers A and B, count number of flips required to convert number A to B
3+
* Approach : Take XOR of A and B, the result will contain set bits only at positions
4+
* where A differed B. Therefore number of set bits in the result of A ^ B is the number
5+
* of flips required to convert A to B
6+
*/
7+
8+
#include <iostream>
9+
10+
int countSetBits( int x )
11+
{
12+
int count = 0;
13+
while( x ) {
14+
++count;
15+
x = x & (x - 1);
16+
}
17+
return count;
18+
}
19+
20+
int countBitFlips( int a, int b )
21+
{
22+
return countSetBits(a ^ b);
23+
}
24+
25+
int main()
26+
{
27+
int x, y;
28+
std::cout << "Enter number 1 :";
29+
std::cin >> x;
30+
std::cout << "Enter number 2 :";
31+
std::cin >> y;
32+
std::cout << "Bit flips required to convert " << x
33+
<< " to " << y << " is " << countBitFlips(x, y) << std::endl;
34+
return 0;
35+
}

Diff for: math_problems/gcd.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Given two numbers, determine the greatest common divisior of the two numbers.
3+
*/
4+
5+
#include <iostream>
6+
7+
8+
int gcd1( int a, int b ) {
9+
while( b > 0 ) {
10+
int temp = a % b;
11+
a = b;
12+
b = temp;
13+
}
14+
return a;
15+
}
16+
17+
int gcd2( int a, int b ) {
18+
if ( b == 0 ) return a;
19+
if ( a == 0 ) return b;
20+
return gcd2(b, a % b);
21+
}
22+
23+
int gcd3( int a, int b ) {
24+
if ( a == 0 ) return b;
25+
if ( b == 0 ) return a;
26+
while ( a != b ) {
27+
if ( a > b ) {
28+
a = a-b;
29+
} else {
30+
b = b-a;
31+
}
32+
}
33+
return a;
34+
}
35+
36+
int gcd4 ( int a, int b ) {
37+
if ( a == 0 ) return b;
38+
if ( b == 0 ) return a;
39+
if ( a == b ) return a;
40+
if ( a > b ) return gcd4(b, a-b);
41+
else return gcd4(a, b-a);
42+
}
43+
44+
int main()
45+
{
46+
int a, b;
47+
std::cout << "Enter number 1 : ";
48+
std::cin >> a;
49+
std::cout << "Enter number 2 : ";
50+
std::cin >> b;
51+
std::cout << "GCD of " << a << " and "
52+
<< b << " is " << gcd1(a,b) << std::endl;
53+
std::cout << "GCD of " << a << " and "
54+
<< b << " is " << gcd2(a,b) << std::endl;
55+
std::cout << "GCD of " << a << " and "
56+
<< b << " is " << gcd3(a,b) << std::endl;
57+
std::cout << "GCD of " << a << " and "
58+
<< b << " is " << gcd4(a,b) << std::endl;
59+
return 0;
60+
}

0 commit comments

Comments
 (0)