This file contains hidden or 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
| #include <iostream> | |
| using namespace std; | |
| #define ull uint64_t | |
| string checkPow2(ull n) { | |
| bool flag = false; | |
| for(int i = 0; i < 64; ++i, n >>= 1) | |
| if ((n & 1) && flag) return "False"; | |
| else if (n & 1) flag = true; |
This file contains hidden or 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
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| int findMissingNumber(int n) { | |
| int sum = 0, x; | |
| for(int i = 0; i < n; ++i, sum += x) | |
| cin >> x; | |
| int expected_sum = ((n+1)*(n+2)) / 2; | |
| return expected_sum - sum; |
This file contains hidden or 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
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| #define ull uint64_t | |
| string bin_rep(ull n) { | |
| if (n == 0) return ""; | |
| string rem(1, (n&1) + '0'); // https://www.geeksforgeeks.org/how-to-convert-a-single-character-to-string-in-cpp/ | |
| return bin_rep(n>>=1) + rem; |
This file contains hidden or 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
| #include <iostream> | |
| using namespace std; | |
| #define ui uint32_t | |
| ui bit_flips(ui a, ui b) { | |
| ui flips = 0; | |
| for(int i = 0; i < 32; ++i, a>>=1, b>>=1) | |
| if ((a&1) ^ (b&1)) | |
| ++flips; |
This file contains hidden or 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
| #include <iostream> | |
| using namespace std; | |
| #define ui uint32_t | |
| ui rev_bits(ui n) { | |
| ui rev_num = 0; | |
| for(int i = 0; i < 31; ++i, n>>=1, rev_num<<=1) | |
| if(n&1U) rev_num |= 1; | |
| return rev_num; |
This file contains hidden or 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
| #include <iostream> | |
| using namespace std; | |
| #define ui uint32_t | |
| ui swap_bits(ui n) { | |
| ui bitval; | |
| for(int i = 0; i < 32; i+=2) { | |
| bitval = ((n>>i)&1) | (((n>>(i+1))&1)<<1); | |
This file contains hidden or 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
| #include <iostream> | |
| #include <vector> | |
| #include <algorithm> | |
| using namespace std; | |
| #define ull uint64_t | |
| bool checkBit(int i, int j) { return (i>>j)&1; } | |
| void generate_subsets(vector<vector<int>> &subsets, const vector<int> &arr) { |
This file contains hidden or 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
| int sum(int n) { | |
| if (n==1) return 1; | |
| return sum(n-1) + n; | |
| } |
This file contains hidden or 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
| int fib(int n) { | |
| if (n==1 || n==2) | |
| return 1; | |
| return fib(n-1) + fib(n-2); | |
| } |
This file contains hidden or 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
| int fact(int n) { | |
| if (n==0) return 1; | |
| return n * fact(n-1); | |
| } |