Skip to content

Instantly share code, notes, and snippets.

View Ch-sriram's full-sized avatar
🎯
Solving problems, one step at a time

Chandrabhatta Sriram Ch-sriram

🎯
Solving problems, one step at a time
View GitHub Profile
@Ch-sriram
Ch-sriram / hello.py
Created September 11, 2019 17:22
Testing Github Gists
print("Hello World!")
@Ch-sriram
Ch-sriram / pow2.cpp
Last active May 29, 2020 12:12
Given a number N, find 2^N.
// Only works for 64 bit integers
int64_t pow2 (long long N) {
if (N==0) return 1;
return 1LL << N;
}
@Ch-sriram
Ch-sriram / setxy.cpp
Created May 28, 2020 14:16
Given Xth & Yth bit positions, WAF to create a number where Xth & Yth bit are set.
int setxy (int x, int y) {
return ((1 << x) | (1 << y));
}
@Ch-sriram
Ch-sriram / checkith.cpp
Last active May 28, 2020 14:22
Given a number N & a bit position - i, WAF to check whether the i'th bit is set in N or not.
bool checkith (int N, int i) {
return N & (1 << i);
// Other solutions are:
// return (N >> i) % 2;
// return (N >> i) & 1;
}
@Ch-sriram
Ch-sriram / setbits.cpp
Created May 28, 2020 14:25
Given a Number - N, count the number of set bits in N.
// Refer checkith() function here: https://gist.github.com/Ch-sriram/fef3d0a6274d20f6705495b7cf5b22e4
bool checkith (long long N, int i) {
return N & ((long long) 1 << i);
}
int countSetBits (long long N) {
int count = 0;
for (int i = 0; i < 64; ++i)
if (checkith(N, i))
++count;
@Ch-sriram
Ch-sriram / setbits2.cpp
Created May 28, 2020 14:36
Given a Number - N, count the number of set bits in N [This solution unsets the Least Significant Set Bit of the given N].
// for previous solution: https://gist.github.com/Ch-sriram/212f8185414fcb71278c6028128fcb2a
// Intuition behind the solution:
// When we apply bitwise-AND the number N with N-1, we get a new number whose Least Significant Set Bit is Unset.
int countSetBits (long long N) {
int cnt = 0;
while (N != 0) {
++cnt;
N = N & (N-1); // This is the unsetting step.
@Ch-sriram
Ch-sriram / pow_slow.cpp
Created May 29, 2020 01:41
Given Number a & N, compute a^N. [Naive Solution: O(N)].
// Naive Solution: O(N) Time Complexity
const long long M = (long long) 1e9+7; // a very big prime number
long long pow(a, N) {
if (N == 0) return 1;
long long ans = 1;
for(int i = 0; i < N; ++i)
ans = ((ans % M) * a) % M;
return ans % M;
}
@Ch-sriram
Ch-sriram / pow_fast.cpp
Last active April 15, 2022 11:13
Given Number a & N, compute a^N. [Optimal Solution: O(log N)].
// a^N using fast exponentiation
const int64_t M = (int64_t) 1e9+7; // very big prime number
int64_t pow_fast (int64_t a, int64_t N) {
int64_t ans = 1, x = a;
while (N != 0) {
if (N & 1)
ans = ((ans % M) * x) % M;
x = ((x % M) * x) % M;
N >>= 1;
@Ch-sriram
Ch-sriram / trailing_zeroes.cpp
Created May 29, 2020 03:25
Given a number N, find the number of trailing zeroes in N!
// Good explanation: https://www.purplemath.com/modules/factzero.htm
#include <bits/stdc++.h>
using namespace std;
uint64_t trailing_zeroes(uint64_t n) {
uint64_t zeroes = 0;
for(uint64_t i = 1ULL, factor = 5ULL; factor <= n; ++i, factor = pow(5, i))
zeroes += (n / factor);
return zeroes;
}
@Ch-sriram
Ch-sriram / gcd_lcm.cpp
Last active May 29, 2020 04:20
Given two integers A and B, find their GCD & LCM.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define u uint64_t
// Good explanation: https://www.youtube.com/watch?v=p5gn2hj51hs
u gcd_euclid(u a, u b) { return b == 0 ? a : gcd_euclid(b, a%b); }
u _lcm(u a, u b, u gcd) { return (a*b)/gcd; }