From 16e7b9b0543a8a59535a5d0f4f9c43255747d072 Mon Sep 17 00:00:00 2001 From: mandliya Date: Tue, 15 Sep 2015 00:02:01 -0400 Subject: [PATCH] Day-30: More Bit manipulation problems --- README.md | 10 ++++--- bit_manipulation/find_odd_one_out.cpp | 37 +++++++++++++++++++++++++ bit_manipulation/integerOverflow.cpp | 32 +++++++++++++++++++++ bit_manipulation/right_most_set_bit.cpp | 26 +++++++++++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 bit_manipulation/find_odd_one_out.cpp create mode 100644 bit_manipulation/integerOverflow.cpp create mode 100644 bit_manipulation/right_most_set_bit.cpp diff --git a/README.md b/README.md index a6e76ff..c9116bf 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ | Current Status| Stats | | :------------: | :----------: | -| Total Problems | 43 | -| Current Streak | 29 | -| Longest Streak | 29 ( August 17, 2015 - September 14, 2015 ) | +| Total Problems | 46 | +| Current Streak | 30 | +| Longest Streak | 30 ( August 17, 2015 - September 15, 2015 ) | @@ -52,7 +52,9 @@ Include contains single header implementation of data structures and some algori | Find the parity of given number. | [find_parity.cpp](bit_manipulation/find_parity.cpp) | | Implement fast multiplication of a number to 7 using bit manipulation. | [multiply_by_7.cpp](bit_manipulation/multiply_by_7.cpp) | | Reverse bits of unsigned integer (two methods - Reversing bit by bit & divide and conquer). | [reverseBitsOfAnInteger.cpp](bit_manipulation/reverseBitsOfAnInteger.cpp) | - +| 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)| ### Cracking the coding interview problems | Problem | Solution | | :------------ | :----------: | diff --git a/bit_manipulation/find_odd_one_out.cpp b/bit_manipulation/find_odd_one_out.cpp new file mode 100644 index 0000000..a211e59 --- /dev/null +++ b/bit_manipulation/find_odd_one_out.cpp @@ -0,0 +1,37 @@ +/** + * Given a vector of numbers, only one number occurs odd number of times, find the number + * Example - { 1, 1, 2, 2, 2, 3, 3, 3, 3} ==> Answer 3 + * Approach - XOR of number with itself is 0, so even numbers will cancel out + * and we will be left with odd number. + */ + +#include +#include + +int find_odd_one_out( const std::vector & vec ) +{ + int check = 0; + for ( auto i : vec ) + { + check ^= i; + } + return check; +} + +void printVector( const std::vector & vec ) +{ + for ( auto & i : vec ) { + std::cout << i << " "; + } + std::cout << std::endl; +} + +int main() +{ + std::vector vec{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2}; + std::cout << "Vector contains :" << std::endl; + printVector( vec ); + std::cout << "Number which occurs odd time in the above vector :" + << find_odd_one_out( vec ) << std::endl; + return 0; +} diff --git a/bit_manipulation/integerOverflow.cpp b/bit_manipulation/integerOverflow.cpp new file mode 100644 index 0000000..85d1f8f --- /dev/null +++ b/bit_manipulation/integerOverflow.cpp @@ -0,0 +1,32 @@ +/** + * Given two integers, determine if their sum would be interger overflow. + */ + +#include + +/** + * Integer overflow check + * @param a [an Integer] + * @param b [another Integer] + * @return [returns true if a + b causes Integer overflow] + */ +bool integerOverflow( int a, int b) +{ + int c = a + b; + if ( a > 0 && b > 0 && c < 0 ) { + return true; + } else if ( a < 0 && b < 0 && c > 0 ) { + return true; + } + else { + return false; + } +} + +int main() +{ + int x = 2147483640; + int y = 10; + std::cout << "Sum of " << x << " and " << y << " causes interger overflow :" + << (integerOverflow( x, y ) ? "true\n":"false\n"); +} diff --git a/bit_manipulation/right_most_set_bit.cpp b/bit_manipulation/right_most_set_bit.cpp new file mode 100644 index 0000000..4e725c2 --- /dev/null +++ b/bit_manipulation/right_most_set_bit.cpp @@ -0,0 +1,26 @@ +/** + * Problem : One line function to return the position of right most bit. + * Approach : take 2's compliment and it with number. + * And finally taking a log of 2 + 1 will give us the positon + */ + +#include +#include + +int position_of_first_set_bit( int num ) +{ + return log2(num & -num) + 1; +} + +int main() +{ + int num; + std::cout << "Enter a number :"; + std::cin >> num; + + std::cout << "Position of first set bit number in binary representation of " + << num << " is " << position_of_first_set_bit( num ) + << std::endl; + + return 0; +}