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 <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) | |
{ |
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 <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'; |
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 <cctype> | |
#include <array> | |
#include <unordered_map> | |
using namespace std; | |
static unordered_map<char, int> roman_to_dec { | |
{'I', 1}, | |
{'V', 5}, | |
{'X', 10}, |
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 <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'; |
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> | |
#include <algorithm> | |
void permuteHelper(std::string s, std::string chosen) | |
{ | |
if (s.empty()) | |
{ | |
std::cout << chosen << '\n'; | |
return; | |
} |
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 <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) | |
{ |
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 <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); |
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; | |
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); | |
} |
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> | |
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); | |
} |
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 <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 |