Skip to content

Instantly share code, notes, and snippets.

View patelpreet422's full-sized avatar
๐Ÿš€
Changing the world for better

Preet Patel patelpreet422

๐Ÿš€
Changing the world for better
View GitHub Profile
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool binary_search(vector<int>::iterator lower, vector<int>::iterator upper, int k)
{
auto mid = next(lower, distance(lower, upper)/2);
while(lower <= upper)
{
@patelpreet422
patelpreet422 / set_rightmost_off.cpp
Created October 24, 2018 17:32
Setting rightmost set bit of a number to zero.
#include <iostream>
#include <cstdio>
using namespace std;
unsigned long set_rightmost_bit_off(unsigned long k)
{
return k&(k-1);
}
int main()
{
cout << set_rightmost_bit_off(6ul) << '\n';
@patelpreet422
patelpreet422 / r2d_d2r_converter.cpp
Created July 19, 2018 19:18
Roman to decimal and decimal to roman number converter
#include <iostream>
#include <cctype>
#include <array>
#include <unordered_map>
using namespace std;
static unordered_map<char, int> roman_to_dec {
{'I', 1},
{'V', 5},
{'X', 10},
@patelpreet422
patelpreet422 / factorial.cpp
Last active June 9, 2018 19:31
Factorial of large numbers
#include <iostream>
#include <algorithm>
#include <string>
#include <chrono>
using namespace std::chrono;
const int limit = 200;
const std::string factorial(int n)
{
std::string res(limit, '0');
res[0] = '1';
@patelpreet422
patelpreet422 / permutation.cpp
Last active June 7, 2018 21:03
Permutation in C++
#include <iostream>
#include <string>
#include <algorithm>
void permuteHelper(std::string s, std::string chosen)
{
if (s.empty())
{
std::cout << chosen << '\n';
return;
}
@patelpreet422
patelpreet422 / kadanes_algorithm.cpp
Last active June 8, 2018 03:44
Maximum sum sub-array
#include <iostream>
#include <algorithm>
#include <climits>
#include <tuple>
using namespace std;
tuple<int, int, int> maxsumsubarray(int *arr, int size)
{
int start = 0, end = size-1, max_sum=INT_MIN, sum = 0;
for(int i = 0; i < size; ++i)
{
@patelpreet422
patelpreet422 / recursive_bsearch.cpp
Last active June 6, 2018 12:26
Recursive binary search
#include <iostream>
#include <algorithm>
using namespace std;
bool binarySearchHelper(int key, int *array, int low, int high){
if(low == high) return array[low] == key;
if(low < high) {
int mid = (low + high)/2;
if(array[mid] == key) return true;
else if(array[mid] > key) return binarySearchHelper(key, array, low, mid);
@patelpreet422
patelpreet422 / recursive_palindrome.cpp
Last active June 7, 2018 21:12
Palindrome using recursion
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(const string &str, int low, int high)
{
if (low >= high)return true;
if (str[low] != str[high])return false;
return isPalindrome(str, ++low, --high);
}
#include <iostream>
void toh(char from, char to, char intermediate, int noofdisk)
{
if(noofdisk == 1)
std::cout << "Move " << noofdisk << " from " << from << " to " << to << '\n';
else {
toh(from, intermediate, to, noofdisk-1);
std::cout << "Move " << noofdisk << " from " << from << " to " << to << '\n';
toh(intermediate, to, from, noofdisk-1);
}
#include <map>
#include <iostream>
#include <algorithm>
unsigned long long moveto(const std::pair<int, int> &p, std::map< std::pair<int, int>, unsigned long long> &memo)
{
if(memo.find(p) != memo.end()) return memo[p];
/*
(0, 0) -> (0, 0) => We are already at our destiny so, no of ways we can reach our destiny is '0'
(0, 0) -> (a, 0) => Only '1' way to reach any point on X-axis from origin