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 / toh.cpp
Created June 1, 2020 01:39
Towers of Hanoi/Brahma/Lucas.
#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);
}
@Ch-sriram
Ch-sriram / gen_par.cpp
Last active June 1, 2020 02:57
Given N pairs of parentheses, write a function to generate all combinations of well-formed valid parentheses.
// 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;
}
@Ch-sriram
Ch-sriram / bubble_sort_rec.cpp
Last active June 2, 2020 06:36
Bubble Sort Recursive Algorithm O(N^2).
#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;
@Ch-sriram
Ch-sriram / insertion_sort_rec.cpp
Last active June 2, 2020 12:48
Insertion Sort Recursive Algorithm O(N^2).
#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;
@Ch-sriram
Ch-sriram / selection_sort_rec.cpp
Last active June 2, 2020 12:47
Selection Sort Recursive Algorithm O(N^2)
#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;
@Ch-sriram
Ch-sriram / repeated_numbers_countsort.cpp
Created June 3, 2020 04:16
Find the two repeating elements in a given array.
// 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) {
@Ch-sriram
Ch-sriram / migratory_birds_count_sort.cpp
Created June 3, 2020 09:11
Migratory Birds using Counting Sort [O(N)].
// 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
@Ch-sriram
Ch-sriram / sum_pairs.cpp
Created June 4, 2020 05:20
Sum of Pairs: Given an array of integers and a number K, check if there exist a pair of indices i,j s.t. a[i] + a[j] = K and i!=j.
#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;
// 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;
@Ch-sriram
Ch-sriram / large_concat_num.cpp
Created June 4, 2020 06:22
Largest Concatenated Number
// 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