Skip to content

Instantly share code, notes, and snippets.

View Ch-sriram's full-sized avatar
🎯
Achieving Greatness | Non-stop Hustling

Sriram Chandrabhatta Ch-sriram

🎯
Achieving Greatness | Non-stop Hustling
View GitHub Profile
@Ch-sriram
Ch-sriram / checkPow2.cpp
Last active October 27, 2020 03:29
Given a number N, check if it is a power of 2 [TC: O(log(N)); SC: O(1)]
#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;
@Ch-sriram
Ch-sriram / find_missing.cpp
Created May 30, 2020 01:27
Given an array of size N, it contains all the numbers from 1 to N+1 inclusive, except one number. You have to find the missing number.
#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;
@Ch-sriram
Ch-sriram / bin_rep.cpp
Created May 30, 2020 01:56
Given an integer N, find its binary representation.
#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;
@Ch-sriram
Ch-sriram / flip_bits.cpp
Created May 30, 2020 02:12
Given two integers A & B, find the number of bits that need to be flipped in B to convert it to A.
#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;
@Ch-sriram
Ch-sriram / rev_bits.cpp
Created May 30, 2020 04:07
Given an unsigned 32-bit integer N, reverse the bits in the binary representation of the integer, and print the new integer formed.
#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;
@Ch-sriram
Ch-sriram / swap_bits.cpp
Created May 30, 2020 05:30
Given an integer N, swap the adjacent bits in the binary representation of the integer, and print the new number formed after swapping.
#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);
@Ch-sriram
Ch-sriram / gen_subsets.cpp
Created May 31, 2020 10:22
Given an array of unique integer elements, print all the subsets of the given array in lexicographical order [O(2^N) Solution].
#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) {
@Ch-sriram
Ch-sriram / natural_sum.cpp
Created May 31, 2020 11:16
Sum of N natural numbers.
int sum(int n) {
if (n==1) return 1;
return sum(n-1) + n;
}
@Ch-sriram
Ch-sriram / nth_fib.cpp
Created May 31, 2020 11:20
Given an integer N, return the Nth integer in the Fibonacci Sequence [Naive Solution O(2^N)].
int fib(int n) {
if (n==1 || n==2)
return 1;
return fib(n-1) + fib(n-2);
}
@Ch-sriram
Ch-sriram / fact.cpp
Created May 31, 2020 11:24
Given N, return N! ["N factorial"] [Naive Solution]
int fact(int n) {
if (n==0) return 1;
return n * fact(n-1);
}