-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day-30: More Bit manipulation problems
- Loading branch information
Showing
4 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <iostream> | ||
#include <vector> | ||
|
||
int find_odd_one_out( const std::vector<int> & vec ) | ||
{ | ||
int check = 0; | ||
for ( auto i : vec ) | ||
{ | ||
check ^= i; | ||
} | ||
return check; | ||
} | ||
|
||
void printVector( const std::vector<int> & vec ) | ||
{ | ||
for ( auto & i : vec ) { | ||
std::cout << i << " "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
int main() | ||
{ | ||
std::vector<int> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Given two integers, determine if their sum would be interger overflow. | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
/** | ||
* 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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <iostream> | ||
#include <cmath> | ||
|
||
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; | ||
} |