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; | |
| void TOH(int n, char src, char dest, char temp) { | |
| if(n==0) return; | |
| TOH(n-1, src, temp, dest); | |
| cout << "Move " << n << " from " << src << " to " << dest << "\n"; | |
| TOH(n-1, temp, dest, src); | |
| } |
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
| // Problem link: https://leetcode.com/problems/generate-parentheses/ | |
| class Solution { | |
| public: | |
| void gen_par(string str, int pos, int open, int close, int n, vector<string> &result) { | |
| if(pos == n) { | |
| result.push_back(str); | |
| cout << str << "\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 <vector> | |
| #include <algorithm> | |
| using namespace std; | |
| static int total_swaps = 0; // if we want to count the number of swaps | |
| void bubble_sort(vector<int> &arr, int n, int count_swaps = 0) { | |
| if(n==1) 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 <vector> | |
| using namespace std; | |
| #define ull uint64_t | |
| void ins_sort_rec(vector<int> &ar, vector<int> &ins_idx, ull start = 1) { | |
| // Base Case | |
| if(start == ar.size()) 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 <vector> | |
| #include <algorithm> // for swap() | |
| #include <climits> // for INT_MIN | |
| using namespace std; | |
| void select_sort_rec(vector<int> &ar, vector<int> &swaps_eles, int n, int max_ele = INT_MIN, int max_idx = -1) { | |
| // Base Case | |
| if(n == 1) 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
| // Problem link: https://www.geeksforgeeks.org/find-the-two-repeating-elements-in-a-given-array/ | |
| #include <vector> | |
| #include <iostream> | |
| #include <algorithm> | |
| using namespace std; | |
| #define ull uint64_t | |
| void countsort_rec(const vector<int> &ar, vector<int> &cnt, ull idx = 0) { |
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
| // Problem link: https://www.hackerrank.com/challenges/migratory-birds/problem | |
| #include <iostream> | |
| #include <vector> | |
| #include <algorithm> | |
| #include <climits> | |
| using namespace std; | |
| #define ull uint64_t |
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 <vector> | |
| #include <iostream> | |
| #include <algorithm> | |
| using namespace std; | |
| string sum_pair(vector<int> &ar, const int k) { | |
| sort(ar.begin(), ar.end()); | |
| for(int i = 0, j = ar.size()-1, sum = ar[i] + ar[j]; i < j; sum = ar[i] + ar[j]) { | |
| if(sum == k) return "True"; | |
| else if(sum < k) ++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
| // Problem Link: https://leetcode.com/problems/two-sum/ | |
| class Solution { | |
| public: | |
| vector<int> twoSum(vector<int>& nums, int target) { | |
| unordered_map<int, int> m; // dict aka HashMap | |
| vector<int> result{-1, -1}; | |
| for(int i = 0, complement = target - nums[i]; i < nums.size(); ++i, complement = target - nums[i]) { | |
| auto it = m.find(complement); | |
| if(it != m.end()) { | |
| result[0] = it->second; |
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
| // Problem Link: https://practice.geeksforgeeks.org/problems/largest-number-formed-from-an-array/0 | |
| #include <iostream> | |
| #include <algorithm> | |
| #include <vector> | |
| #include <string> | |
| using namespace std; | |
| #define ull uint64_t |