diff --git a/README.md b/README.md index c9116bf..c803c90 100644 --- a/README.md +++ b/README.md @@ -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 ) | @@ -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 | | :------------ | :----------: | @@ -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 | diff --git a/bit_manipulation/countNumberOfBitFlips.cpp b/bit_manipulation/countNumberOfBitFlips.cpp new file mode 100644 index 0000000..e872dae --- /dev/null +++ b/bit_manipulation/countNumberOfBitFlips.cpp @@ -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 + +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; +} diff --git a/math_problems/gcd.cpp b/math_problems/gcd.cpp new file mode 100644 index 0000000..23933dc --- /dev/null +++ b/math_problems/gcd.cpp @@ -0,0 +1,60 @@ +/** + * Given two numbers, determine the greatest common divisior of the two numbers. + */ + +#include + + +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; +}