Skip to content

Commit

Permalink
Day-31: Math & Bit manipulation problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Sep 16, 2015
1 parent 5557766 commit 4cb5b6f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 46 |
| Current Streak | 30 |
| Longest Streak | 30 ( August 17, 2015 - September 15, 2015 ) |
| Total Problems | 48 |
| Current Streak | 31 |
| Longest Streak | 31 ( August 17, 2015 - September 16, 2015 ) |

</center>

Expand Down Expand Up @@ -55,6 +55,8 @@ Include contains single header implementation of data structures and some algori
| 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)|
|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)|
| Given two integers, determine if their sum would be interger overflow.| [integerOverflow.cpp](bit_manipulation/integerOverflow.cpp)|
| How many bit flip operation would require to convert number A to B. | [countNumberOfBitFlips.cpp](bit_manipulation/countNumberOfBitFlips.cpp)|

### Cracking the coding interview problems
| Problem | Solution |
| :------------ | :----------: |
Expand Down Expand Up @@ -85,6 +87,7 @@ Include contains single header implementation of data structures and some algori
| Problem | Solution |
| :------------ | :----------: |
| 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) |
| Euclidean algorithm to find greatest common divisor of two numbers. (Iterative and recursive)|[gcd.cpp](math_problems/gcd.cpp)|

### Stack Problems
| Problem | Solution |
Expand Down
35 changes: 35 additions & 0 deletions bit_manipulation/countNumberOfBitFlips.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Given two numbers A and B, count number of flips required to convert number A to B
* Approach : Take XOR of A and B, the result will contain set bits only at positions
* where A differed B. Therefore number of set bits in the result of A ^ B is the number
* of flips required to convert A to B
*/

#include <iostream>

int countSetBits( int x )
{
int count = 0;
while( x ) {
++count;
x = x & (x - 1);
}
return count;
}

int countBitFlips( int a, int b )
{
return countSetBits(a ^ b);
}

int main()
{
int x, y;
std::cout << "Enter number 1 :";
std::cin >> x;
std::cout << "Enter number 2 :";
std::cin >> y;
std::cout << "Bit flips required to convert " << x
<< " to " << y << " is " << countBitFlips(x, y) << std::endl;
return 0;
}
60 changes: 60 additions & 0 deletions math_problems/gcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Given two numbers, determine the greatest common divisior of the two numbers.
*/

#include <iostream>


int gcd1( int a, int b ) {
while( b > 0 ) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}

int gcd2( int a, int b ) {
if ( b == 0 ) return a;
if ( a == 0 ) return b;
return gcd2(b, a % b);
}

int gcd3( int a, int b ) {
if ( a == 0 ) return b;
if ( b == 0 ) return a;
while ( a != b ) {
if ( a > b ) {
a = a-b;
} else {
b = b-a;
}
}
return a;
}

int gcd4 ( int a, int b ) {
if ( a == 0 ) return b;
if ( b == 0 ) return a;
if ( a == b ) return a;
if ( a > b ) return gcd4(b, a-b);
else return gcd4(a, b-a);
}

int main()
{
int a, b;
std::cout << "Enter number 1 : ";
std::cin >> a;
std::cout << "Enter number 2 : ";
std::cin >> b;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd1(a,b) << std::endl;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd2(a,b) << std::endl;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd3(a,b) << std::endl;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd4(a,b) << std::endl;
return 0;
}

0 comments on commit 4cb5b6f

Please sign in to comment.