Repository: coding-minutes/dsa-essentials-solutions-cpp
Branch: master
Commit: 03d2b96551a0
Files: 76
Total size: 53.1 KB
Directory structure:
gitextract_2mb6elmx/
└── DSA Essentials Solutions/
├── 2d Arrays/
│ ├── PascalsTriangle.cpp
│ ├── SubmatrixSum.cpp
│ └── WavePrint.cpp
├── Arrays/
│ ├── K-rotate.cpp
│ ├── LargestElement.cpp
│ ├── LowerBound.cpp
│ ├── MaximumSumSubarray.cpp
│ └── SortedPairSum.cpp
├── Backtracking/
│ ├── N-QueenWays.cpp
│ ├── RatAndMice.cpp
│ ├── UniqueSubset.cpp
│ ├── WordBreakProblem.cpp
│ └── WordSearch.cpp
├── Basic Sorting Algorithms/
│ ├── Chopsticks.cpp
│ ├── DefenseKingdom.cpp
│ ├── OptimisedBubbleSort.cpp
│ ├── SortingCartesianProducts.cpp
│ └── SortingWithComparator.cpp
├── BinarySearchTree/
│ ├── DeleteInBST.cpp
│ ├── IsBST.cpp
│ └── MirrorABST.cpp
├── BinaryTree/
│ ├── ExpressionTree.cpp
│ ├── K-thLevel.cpp
│ ├── MinDepth.cpp
│ ├── RemoveHalfNodes.cpp
│ ├── SumOfNodes.cpp
│ ├── SymmetricTree.cpp
│ └── TargetPathSum.cpp
├── Bit Manipulation/
│ ├── EarthLevels.cpp
│ ├── ModuloExponentiation.cpp
│ ├── SubsetSumQueries.cpp
│ └── Xoring.cpp
├── Divide and Conquer/
│ ├── 2DArrayMerge.cpp
│ └── BinarySearchUsingRecursion.cpp
├── Dynamic Programming/
│ ├── CoinChange.cpp
│ ├── MinimumPartitioning.cpp
│ ├── OptimalGameStrategy.cpp
│ └── Vacation.cpp
├── Graphs/
│ ├── AllPathsFromSourceToTarget.cpp
│ ├── FindStarInGraph.cpp
│ └── KeysAndRooms.cpp
├── Hashing/
│ ├── ArrayIntersection.cpp
│ └── KSumSubarray.cpp
├── Heaps/
│ ├── MaximumProduct.cpp
│ ├── ReduceArraySizeToHalf.cpp
│ ├── RelativeRanks.cpp
│ └── WeakestRows.cpp
├── Linked List/
│ ├── AlternateMerge.cpp
│ ├── BubbleSortOnLinkedList.cpp
│ ├── DeleteTail.cpp
│ └── KthLastElement.cpp
├── Queues/
│ ├── FirstNonRepeatingLetter.cpp
│ ├── InterleaveTwoHalvesOfQueue.cpp
│ └── SortQueueWithConstantSpace.cpp
├── Recursion/
│ ├── 2DArrayMerge.cpp
│ ├── AllOccurences.cpp
│ ├── BinaryStrings.cpp
│ ├── FriendsParty.cpp
│ ├── PrintIncreasingNumbers.cpp
│ └── TilingProblem.cpp
├── Stacks/
│ ├── DuplicateParenthesis.cpp
│ ├── MaximumRectangularAreaInHistogram.cpp
│ ├── NextGreaterElement.cpp
│ ├── ReverseANumberUsingStack.cpp
│ └── StockSpanProblem.cpp
├── Strings/
│ ├── ArePermutation.cpp
│ ├── BinaryStringToNumber.cpp
│ ├── CheckPalindrome.cpp
│ ├── RemoveDuplicates.cpp
│ ├── StringCompression.cpp
│ └── VowelFind.cpp
├── Trie/
│ └── PrefixStrings.cpp
└── Vectors/
├── MakeZeroes.cpp
├── RotateImage.cpp
├── SortFruits.cpp
└── SortingCabs.cpp
================================================
FILE CONTENTS
================================================
================================================
FILE: DSA Essentials Solutions/2d Arrays/PascalsTriangle.cpp
================================================
//Expected Time Complexity: O(n^3)
//Hint: Use Binomial Coeffiecients
#include<bits/stdc++.h>
using namespace std;
int binomialCoeff(int n, int k);
// Function to print first
// n lines of Pascal's
// Triangle
vector<vector<int>> printPascal(int n)
{
// Iterate through every line and
// print entries in it
vector<vector<int>> res;
for (int line = 0; line < n; line++)
{
// Every line has number of
// integers equal to line
// number
vector<int> v;
for (int i = 0; i <= line; i++)
{v.push_back(binomialCoeff(line, i));}
res.push_back(v);
}
return res;
}
int binomialCoeff(int n, int k)
{
int res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
================================================
FILE: DSA Essentials Solutions/2d Arrays/SubmatrixSum.cpp
================================================
//Hint: Pre Compute Cumilative Sums of every index i,j.
//Expected Time Complexity:
// Pre Computing : O(N^2)
// Queries: O(1)
#include<bits/stdc++.h>
using namespace std;
int sum(vector<vector<int>> v, int sr, int sc, int er, int ec){
int m=v.size();
int n=v[0].size();
// // int aux[m][n];
int M=m;
int N=n;
vector<vector<int>> aux = v;
vector<vector<int>> mat = v;
int tli=sr, tlj=sc, rbi=er, rbj=ec;
for (int i=0; i<N; i++)
aux[0][i] = mat[0][i];
// Do column wise sum
for (int i=1; i<M; i++)
for (int j=0; j<N; j++)
aux[i][j] = mat[i][j] + aux[i-1][j];
// Do row wise sum
for (int i=0; i<M; i++)
for (int j=1; j<N; j++)
aux[i][j] += aux[i][j-1];
int res = aux[rbi][rbj];
// Remove elements between (0, 0) and (tli-1, rbj)
if (tli > 0)
res = res - aux[tli-1][rbj];
// Remove elements between (0, 0) and (rbi, tlj-1)
if (tlj > 0)
res = res - aux[rbi][tlj-1];
// Add aux[tli-1][tlj-1] as elements between (0, 0)
// and (tli-1, tlj-1) are subtracted twice
if (tli > 0 && tlj > 0)
res = res + aux[tli-1][tlj-1];
return res;
}
================================================
FILE: DSA Essentials Solutions/2d Arrays/WavePrint.cpp
================================================
//Expected Time Complexity: O(n^2)
#include<bits/stdc++.h>
using namespace std;
vector<int> WavePrint(int m, int n, vector<vector<int>> arr)
{
vector<int> res;
int i, j = n - 1, wave = 1;
/* m - Ending row index
n - Ending column index
i, j - Iterator
wave - for Direction
wave = 1 - Wave direction down
wave = 0 - Wave direction up */
while (j >= 0) {
// Check whether to go in
// upward or downward
if (wave == 1) {
// Print the element of the matrix
// downward since the value of wave = 1
for (i = 0; i < m; i++)
res.push_back(arr[i][j]);
wave = 0;
j--;
}
else {
// Print the elements of the
// matrix upward since the value
// of wave = 0
for (i = m - 1; i >= 0; i--)
res.push_back(arr[i][j]) ;
wave = 1;
j--;
}
}
return res;
}
================================================
FILE: DSA Essentials Solutions/Arrays/K-rotate.cpp
================================================
//Expected Time Complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
vector<int> kRotate(vector<int> a, int k)
{
vector<int> v;
int n = a.size();
k = k % n;
for(int i = 0; i < n; i++)
{
if(i < k)
{
v.push_back(a[n + i - k]);
}
else
{
v.push_back(a[i - k]);
}
}
return v;
}
================================================
FILE: DSA Essentials Solutions/Arrays/LargestElement.cpp
================================================
//Expected Time Complexity: O(N)
#include<bits/stdc++.h>
using namespace std;
int largestElement(vector<int> A) {
int largestEle = INT_MIN;
for (auto element : A ) {
largestEle = max(largestEle, element);
}
return largestEle;
}
================================================
FILE: DSA Essentials Solutions/Arrays/LowerBound.cpp
================================================
//Expected Time Complexity: O(logN)
//Hint: Binary Search
#include<bits/stdc++.h>
using namespace std;
int lowerBound(vector<int> A, int Val) {
int sz = A.size();
int l = 0, r = (sz-1);
int answer = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (A[mid] > Val) {
r = mid-1;
}
else {
answer = A[mid];
l = mid+1;
}
}
return answer;
}
================================================
FILE: DSA Essentials Solutions/Arrays/MaximumSumSubarray.cpp
================================================
//Time complexity: O(n)
//Hint: Kadane's Algorithm
#include <bits/stdc++.h>
using namespace std ;
int maxSumSubarray(vector<int> A) {
int n = A.size(), max_sum = *min_element(A.begin(), A.end()), curr_sum = 0;
for(int i = 0 ; i < n ; i++){
if(curr_sum + A[i] <= 0){
curr_sum = A[i];
}else{
curr_sum += A[i];
}
max_sum = max(max_sum, curr_sum);
}
return max_sum;
}
================================================
FILE: DSA Essentials Solutions/Arrays/SortedPairSum.cpp
================================================
//Expected Time Complexity: O(N)
//Hint: Two Pointer Approach
#include<bits/stdc++.h>
using namespace std;
pair<int, int> closestSum(vector<int> arr, int x){
int res_l, res_r;
int n = arr.size();
int l = 0, r = n-1, diff = INT_MAX;
while (r > l)
{
if (abs(arr[l] + arr[r] - x) < diff)
{
res_l = l;
res_r = r;
diff = abs(arr[l] + arr[r] - x);
}
if (arr[l] + arr[r] > x)
r--;
else
l++;
}
return {arr[res_l], arr[res_r]};
}
================================================
FILE: DSA Essentials Solutions/Backtracking/N-QueenWays.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
int cnt;
int isSafe(int N, int mat[][20], int r, int c)
{
// return 0 if two queens share the same column
for (int i = 0; i < r; i++)
{
if (mat[i][c] == 1) {
return 0;
}
}
// return 0 if two queens share the same `` diagonal
for (int i = r, j = c; i >= 0 && j >= 0; i--, j--)
{
if (mat[i][j] == 1) {
return 0;
}
}
// return 0 if two queens share the same `/` diagonal
for (int i = r, j = c; i >= 0 && j < N; i--, j++)
{
if (mat[i][j] == 1) {
return 0;
}
}
return 1;
}
void solve(int N,int mat[][20], int r)
{
// if `N` queens are placed successfully, print the solution
if (r == N)
{
cnt++;
return;
}
// place queen at every square in the current row `r`
// and recur for each valid movement
for (int i = 0; i < N; i++)
{
// if no two queens threaten each other
if (isSafe(N, mat, r, i))
{
// place queen on the current square
mat[r][i] = 1;
// recur for the next row
solve(N,mat, r + 1);
// backtrack and remove the queen from the current square
mat[r][i] = 0;
}
}
}
int nQueen(int n){
cnt =0;
int arr[20][20]={0};
solve(n,arr,0);
return cnt;
}
================================================
FILE: DSA Essentials Solutions/Backtracking/RatAndMice.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
void ratchase(vector<string> a,vector<vector<int>> &b,vector<vector<int>> &v,int i,int j,int n,int m){
if(i==n && j==m){
for(int k=0;k<=n;k++){
for(int l=0;l<=m;l++){
v[k][l] = b[k][l];
}cout<<endl;
}
return;
}
if(i!=n && a[i+1][j]!='X' && b[i+1][j]!=1){
b[i+1][j]=1;
ratchase(a,b,v,i+1,j,n,m);
b[i+1][j]=0;
}
if(i>0 && a[i-1][j]!='X' && b[i-1][j]!=1){
b[i-1][j]=1;
ratchase(a,b,v,i-1,j,n,m);
b[i-1][j]=0;
}
if(j!=m && a[i][j+1]!='X' && b[i][j+1]!=1){
b[i][j+1]=1;
ratchase(a,b,v,i,j+1,n,m);
b[i][j+1]=0;
}
if(j>0 && a[i][j-1]!='X' && b[i][j-1]!=1){
b[i][j-1]=1;
ratchase(a,b,v,i,j-1,n,m);
b[i][j-1]=0;
}
return;
}
vector<vector<int>> ratAndMice(vector<string> a) {
int n = a.size();
int m = a[0].size();
vector<vector<int>> v(n, vector<int>(m, 0));
vector<vector<int>> b(n, vector<int>(m, 0));
b[0][0] = 1;
ratchase(a,b,v,0,0,n-1,m-1);
return v;
}
================================================
FILE: DSA Essentials Solutions/Backtracking/UniqueSubset.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
set<vector<int>> s;
void recur(vector<int> &nums, vector<int> ans, int i)
{
if(i == nums.size()){
sort(ans.begin(), ans.end());
s.insert(ans);
return;
}
ans.push_back(nums[i]);
recur(nums, ans, i+1);
ans.pop_back();
recur(nums, ans, i+1);
}
vector<vector<int>> uniqueSubsets(vector<int> nums){
s.clear();
vector<int> ans;
recur(nums, ans, 0);
vector<vector<int>> v;
for(auto x : s)
v.push_back(x);
return v;
}
================================================
FILE: DSA Essentials Solutions/Backtracking/WordBreakProblem.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
int cnt = 0;
vector<string> v;
void help(string s, int n, string res, vector<string> &word)
{
for (int i = 1; i <= n; i++)
{
string ss = s.substr(0, i);
int l = word.size();
bool flag = false;
for (int j = 0; j < l; j++)
if (word[j] == ss)
flag = true;
if (flag)
{
if (i == n)
{
res += ss;
// v.push_back(res);
cnt++;
return;
}
help(s.substr(i, n - i), n - i, res + ss + " ", word);
}
}
}
int wordBreak(string s, vector<string> &dictionary)
{
cnt = 0;
// v.clear();
help(s, s.size(), "", dictionary);
// for (auto x : v) cout << x << '\n';
return cnt;
}
================================================
FILE: DSA Essentials Solutions/Backtracking/WordSearch.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
bool chk;
void recur(vector<vector<char>>& board, string &word, int i, int j, int k)
{
if(k == word.size())
{
chk = true;
return;
}
if(i < 0 || j < 0 || i == board.size() || j == board[0].size() || board[i][j] == '-1')
return;
if(board[i][j] == word[k])
{
char c = board[i][j];
board[i][j] = '-1';
recur(board, word, i, j+1, k+1);
recur(board, word, i+1, j, k+1);
recur(board, word, i, j-1, k+1);
recur(board, word, i-1, j, k+1);
board[i][j] = c;
}
return;
}
bool wordSearch(vector<vector<char>> &board, string word)
{
chk = false;
for(int i=0; i<board.size(); i++)
{
for(int j=0; j<board[0].size(); j++)
{
if(board[i][j]==word[0])
recur(board, word, i, j, 0);
}
}
return chk;
}
================================================
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/Chopsticks.cpp
================================================
//Expected Complexity: O(N logN)
#include<bits/stdc++.h>
using namespace std;
int pairSticks(vector<int> length, int D)
{
// your code goes here
sort(length.begin(), length.end());
int res = 0;
for(int i=0; i<length.size()-1; i++)
{
if (length[i + 1] - length[i] <= D) { res++; i++;}
}
return res;
}
================================================
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/DefenseKingdom.cpp
================================================
//Expected Time Complexity= O(N log N)
//Find the largest distance from both W and H axis and take their product.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()
int defkin(int W, int H, vector<pair<int, int>> position)
{
// your code goes here
vector<pair<int, int>> v = position;
int w = W, h = H;
vector<ll> x, y;
x.push_back(0); y.push_back(0);
ll maxx = INT_MIN, maxy = INT_MIN;
for(int i=0; i<v.size(); i++)
{
x.push_back(v[i].first), y.push_back(v[i].second);
}
// x.push_back(W); y.push_back(H);
sort(all(x));
sort(all(y));
for (ll i = 1; i < x.size(); i++) maxx = max(maxx, x[i] - x[i - 1] - 1);
for (ll i = 1; i < y.size(); i++) maxy = max(maxy, y[i] - y[i - 1] - 1);
maxx = max(maxx, W - x[x.size() - 1] );
maxy = max(maxy, H- y[y.size() - 1]);
return (maxx * maxy);
}
================================================
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/OptimisedBubbleSort.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
vector<int> optimizedBubbleSort(vector<int> arr){
// your code goes here
int i, j, n=arr.size();
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(arr[j], arr[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
return arr;
}
================================================
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/SortingCartesianProducts.cpp
================================================
//Expected Time Complexity :O(N log N)
#include<bits/stdc++.h>
using namespace std;
vector<pair<int, int>> sortCartesian(vector<pair<int, int>> v)
{
sort(v.begin(), v.end());
return v;
}
================================================
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/SortingWithComparator.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
bool compare(int a, int b){
return a > b;
}
vector<int> sortingWithComparator(vector<int> v, bool flag){
// your code goes here
if(flag) sort(v.begin(), v.end());
else sort(v.begin(), v.end(), compare);
return v;
}
================================================
FILE: DSA Essentials Solutions/BinarySearchTree/DeleteInBST.cpp
================================================
#include <iostream>
using namespace std;
class node{
public:
int data;
node*left;
node*right;
node(int d){
data=d;
left=NULL;
right=NULL;
}
};
node* deleteNode(node* root, int k){
// Base case
if (root == NULL)
return root;
// Recursive calls for ancestors of
// node to be deleted
if (root->data > k) {
root->left = deleteNode(root->left, k);
return root;
}
else if (root->data < k) {
root->right = deleteNode(root->right, k);
return root;
}
// We reach here when root is the node
// to be deleted.
// If one of the children is empty
if (root->left == NULL) {
node* temp = root->right;
delete root;
return temp;
}
else if (root->right == NULL) {
node* temp = root->left;
delete root;
return temp;
}
// If both children exist
else {
node* succParent = root;
// Find successor
node* succ = root->right;
while (succ->left != NULL) {
succParent = succ;
succ = succ->left;
}
// Delete successor. Since successor
// is always left child of its parent
// we can safely make successor's right
// right child as left of its parent.
// If there is no succ, then assign
// succ->right to succParent->right
if (succParent != root)
succParent->left = succ->right;
else
succParent->right = succ->right;
// Copy Successor Data to root
root->data = succ->data;
// Delete Successor and return root
delete succ;
return root;
}
}
================================================
FILE: DSA Essentials Solutions/BinarySearchTree/IsBST.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int key;
Node *left;
Node *right;
Node(int key){
this->key = key;
left = right = NULL;
}
};
bool isBSTUtil(Node* node, int min, int max)
{
/* an empty tree is BST */
if (node==NULL)
return true;
/* false if this node violates
the min/max constraint */
if (node->key < min || node->key > max)
return false;
/* otherwise check the subtrees recursively,
tightening the min or max constraint */
return
isBSTUtil(node->left, min, node->key) &&
isBSTUtil(node->right, node->key, max);
}
bool isBST(Node * root){
//complete this method
return isBSTUtil(root, INT_MIN,INT_MAX);
}
================================================
FILE: DSA Essentials Solutions/BinarySearchTree/MirrorABST.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int key;
Node *left;
Node *right;
Node(int key){
this->key = key;
left = right = NULL;
}
};
void mirror(Node* node)
{
if (node == NULL)
return;
else
{
struct Node* temp;
/* do the subtrees */
mirror(node->left);
mirror(node->right);
/* swap the pointers in this node */
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
Node* mirrorBST(Node * root){
//complete this method
mirror(root);
return root;
}
================================================
FILE: DSA Essentials Solutions/BinaryTree/ExpressionTree.cpp
================================================
//Expected Time Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
struct Node {
string key;
Node* left, *right;
};
bool isOp(string data)
{
if(data == "+" or data == "-" or data == "*" or data == "/")
return true;
return false;
}
int evalTree(Node* root){
if(root == NULL) return 0;
if(!isOp(root->key)) return stoi(root->key);
if(root->key == "+") return evalTree(root->left)+evalTree(root->right);
if(root->key == "-") return evalTree(root->left)-evalTree(root->right);
if(root->key == "*") return evalTree(root->left)*evalTree(root->right);
if(root->key == "/") return evalTree(root->left)/evalTree(root->right);
}
================================================
FILE: DSA Essentials Solutions/BinaryTree/K-thLevel.cpp
================================================
//Expected Time Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node* left, *right;
};
vector<int> printKthLevel(Node* root, int k){
// your code goes here
// Create Queue
queue<struct Node*> que;
// Enqueue the root node
que.push(root);
// Create a set
vector<int> s;
// Level to track
// the current level
int level = 0;
int flag = 0;
// Iterate the queue till its not empty
while (!que.empty()) {
// Calculate the number of nodes
// in the current level
int size = que.size();
// Process each node of the current
// level and enqueue their left
// and right child to the queue
while (size--) {
struct Node* ptr = que.front();
que.pop();
// If the current level matches the
// required level then add into set
if (level == k) {
// Flag initialized to 1
flag = 1;
// Inserting node data in set
s.push_back(ptr->key);
}
else {
// Traverse to the left child
if (ptr->left)
que.push(ptr->left);
// Traverse to the right child
if (ptr->right)
que.push(ptr->right);
}
}
// Increment the variable level
// by 1 for each level
level++;
// Break out from the loop
// if the Kth Level is reached
if (flag == 1)
break;
}
return s;
}
================================================
FILE: DSA Essentials Solutions/BinaryTree/MinDepth.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node* left, *right;
};
int minDepth(Node *root) {
// Your code here
int res = INT_MAX;
queue<pair<Node*, int>> q;
int d = 1;
q.push({root, d});
while(!q.empty())
{
Node* f = q.front().first;
d = 1+q.front().second;
if(f->left == NULL && f->right == NULL)
res = min(res, q.front().second);
q.pop();
if(f->left) q.push({f->left, d});
if(f->right) q.push({f->right, d});
}
return res;
}
================================================
FILE: DSA Essentials Solutions/BinaryTree/RemoveHalfNodes.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node* left, *right;
};
void inorder(Node* root, vector<int> &v)
{
if(root == NULL) return;
if(root->left) inorder(root->left, v);
v.push_back(root->key);
if(root->right) inorder(root->right, v);
}
Node *help(Node *root)
{
//add code here.
if(root==NULL) return NULL;
if(root->right) root->right = help(root->right);
if(root->left) root->left = help(root->left);
if( (root->left != NULL && root->right == NULL) or (root->left == NULL && root->right !=NULL) )
{
if(root->left) root = root->left;
else root = root->right;
root = help(root);
}
return root;
}
vector<int> removeHalfNodes(Node *root)
{
//add code here.
root = help(root);
vector<int> v;
inorder(root, v);
return v;
}
================================================
FILE: DSA Essentials Solutions/BinaryTree/SumOfNodes.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node* left, *right;
};
int sumBT(Node* root)
{
// Code here
int res = 0;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
Node* f = q.front();
q.pop();
if(f->left) q.push(f->left);
if(f->right) q.push(f->right);
res += f->key;
}
return res;
}
================================================
FILE: DSA Essentials Solutions/BinaryTree/SymmetricTree.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node* left, *right;
};
bool isSymmetric(Node* root) {
queue<Node*> q1;
queue<Node*> q2;
if(root == NULL || (root->right==NULL && root->left==NULL)) return true;
if(root->right == NULL || root->left == NULL) return false;
q1.push(root->left);
q2.push(root->right);
while(!q1.empty() && !q2.empty())
{
Node* f1 = q1.front();
q1.pop();
Node* f2 = q2.front();
q2.pop();
if(q1.empty() && !q2.empty()) return false;
if(!q1.empty() && q2.empty()) return false;
if(f1->left==NULL && f2->right!=NULL) return false;
if(f1->left!=NULL && f2->right==NULL) return false;
if(f1->key != f2->key) return false;
if(f1->left) q1.push(f1->left);
if(f1->right) q1.push(f1->right);
if(f2->right) q2.push(f2->right);
if(f2->left) q2.push(f2->left);
}
if(q1.empty() && q2.empty()) return true;
return false;
}
================================================
FILE: DSA Essentials Solutions/BinaryTree/TargetPathSum.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
Node* left, *right;
};
vector<vector<int>> vv;
void help(Node* root, int a, vector<int> &v, int b)
{
if(root == NULL) return;
if(root->left == NULL && root->right == NULL)
{
if(a == b+root->val)
{
v.push_back(root->val);
vv.push_back(v);
v.pop_back();
}
return;
}
if(root->left)
{
v.push_back(root->val);
help(root->left, a, v, b+root->val);
v.pop_back();
}
if(root->right)
{
v.push_back(root->val);
help(root->right, a, v, b+root->val);
v.pop_back();
}
}
vector<vector<int>> pathSum(Node* root, int targetSum) {
vv.clear();
vector<int> v;
help(root, targetSum, v, 0);
return vv;
}
================================================
FILE: DSA Essentials Solutions/Bit Manipulation/EarthLevels.cpp
================================================
// Expected Time Complexity : O(Log n)
#include<bits/stdc++.h>
using namespace std;
long long convertDecimalToBinary(unsigned long long int n)
{
long long binaryNumber = 0;
unsigned long long int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
int earthLevel(int k)
{
//your code goes here
unsigned long long int binaryNumber, sum = 0;
binaryNumber = convertDecimalToBinary(k);
while (binaryNumber != 0)
{
unsigned long long int t;
t = binaryNumber%2;
sum = sum + t;
binaryNumber = binaryNumber/10;
}
return sum;
}
================================================
FILE: DSA Essentials Solutions/Bit Manipulation/ModuloExponentiation.cpp
================================================
// Expected Time Complexity : O(Log N)
// Hint: if a is even, than x^a can be written as (x^(a/2))*(x^(a/2))
#include <iostream>
using namespace std;
int power(int x, int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0) return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
================================================
FILE: DSA Essentials Solutions/Bit Manipulation/SubsetSumQueries.cpp
================================================
// Expected Time Complexity : O(1) for each query
#include<bits/stdc++.h>
using namespace std;
vector<bool> subsetSum(vector<int> v, vector<int> q)
{
int n = q.size();
vector<bool> b(n);
bitset<10000> bit;
bit.reset();
bit[0] = 1;
for (int i = 0; i < v.size(); ++i)
bit |= (bit << v[i]);
for(int i=0; i<n; i++)
{
int x = q[i];
bit[x]? b[i]=true : b[i]=false;
}
return b;
}
================================================
FILE: DSA Essentials Solutions/Bit Manipulation/Xoring.cpp
================================================
// Expected Time Complexity : O(N)
// xor of two same elements is 0
#include<bits/stdc++.h>
using namespace std;
int xoring(vector<int> v)
{
int res=0;
for(auto x : v) res ^= x;
return res;
}
================================================
FILE: DSA Essentials Solutions/Divide and Conquer/2DArrayMerge.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
void merge_row(vector<vector<int>> &mat,int i, int cs, int cm, int ce){
vector<int> sorted;
int x=cs;
int y=cm+1;
//cout<<x<<" "<<cm<<" "<<y<<" "<<ce<<endl;
while(x<=cm && y<=ce){
if(mat[i][x]<mat[i][y]){
sorted.push_back(mat[i][x]);
x++;
}
else{
sorted.push_back(mat[i][y]);
y++;
}
}
while(x<=cm){
sorted.push_back(mat[i][x]);
x++;
}
while(y<=ce){
sorted.push_back(mat[i][y]);
y++;
}
int k=0;
for(int j=cs; j<=ce; j++){
mat[i][j]=sorted[k];
k++;
}
return;
}
void merge_col(vector<vector<int>> &mat,int j, int rs, int rm, int re){
vector<int> sorted;
int x=rs;
int y=rm+1;
while(x<=rm && y<=re){
if(mat[x][j]<mat[y][j]){
sorted.push_back(mat[x][j]);
x++;
}
else{
sorted.push_back(mat[y][j]);
y++;
}
}
while(x<=rm){
sorted.push_back(mat[x][j]);
x++;
}
while(y<=re){
sorted.push_back(mat[y][j]);
y++;
}
int k=0;
for(int i=rs; i<=re; i++){
mat[i][j]=sorted[k];
k++;
}
return;
}
void merge(int m, int n, vector<vector<int>> &mat, int rs, int rm, int re,int cs, int cm, int ce){
//for sorting rows
for(int i=rs; i<=re; i++){
merge_row(mat,i,cs,cm,ce);
}
//for sorting columns
for(int j=cs; j<=ce; j++){
merge_col(mat,j,rs,rm,re);
}
return;
}
void merge_sort(int m, int n, vector<vector<int>> &mat, int rs, int re, int cs, int ce){
//cout<<rs<<" "<<re<<endl;
//cout<<cs<<" "<<ce<<endl;
if(rs>=re && cs>=ce){
return;
}
int rm=(rs+re)/2;
int cm=(cs+ce)/2;
// cout<<rs<<" "<<rm<<" "<<re<<" "<<cs<<" "<<cm<<" "<<ce<<endl;
//for dividing into subarrays
merge_sort(m,n,mat,rs,rm,cs,cm);
merge_sort(m,n,mat,rm+1,re,cs,cm);
merge_sort(m,n,mat,rs,rm,cm+1,ce);
merge_sort(m,n,mat,rm+1,re,cm+1,ce);
//for merging sorted subarrays
merge(m,n,mat,rs,rm,re,cs,cm,ce);
return;
}
vector<vector<int>> mergeSort(int m, int n, vector<vector<int>> v){
merge_sort(m,n,v,0,m-1,0,n-1);
return v;
}
================================================
FILE: DSA Essentials Solutions/Divide and Conquer/BinarySearchUsingRecursion.cpp
================================================
//Expected Time Complexity: O(logn)
#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binary(vector<int> arr, int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binary(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binary(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
int binarySearch(vector<int> v, int x)
{
// your code goes here
int n = v.size();
int result = binary(v, 0, n - 1, x);
return result;
}
================================================
FILE: DSA Essentials Solutions/Dynamic Programming/CoinChange.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
long long coinChange(int s, int n , vector<int> a, long long dp[500][100])
{
if (n < 0 || s < 0) return 0;
if (s == 0) return 1;
if (dp[s][n] != 0) return dp[s][n];
long long op1 = coinChange(s, n - 1, a, dp);
long long op2 = coinChange(s - a[n], n, a, dp);
return dp[s][n] = op1 + op2;
}
long long findCombinations(int n, vector<int> coins)
{
long long dp[500][100] = {{0}};
return coinChange(n, coins.size()-1, coins, dp);
}
================================================
FILE: DSA Essentials Solutions/Dynamic Programming/MinimumPartitioning.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
int findMin(vector<int> arr)
{
int n = arr.size();
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
// Create an array to store results of subproblems
bool dp[n + 1][sum + 1];
// Initialize first column as true. 0 sum is possible
// with all elements.
for (int i = 0; i <= n; i++)
dp[i][0] = true;
// Initialize top row, except dp[0][0], as false. With
// 0 elements, no other sum except 0 is possible
for (int i = 1; i <= sum; i++)
dp[0][i] = false;
// Fill the partition table in bottom up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
// If i'th element is excluded
dp[i][j] = dp[i - 1][j];
// If i'th element is included
if (arr[i - 1] <= j)
dp[i][j] |= dp[i - 1][j - arr[i - 1]];
}
}
// Initialize difference of two sums.
int diff = INT_MAX;
// Find the largest j such that dp[n][j]
// is true where j loops from sum/2 t0 0
for (int j = sum / 2; j >= 0; j--) {
// Find the
if (dp[n][j] == true) {
diff = sum - 2 * j;
break;
}
}
return diff;
}
================================================
FILE: DSA Essentials Solutions/Dynamic Programming/OptimalGameStrategy.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
int game(int n, vector<int> v, int s, int e){
if(s==e || s==e-1){
return max(v[s],v[e]);
}
int op1=v[s] + min(game(n,v,s+2,e),game(n,v,s+1,e-1));
int op2=v[e] + min(game(n,v,s+1,e-1),game(n,v,s,e-2));
return max(op1,op2);
}
int MaxValue(int n, vector<int> v){
int res=game(n,v,0,n-1);
return res;
}
================================================
FILE: DSA Essentials Solutions/Dynamic Programming/Vacation.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using mi = map<int, int>;
using ml = map<ll, ll>;
using umi = unordered_map<int, int>;
using uml = unordered_map<ll, ll>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using ti = tuple<int, int, int>;
using tl = tuple<ll, ll, ll>;
using vii = vector<pi>;
using viii = vector<ti>;
using vll = vector<pl>;
using vlll = vector<tl>;
#define mem(dp) memset(dp, -1, sizeof(dp))
#define aut(a, b) for (auto&(a) : (b))
#define out(x, v) for (auto&(x) : (v)) cout << x << " "; cout << '\n';
#define rep(i, n) for (ll (i) = 0; (i) < (n); ++(i) )
#define repp(i, n) for (ll (i) = 1; (i) <= (n); ++(i) )
#define all(v) v.begin(), v.end()
#define fi get<0>
#define se get<1>
#define th get<2>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
int topDown(viii &v, int n, int i, int dp[][4], int prev)
{
if (i == n) return 0;
if (dp[i][prev] != -1) return dp[i][prev];
int op1 = INT_MIN, op2 = INT_MIN, op3 = INT_MIN;
if (prev != 1) op1 = fi(v[i]) + topDown(v, n, i + 1, dp, 1);
if (prev != 2) op2 = se(v[i]) + topDown(v, n, i + 1, dp, 2);
if (prev != 3) op3 = th(v[i]) + topDown(v, n, i + 1, dp, 3);
return dp[i][prev] = max(op1, max(op2, op3));
}
int vacation(vector<int> a, vector<int> b, vector<int> c)
{
viii v;
int n = a.size();
rep(i, n)
{
v.pb(mt(a[i], b[i], c[i]));
}
int dp[n][4];
mem(dp);
return topDown(v, n, 0, dp, 0);
}
================================================
FILE: DSA Essentials Solutions/Graphs/AllPathsFromSourceToTarget.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>>& graph, vector<vector<int>>& result, vector<int> path, int src, int dst) {
path.push_back(src);
if(src == dst) {
result.push_back(path);
return;
}
for(auto node : graph[src])
dfs(graph, result, path, node, dst);
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> paths; vector<int> path;
int nodes = graph.size();
if(nodes == 0) return paths;
dfs(graph, paths, path, 0, nodes - 1);
return paths;
}
================================================
FILE: DSA Essentials Solutions/Graphs/FindStarInGraph.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
int findCenter(vector<vector<int>>& v) {
pair<int, int> f={v[0][0],v[0][1]};
pair<int, int> s={v[1][0],v[1][1]};
if(f.first==s.first){
return f.first;
}
else if(f. first==s.second){
return f.first;
}
else if(f.second==s.first){
return f.second;
}
else{
return f.second;
}
}
================================================
FILE: DSA Essentials Solutions/Graphs/KeysAndRooms.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
bool canVisitAllRooms(vector<vector<int>>& rooms) {
unordered_map<int,bool> map;
int n=rooms.size();
for(int i=0;i<n;i++){
map[i]=false;
}
queue<int> q;
q.push(0);
while(!q.empty()){
int k=q.size();
while(k--){
int a=q.front();
q.pop();
map[a]=true;
for(int j=0;j<rooms[a].size() && rooms[a].size()!=0;j++){
if(map[rooms[a][j]]==false){
q.push(rooms[a][j]);
}
}
}
}
for(auto i:map){
if(i.second==false){
return false;
}
}
return true;
}
================================================
FILE: DSA Essentials Solutions/Hashing/ArrayIntersection.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int,int> map;
vector<int> result;
for(int i=0;i<nums1.size();i++)
{
map[nums1[i]]++;
}
for(int i=0;i<nums2.size();i++)
{
if(map[nums2[i]] > 0)
{
result.push_back(nums2[i]);
map[nums2[i]] = 0;
}
}
sort(result.begin(), result.end());
return result;
}
================================================
FILE: DSA Essentials Solutions/Hashing/KSumSubarray.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
int longestSubarrayKSum(vector<int> arr,int k){
int n = arr.size();
unordered_map<int,int> m;
int pre = 0;
int len = 0;
for(int i=0;i<n;i++){
pre += arr[i];
if(pre==k){
len = max(len,i+1);
}
if(m.find(pre-k)!=m.end()){
len = max(len,i - m[pre-k]);
}
else{
//store the first occ
m[pre] = i;
}
}
return len;
}
================================================
FILE: DSA Essentials Solutions/Heaps/MaximumProduct.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
int maxProduct(vector<int>& nums) {
priority_queue<int> q;
for(int i=0; i<nums.size(); i++){
q.push((nums[i]-1));
}
int p=q.top();
q.pop();
return p*q.top();
}
================================================
FILE: DSA Essentials Solutions/Heaps/ReduceArraySizeToHalf.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
int minSetSize(vector<int>& arr) {
int n=arr.size();
priority_queue<int> q;
unordered_map<int,int> mp;
for(int i=0; i<n; i++){
mp[arr[i]]++;
}
for(auto pair:mp){
q.push(pair.second);
}
int sum=0;
int cnt=0;
while(n-sum>n/2){
sum+=q.top();
q.pop();
cnt++;
}
return cnt;
}
================================================
FILE: DSA Essentials Solutions/Heaps/RelativeRanks.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
vector<string> findRelativeRanks(vector<int>& score) {
priority_queue<pair<int,int>> pq;
for(int i=0; i<score.size(); i++){
pq.push({score[i],i});
}
int n= score.size();
vector<string> vec(n);
int cnt=0;
while(!pq.empty()){
cnt++;
if(cnt==1){
cout<<"hey"<<endl;
vec[pq.top().second].append("Gold Medal");
}
else if(cnt==2){
vec[pq.top().second].append("Silver Medal");
}
else if(cnt==3){
vec[pq.top().second].append("Bronze Medal");
}
else {
vec[pq.top().second].append(to_string(cnt));
}
pq.pop();
}
return vec;
}
================================================
FILE: DSA Essentials Solutions/Heaps/WeakestRows.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
priority_queue <pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> > pq; //Min-heap
for(int i=0;i<mat.size();i++)
{
int count=0;
for(int j=0;j<mat[i].size();j++)
{
if(mat[i][j]==1)
{
count++; //Counting the number of soldiers in each case
}
}
pq.push(make_pair(count,i));
}
vector<int> x;
while(k>0)
{
pair<int,int> temp=pq.top();
x.push_back(temp.second);
pq.pop();
k--;
}
return x;
}
================================================
FILE: DSA Essentials Solutions/Linked List/AlternateMerge.cpp
================================================
//Expected Time Complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
}
};
node* apend(node* root, int d){
if(root == NULL) return new node(d);
node* temp = root;
while(temp->next){
temp = temp->next;
}
temp->next = new node(d);
return root;
}
node* alternateMerge(node * root1, node* root2){
//Complete this function
node* root = NULL;
if(!root1) return root2;
if(!root2) return root1;
while(root1 && root2){
root = apend(root, root1->data);
root = apend(root, root2->data);
root1 = root1->next;
root2 = root2->next;
}
if(root1){
while(root1){
root = apend(root, root1->data);
root1 = root1->next;
}
}
if(root2){
while(root2){
root = apend(root, root2->data);
root2 = root2->next;
}
}
return root;
}
================================================
FILE: DSA Essentials Solutions/Linked List/BubbleSortOnLinkedList.cpp
================================================
//Expected Time Complexity: O(n^2)
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
node(int data)
{
this->data = data;
this->next = NULL;
}
};
int len(node* head)
{
node* temp = head ;
int i = 0 ;
while(temp!=NULL)
{
i++;
temp=temp->next ;
}
return i ;
}
node* bubble_sort_LinkedList_itr(node* head)
{
int n = len(head)-1;
while(n--)
{
node* prev =NULL;
node*cur = head;
while(cur->next!=NULL)
{
if(cur->data >=cur->next->data)
{
if(prev==NULL)
{
//first node
node* nxt = cur->next ;
cur->next = nxt->next ;
nxt->next = cur ;
prev=nxt ;
head = prev ;
}
else
{
node* nxt = cur->next ;
prev->next = nxt ;
cur->next = nxt->next ;
nxt->next = cur ;
prev = nxt ;
}
}
else
{
prev = cur ;
cur=cur->next ;
}
}
}
return head ;
}
================================================
FILE: DSA Essentials Solutions/Linked List/DeleteTail.cpp
================================================
//Expected Time Complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
}
};
node* deleteTail(node * head){
//Complete this function
if (head == NULL)
return NULL;
if (head->next == NULL) {
delete head;
return NULL;
}
// Find the second last node
node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
// Delete last node
delete (second_last->next);
// Change next of second last
second_last->next = NULL;
return head;
}
================================================
FILE: DSA Essentials Solutions/Linked List/KthLastElement.cpp
================================================
//Expected Time Complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
}
};
int kthLastElement(node * head,int k){
//Complete this function to return kth last element
node * fast = head;
node * slow = head;
int cnt = 0;
while(cnt < k){
fast = fast->next;
cnt++;
}
while(fast!=NULL){
slow = slow->next;
fast = fast->next;
}
return slow->data;
}
================================================
FILE: DSA Essentials Solutions/Queues/FirstNonRepeatingLetter.cpp
================================================
//Expected Time Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
vector<char> firstnonrepeating(vector<char> str)
{
queue<char> q;
vector<char> v;
int charCount[MAX_CHAR] = { 0 };
for (int i = 0; i<str.size(); i++) {
q.push(str[i]);
charCount[str[i] - 'a']++;
while (!q.empty()) {
if (charCount[q.front() - 'a'] > 1)
q.pop();
else {
v.push_back(q.front());
break;
}
}
if (q.empty())
v.push_back('0');
}
return v;
}
================================================
FILE: DSA Essentials Solutions/Queues/InterleaveTwoHalvesOfQueue.cpp
================================================
//Expected Time Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
queue<int> interLeave(queue<int> q){
int n=q.size();
queue<int> q1, q2;
for (int i=0;i<(n/2);i++) {
q1.push(q.front());
q.pop(); //Expected Time Complexity: O(2^n)
}
for (int i=0;i<(n/2);i++) {
q2.push(q.front());
q.pop();
}
for (int i=0;i<(n/2);i++) {
q.push(q1.front());
q1.pop();
q.push(q2.front());
q2.pop();
}
return q;
}
================================================
FILE: DSA Essentials Solutions/Queues/SortQueueWithConstantSpace.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
int minIndex(queue<int> &q, int sortedIndex)
{
int min_index = -1;
int min_val = INT_MAX;
int n = q.size();
for (int i=0; i<n; i++)
{
int curr = q.front();
q.pop();
if (curr <= min_val && i <= sortedIndex)
{
min_index = i;
min_val = curr;
}
q.push(curr);
}
return min_index;
}
void insertMinToRear(queue<int> &q, int min_index)
{
int min_val;
int n = q.size();
for (int i = 0; i < n; i++)
{
int curr = q.front();
q.pop();
if (i != min_index)
q.push(curr);
else
min_val = curr;
}
q.push(min_val);
}
void sortQueue(queue<int> &q)
{
for (int i = 1; i <= q.size(); i++)
{
int min_index = minIndex(q, q.size() - i);
insertMinToRear(q, min_index);
}
}
queue<int> sortqueue(queue<int> &q)
{
sortQueue(q);
return q;
}
================================================
FILE: DSA Essentials Solutions/Recursion/2DArrayMerge.cpp
================================================
Hint: Divide, sort row-wise, sort col-wise
#include<bits/stdc++.h>
using namespace std;
void merge_row(vector<vector<int>> &mat,int i, int cs, int cm, int ce){
vector<int> sorted;
int x=cs;
int y=cm+1;
//cout<<x<<" "<<cm<<" "<<y<<" "<<ce<<endl;
while(x<=cm && y<=ce){
if(mat[i][x]<mat[i][y]){
sorted.push_back(mat[i][x]);
x++;
}
else{
sorted.push_back(mat[i][y]);
y++;
}
}
while(x<=cm){
sorted.push_back(mat[i][x]);
x++;
}
while(y<=ce){
sorted.push_back(mat[i][y]);
y++;
}
int k=0;
for(int j=cs; j<=ce; j++){
mat[i][j]=sorted[k];
k++;
}
return;
}
void merge_col(vector<vector<int>> &mat,int j, int rs, int rm, int re){
vector<int> sorted;
int x=rs;
int y=rm+1;
while(x<=rm && y<=re){
if(mat[x][j]<mat[y][j]){
sorted.push_back(mat[x][j]);
x++;
}
else{
sorted.push_back(mat[y][j]);
y++;
}
}
while(x<=rm){
sorted.push_back(mat[x][j]);
x++;
}
while(y<=re){
sorted.push_back(mat[y][j]);
y++;
}
int k=0;
for(int i=rs; i<=re; i++){
mat[i][j]=sorted[k];
k++;
}
return;
}
void merge(int m, int n, vector<vector<int>> &mat, int rs, int rm, int re,int cs, int cm, int ce){
//for sorting rows
for(int i=rs; i<=re; i++){
merge_row(mat,i,cs,cm,ce);
}
//for sorting columns
for(int j=cs; j<=ce; j++){
merge_col(mat,j,rs,rm,re);
}
return;
}
void merge_sort(int m, int n, vector<vector<int>> &mat, int rs, int re, int cs, int ce){
//cout<<rs<<" "<<re<<endl;
//cout<<cs<<" "<<ce<<endl;
if(rs>=re && cs>=ce){
return;
}
int rm=(rs+re)/2;
int cm=(cs+ce)/2;
// cout<<rs<<" "<<rm<<" "<<re<<" "<<cs<<" "<<cm<<" "<<ce<<endl;
//for dividing into subarrays
merge_sort(m,n,mat,rs,rm,cs,cm);
merge_sort(m,n,mat,rm+1,re,cs,cm);
merge_sort(m,n,mat,rs,rm,cm+1,ce);
merge_sort(m,n,mat,rm+1,re,cm+1,ce);
//for merging sorted subarrays
merge(m,n,mat,rs,rm,re,cs,cm,ce);
return;
}
vector<vector<int>> mergeSort(int m, int n, vector<vector<int>> v){
merge_sort(m,n,v,0,m-1,0,n-1);
return v;
}
================================================
FILE: DSA Essentials Solutions/Recursion/AllOccurences.cpp
================================================
//Expected Time Complexity: O(N)
#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
void helper(int k, vector<int> v, int i){
if(i==v.size()){
return;
}
if(v[i]==k){
vec.push_back(i);
}
helper(k,v,i+1);
return;
}
vector<int> findAllOccurences(int k, vector<int> v){
vec.clear();
helper(k,v,0);
return vec;
}
================================================
FILE: DSA Essentials Solutions/Recursion/BinaryStrings.cpp
================================================
//Expected Time Complexity: O(2^n)
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
void helper(string str,int n,int i){
if(i==n){
v.push_back(str);
return;
}
string s1= str;
s1.push_back('0');
helper(s1,n,i+1);
if(i>0 && str[i-1]=='0'){
str.push_back('1');
helper(str,n,i+1);
}
else if(i==0){
str.push_back('1');
helper(str,n,i+1);
}
return;
}
vector<string> binaryStrings(int n){
v.clear();
string str;
helper(str,n,0);
return v;
}
================================================
FILE: DSA Essentials Solutions/Recursion/FriendsParty.cpp
================================================
//Expected Time Complexity: O(2^n)
#include <iostream>
using namespace std;
int help(int n)
{
if (n <= 0) return 0;
if(n == 2 || n == 1) return n;
return help(n - 1) + (n - 1) * help(n - 2);
}
int friendsPairing(int n){
return help(n);
}
================================================
FILE: DSA Essentials Solutions/Recursion/PrintIncreasingNumbers.cpp
================================================
//Expected Time Complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
void help(int i, int n, vector<int> &v)
{
if(i > n) return;
v.push_back(i);
help(i+1, n, v);
}
vector<int> increasingNumbers(int N) {
vector<int> v;
help(1, N, v);
return v;
}
================================================
FILE: DSA Essentials Solutions/Recursion/TilingProblem.cpp
================================================
//Expected Time Complexity: O(2^n)
#include <iostream>
using namespace std;
int tiles(int n,int m){
if(n<m) return 1;
int op1 = tiles(n-1, m);
int op2 = tiles(n-m, m);
return (op1 + op2);
}
int tillingProblem(int n, int m){
return tiles(n, m);
}
================================================
FILE: DSA Essentials Solutions/Stacks/DuplicateParenthesis.cpp
================================================
//Expected Time Complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
bool duplicateParentheses(string str){
stack<char> Stack;
for (char ch : str)
{
if (ch == ')')
{
char top = Stack.top();
Stack.pop();
int elementsInside = 0;
while (top != '(')
{
elementsInside++;
top = Stack.top();
Stack.pop();
}
if(elementsInside < 1) {
return true;
}
}
else
Stack.push(ch);
}
return false;
}
================================================
FILE: DSA Essentials Solutions/Stacks/MaximumRectangularAreaInHistogram.cpp
================================================
//Expected Time Complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
int getMaxArea(vector<int> hist)
{
int n = hist.size();
stack<int> s;
int max_area = 0;
int tp;
int area_with_top;
int i = 0;
while (i < n)
{
if (s.empty() || hist[s.top()] <= hist[i])
s.push(i++);
else
{
tp = s.top();
s.pop();
area_with_top = hist[tp] * (s.empty() ? i :
i - s.top() - 1);
if (max_area < area_with_top)
max_area = area_with_top;
}
}
while (s.empty() == false)
{
tp = s.top();
s.pop();
area_with_top = hist[tp] * (s.empty() ? i :
i - s.top() - 1);
if (max_area < area_with_top)
max_area = area_with_top;
}
return max_area;
}
================================================
FILE: DSA Essentials Solutions/Stacks/NextGreaterElement.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
vector<int> nextGreaterElement(vector<int> arr){
int n = arr.size();
vector<int> arr1(n, 0);
stack<int> s;
for (int i = n - 1; i >= 0; i--)
{
while (!s.empty() && s.top() <= arr[i])
s.pop();
if (s.empty())
arr1[i] = -1;
else
arr1[i] = s.top();
s.push(arr[i]);
}
return arr1;
}
================================================
FILE: DSA Essentials Solutions/Stacks/ReverseANumberUsingStack.cpp
================================================
//Expected Time Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
int reverse(int n){
int number = n;
stack <int> st;
while (number != 0)
{
st.push(number % 10);
number = number / 10;
}
int rev = 0;
int i = 1;
while (!st.empty())
{
rev = rev + (st.top() * i);
st.pop();
i = i * 10;
}
return rev;
}
================================================
FILE: DSA Essentials Solutions/Stacks/StockSpanProblem.cpp
================================================
//Expected Time Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
vector<int> stockSpanner(vector<int> &a){
stack <int> s;
int n = a.size();
s.push(0);
vector<int> arr(n, 1);
for (int i = 1; i < n; i++) {
while (!s.empty() and a[s.top()] <= a[i]) {
s.pop();
}
if (!s.empty()) {
arr[i] = i - s.top();
} else arr[i] = i + 1;
s.push(i);
}
return arr;
}
================================================
FILE: DSA Essentials Solutions/Strings/ArePermutation.cpp
================================================
//Expected Time Complexity= O(N log N)
//Hint: Permuatations are just different arrangements of same alphabets. Can you make the arrangement same?
#include <bits/stdc++.h>
using namespace std;
bool arePermutation(string str1, string str2)
{
// Get lenghts of both strings
int n1 = str1.length();
int n2 = str2.length();
// If length of both strings is not same,
// then they cannot be Permutation
if (n1 != n2)
return false;
// Sort both strings
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
// Compare sorted strings
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
================================================
FILE: DSA Essentials Solutions/Strings/BinaryStringToNumber.cpp
================================================
Expected Time Complexity : O(N)
#include <iostream>
#include <string>
using namespace std;
// Function to convert binary to decimal
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
================================================
FILE: DSA Essentials Solutions/Strings/CheckPalindrome.cpp
================================================
Expected Time Complexity : O(N)
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(string str)
{
// Start from leftmost and rightmost corners of str
int l = 0;
int h = str.length() - 1;
// Keep comparing characters while they are same
while (h > l)
{
if (str[l++] != str[h--])
{
return false;
}
}
return true;
}
================================================
FILE: DSA Essentials Solutions/Strings/RemoveDuplicates.cpp
================================================
Expected Time Complexity : O(N)
#include <bits/stdc++.h>
using namespace std;
string removeDuplicate(string s){
// your code goes here
set<char> ss(s.begin(), s.end());
string str;
for (auto x : ss)
str.push_back(x);
return str;
}
================================================
FILE: DSA Essentials Solutions/Strings/StringCompression.cpp
================================================
Expected Time Complexity : O(N)
#include<bits/stdc++.h>
using namespace std;
int compress(vector<char>& chars) {
int count_=1;
string ans;
for(int i=0;i<chars.size();i++)
{
while(i<chars.size()-1 && chars[i+1] == chars[i])
{
count_++;
i++;
}
ans += chars[i];
if(count_ == 1)
{
continue;
}
ans += to_string(count_);
count_ = 1;
}
chars.clear();
for(int i=0;i<ans.size();i++)
{
chars.push_back(ans[i]);
}
return chars.size();
}
================================================
FILE: DSA Essentials Solutions/Strings/VowelFind.cpp
================================================
Expected Time Complexity : O(N)
#include<bits/stdc++.h>
using namespace std;
string vowel(string S){
// your code goes here
string out;
for(auto x : S){
if(x=='a' || x=='e' || x=='i' || x=='o' || x=='u') out.push_back(x);
}
return out;
}
================================================
FILE: DSA Essentials Solutions/Trie/PrefixStrings.cpp
================================================
#include <bits/stdc++.h>
using namespace std;
class node{
public:
char ch;
unordered_map<char,node*> next;
bool isTerminal;
node(char a){
ch=a;
bool isTerminal=false;
}
};
class Trie{
public:
node*root= new node('\0');
void insert(string str){
node*temp=root;
for(int i=0; i<str.length(); i++){
if(temp->next.count(str[i])==0){
temp->next[str[i]]=new node(str[i]);
}
temp=temp->next[str[i]];
}
temp->isTerminal=true;
return;
}
void dfs(node*temp, vector<string> &v, string word ){
if(temp->isTerminal){
v.push_back(word);
}
if(temp->next.empty()){
return;
}
for(auto p:temp->next){
word.push_back(p.first);
dfs(temp->next[p.first],v,word);
word.pop_back();
}
return;
}
vector<string> find(string str){
vector<string> v;
node* temp=root;
string word="";
for(int i=0; i<str.length(); i++){
if(temp->next.count(str[i])==0){
return v;
}
word.push_back(str[i]);
temp=temp->next[str[i]];
}
if(temp->isTerminal){
v.push_back(word);
}
dfs(temp,v,word);
sort(v.begin(),v.end());
return v;
}
};
vector<string> findPrefixStrings(vector<string> words, string prefix){
Trie t;
for(auto s:words){
t.insert(s);
}
vector<string> res=t.find(prefix);
return res;
}
================================================
FILE: DSA Essentials Solutions/Vectors/MakeZeroes.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> makeZeroes(vector<vector<int>> arr){
// your code goes here
vector<int> r,c;
int n = arr.size(), m = arr[0].size();
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(arr[i][j] == 0){
r.push_back(i); c.push_back(j);
}
}
}
for(auto x : r){
for(int i=0; i<n; i++){
arr[x][i] = 0;
}
}
for(auto x : c){
for(int i=0; i<n; i++){
arr[i][x] = 0;
}
}
return arr;
}
================================================
FILE: DSA Essentials Solutions/Vectors/RotateImage.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
int a = 0;
int b = n-1;
while(a<b){
for(int i=0;i<(b-a);++i){
swap(matrix[a][a+i], matrix[a+i][b]);
swap(matrix[a][a+i], matrix[b][b-i]);
swap(matrix[a][a+i], matrix[b-i][a]);
}
++a;
--b;
}
}
================================================
FILE: DSA Essentials Solutions/Vectors/SortFruits.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
bool comp(pair<string,int> a, pair<string, int> b){
return a.second < b.second;
}
vector<pair<string,int>> sortFruits(vector<pair<string,int>> v, string S){
// your code goes here
if(S=="name") sort(v.begin(), v.end());
else sort(v.begin(), v.end(), comp);
return v;
}
================================================
FILE: DSA Essentials Solutions/Vectors/SortingCabs.cpp
================================================
#include<bits/stdc++.h>
using namespace std;
bool comp(pair<int,int> a, pair<int, int> b){
float x = sqrt((a.first*a.first) + (a.second*a.second));
float y = sqrt((b.first*b.first) + (b.second*b.second));
return x < y;
}
vector<pair<int,int>> sortCabs(vector<pair<int,int>> v){
// your code goes here
sort(v.begin(), v.end(), comp);
return v;
}
gitextract_2mb6elmx/
└── DSA Essentials Solutions/
├── 2d Arrays/
│ ├── PascalsTriangle.cpp
│ ├── SubmatrixSum.cpp
│ └── WavePrint.cpp
├── Arrays/
│ ├── K-rotate.cpp
│ ├── LargestElement.cpp
│ ├── LowerBound.cpp
│ ├── MaximumSumSubarray.cpp
│ └── SortedPairSum.cpp
├── Backtracking/
│ ├── N-QueenWays.cpp
│ ├── RatAndMice.cpp
│ ├── UniqueSubset.cpp
│ ├── WordBreakProblem.cpp
│ └── WordSearch.cpp
├── Basic Sorting Algorithms/
│ ├── Chopsticks.cpp
│ ├── DefenseKingdom.cpp
│ ├── OptimisedBubbleSort.cpp
│ ├── SortingCartesianProducts.cpp
│ └── SortingWithComparator.cpp
├── BinarySearchTree/
│ ├── DeleteInBST.cpp
│ ├── IsBST.cpp
│ └── MirrorABST.cpp
├── BinaryTree/
│ ├── ExpressionTree.cpp
│ ├── K-thLevel.cpp
│ ├── MinDepth.cpp
│ ├── RemoveHalfNodes.cpp
│ ├── SumOfNodes.cpp
│ ├── SymmetricTree.cpp
│ └── TargetPathSum.cpp
├── Bit Manipulation/
│ ├── EarthLevels.cpp
│ ├── ModuloExponentiation.cpp
│ ├── SubsetSumQueries.cpp
│ └── Xoring.cpp
├── Divide and Conquer/
│ ├── 2DArrayMerge.cpp
│ └── BinarySearchUsingRecursion.cpp
├── Dynamic Programming/
│ ├── CoinChange.cpp
│ ├── MinimumPartitioning.cpp
│ ├── OptimalGameStrategy.cpp
│ └── Vacation.cpp
├── Graphs/
│ ├── AllPathsFromSourceToTarget.cpp
│ ├── FindStarInGraph.cpp
│ └── KeysAndRooms.cpp
├── Hashing/
│ ├── ArrayIntersection.cpp
│ └── KSumSubarray.cpp
├── Heaps/
│ ├── MaximumProduct.cpp
│ ├── ReduceArraySizeToHalf.cpp
│ ├── RelativeRanks.cpp
│ └── WeakestRows.cpp
├── Linked List/
│ ├── AlternateMerge.cpp
│ ├── BubbleSortOnLinkedList.cpp
│ ├── DeleteTail.cpp
│ └── KthLastElement.cpp
├── Queues/
│ ├── FirstNonRepeatingLetter.cpp
│ ├── InterleaveTwoHalvesOfQueue.cpp
│ └── SortQueueWithConstantSpace.cpp
├── Recursion/
│ ├── 2DArrayMerge.cpp
│ ├── AllOccurences.cpp
│ ├── BinaryStrings.cpp
│ ├── FriendsParty.cpp
│ ├── PrintIncreasingNumbers.cpp
│ └── TilingProblem.cpp
├── Stacks/
│ ├── DuplicateParenthesis.cpp
│ ├── MaximumRectangularAreaInHistogram.cpp
│ ├── NextGreaterElement.cpp
│ ├── ReverseANumberUsingStack.cpp
│ └── StockSpanProblem.cpp
├── Strings/
│ ├── ArePermutation.cpp
│ ├── BinaryStringToNumber.cpp
│ ├── CheckPalindrome.cpp
│ ├── RemoveDuplicates.cpp
│ ├── StringCompression.cpp
│ └── VowelFind.cpp
├── Trie/
│ └── PrefixStrings.cpp
└── Vectors/
├── MakeZeroes.cpp
├── RotateImage.cpp
├── SortFruits.cpp
└── SortingCabs.cpp
SYMBOL INDEX (143 symbols across 76 files)
FILE: DSA Essentials Solutions/2d Arrays/PascalsTriangle.cpp
function printPascal (line 14) | vector<vector<int>> printPascal(int n)
function binomialCoeff (line 33) | int binomialCoeff(int n, int k)
FILE: DSA Essentials Solutions/2d Arrays/SubmatrixSum.cpp
function sum (line 10) | int sum(vector<vector<int>> v, int sr, int sc, int er, int ec){
FILE: DSA Essentials Solutions/2d Arrays/WavePrint.cpp
function WavePrint (line 7) | vector<int> WavePrint(int m, int n, vector<vector<int>> arr)
FILE: DSA Essentials Solutions/Arrays/K-rotate.cpp
function kRotate (line 6) | vector<int> kRotate(vector<int> a, int k)
FILE: DSA Essentials Solutions/Arrays/LargestElement.cpp
function largestElement (line 6) | int largestElement(vector<int> A) {
FILE: DSA Essentials Solutions/Arrays/LowerBound.cpp
function lowerBound (line 9) | int lowerBound(vector<int> A, int Val) {
FILE: DSA Essentials Solutions/Arrays/MaximumSumSubarray.cpp
function maxSumSubarray (line 7) | int maxSumSubarray(vector<int> A) {
FILE: DSA Essentials Solutions/Arrays/SortedPairSum.cpp
function closestSum (line 7) | pair<int, int> closestSum(vector<int> arr, int x){
FILE: DSA Essentials Solutions/Backtracking/N-QueenWays.cpp
function isSafe (line 7) | int isSafe(int N, int mat[][20], int r, int c)
function solve (line 36) | void solve(int N,int mat[][20], int r)
function nQueen (line 65) | int nQueen(int n){
FILE: DSA Essentials Solutions/Backtracking/RatAndMice.cpp
function ratchase (line 4) | void ratchase(vector<string> a,vector<vector<int>> &b,vector<vector<int>...
function ratAndMice (line 35) | vector<vector<int>> ratAndMice(vector<string> a) {
FILE: DSA Essentials Solutions/Backtracking/UniqueSubset.cpp
function recur (line 4) | void recur(vector<int> &nums, vector<int> ans, int i)
function uniqueSubsets (line 17) | vector<vector<int>> uniqueSubsets(vector<int> nums){
FILE: DSA Essentials Solutions/Backtracking/WordBreakProblem.cpp
function help (line 6) | void help(string s, int n, string res, vector<string> &word)
function wordBreak (line 32) | int wordBreak(string s, vector<string> &dictionary)
FILE: DSA Essentials Solutions/Backtracking/WordSearch.cpp
function recur (line 4) | void recur(vector<vector<char>>& board, string &word, int i, int j, int k)
function wordSearch (line 26) | bool wordSearch(vector<vector<char>> &board, string word)
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/Chopsticks.cpp
function pairSticks (line 7) | int pairSticks(vector<int> length, int D)
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/DefenseKingdom.cpp
function defkin (line 10) | int defkin(int W, int H, vector<pair<int, int>> position)
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/OptimisedBubbleSort.cpp
function optimizedBubbleSort (line 4) | vector<int> optimizedBubbleSort(vector<int> arr){
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/SortingCartesianProducts.cpp
function sortCartesian (line 7) | vector<pair<int, int>> sortCartesian(vector<pair<int, int>> v)
FILE: DSA Essentials Solutions/Basic Sorting Algorithms/SortingWithComparator.cpp
function compare (line 3) | bool compare(int a, int b){
function sortingWithComparator (line 6) | vector<int> sortingWithComparator(vector<int> v, bool flag){
FILE: DSA Essentials Solutions/BinarySearchTree/DeleteInBST.cpp
class node (line 3) | class node{
method node (line 8) | node(int d){
function node (line 16) | node* deleteNode(node* root, int k){
method node (line 8) | node(int d){
FILE: DSA Essentials Solutions/BinarySearchTree/IsBST.cpp
class Node (line 4) | class Node
method Node (line 11) | Node(int key){
function isBSTUtil (line 17) | bool isBSTUtil(Node* node, int min, int max)
function isBST (line 35) | bool isBST(Node * root){
FILE: DSA Essentials Solutions/BinarySearchTree/MirrorABST.cpp
class Node (line 4) | class Node
method Node (line 11) | Node(int key){
function mirror (line 17) | void mirror(Node* node)
function Node (line 36) | Node* mirrorBST(Node * root){
method Node (line 11) | Node(int key){
FILE: DSA Essentials Solutions/BinaryTree/ExpressionTree.cpp
type Node (line 7) | struct Node {
function isOp (line 12) | bool isOp(string data)
function evalTree (line 19) | int evalTree(Node* root){
FILE: DSA Essentials Solutions/BinaryTree/K-thLevel.cpp
type Node (line 6) | struct Node {
function printKthLevel (line 11) | vector<int> printKthLevel(Node* root, int k){
FILE: DSA Essentials Solutions/BinaryTree/MinDepth.cpp
type Node (line 4) | struct Node {
function minDepth (line 9) | int minDepth(Node *root) {
FILE: DSA Essentials Solutions/BinaryTree/RemoveHalfNodes.cpp
type Node (line 4) | struct Node {
function inorder (line 9) | void inorder(Node* root, vector<int> &v)
function Node (line 17) | Node *help(Node *root)
function removeHalfNodes (line 33) | vector<int> removeHalfNodes(Node *root)
FILE: DSA Essentials Solutions/BinaryTree/SumOfNodes.cpp
type Node (line 4) | struct Node {
function sumBT (line 9) | int sumBT(Node* root)
FILE: DSA Essentials Solutions/BinaryTree/SymmetricTree.cpp
type Node (line 4) | struct Node {
function isSymmetric (line 8) | bool isSymmetric(Node* root) {
FILE: DSA Essentials Solutions/BinaryTree/TargetPathSum.cpp
type Node (line 4) | struct Node {
function help (line 10) | void help(Node* root, int a, vector<int> &v, int b)
function pathSum (line 37) | vector<vector<int>> pathSum(Node* root, int targetSum) {
FILE: DSA Essentials Solutions/Bit Manipulation/EarthLevels.cpp
function convertDecimalToBinary (line 7) | long long convertDecimalToBinary(unsigned long long int n)
function earthLevel (line 22) | int earthLevel(int k)
FILE: DSA Essentials Solutions/Bit Manipulation/ModuloExponentiation.cpp
function power (line 8) | int power(int x, int y, int p)
FILE: DSA Essentials Solutions/Bit Manipulation/SubsetSumQueries.cpp
function subsetSum (line 7) | vector<bool> subsetSum(vector<int> v, vector<int> q)
FILE: DSA Essentials Solutions/Bit Manipulation/Xoring.cpp
function xoring (line 7) | int xoring(vector<int> v)
FILE: DSA Essentials Solutions/Divide and Conquer/2DArrayMerge.cpp
function merge_row (line 5) | void merge_row(vector<vector<int>> &mat,int i, int cs, int cm, int ce){
function merge_col (line 37) | void merge_col(vector<vector<int>> &mat,int j, int rs, int rm, int re){
function merge (line 68) | void merge(int m, int n, vector<vector<int>> &mat, int rs, int rm, int r...
function merge_sort (line 85) | void merge_sort(int m, int n, vector<vector<int>> &mat, int rs, int re, ...
function mergeSort (line 111) | vector<vector<int>> mergeSort(int m, int n, vector<vector<int>> v){
FILE: DSA Essentials Solutions/Divide and Conquer/BinarySearchUsingRecursion.cpp
function binary (line 12) | int binary(vector<int> arr, int l, int r, int x)
function binarySearch (line 37) | int binarySearch(vector<int> v, int x)
FILE: DSA Essentials Solutions/Dynamic Programming/CoinChange.cpp
function coinChange (line 4) | long long coinChange(int s, int n , vector<int> a, long long dp[500][100])
function findCombinations (line 17) | long long findCombinations(int n, vector<int> coins)
FILE: DSA Essentials Solutions/Dynamic Programming/MinimumPartitioning.cpp
function findMin (line 4) | int findMin(vector<int> arr)
FILE: DSA Essentials Solutions/Dynamic Programming/OptimalGameStrategy.cpp
function game (line 4) | int game(int n, vector<int> v, int s, int e){
function MaxValue (line 15) | int MaxValue(int n, vector<int> v){
FILE: DSA Essentials Solutions/Dynamic Programming/Vacation.cpp
function topDown (line 35) | int topDown(viii &v, int n, int i, int dp[][4], int prev)
function vacation (line 48) | int vacation(vector<int> a, vector<int> b, vector<int> c)
FILE: DSA Essentials Solutions/Graphs/AllPathsFromSourceToTarget.cpp
function dfs (line 3) | void dfs(vector<vector<int>>& graph, vector<vector<int>>& result, vector...
function allPathsSourceTarget (line 13) | vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
FILE: DSA Essentials Solutions/Graphs/FindStarInGraph.cpp
function findCenter (line 4) | int findCenter(vector<vector<int>>& v) {
FILE: DSA Essentials Solutions/Graphs/KeysAndRooms.cpp
function canVisitAllRooms (line 4) | bool canVisitAllRooms(vector<vector<int>>& rooms) {
FILE: DSA Essentials Solutions/Hashing/ArrayIntersection.cpp
function intersection (line 5) | vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
FILE: DSA Essentials Solutions/Hashing/KSumSubarray.cpp
function longestSubarrayKSum (line 5) | int longestSubarrayKSum(vector<int> arr,int k){
FILE: DSA Essentials Solutions/Heaps/MaximumProduct.cpp
function maxProduct (line 5) | int maxProduct(vector<int>& nums) {
FILE: DSA Essentials Solutions/Heaps/ReduceArraySizeToHalf.cpp
function minSetSize (line 5) | int minSetSize(vector<int>& arr) {
FILE: DSA Essentials Solutions/Heaps/RelativeRanks.cpp
function findRelativeRanks (line 3) | vector<string> findRelativeRanks(vector<int>& score) {
FILE: DSA Essentials Solutions/Heaps/WeakestRows.cpp
function kWeakestRows (line 5) | vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
FILE: DSA Essentials Solutions/Linked List/AlternateMerge.cpp
class node (line 7) | class node{
method node (line 12) | node(int data){
function node (line 18) | node* apend(node* root, int d){
method node (line 12) | node(int data){
function node (line 28) | node* alternateMerge(node * root1, node* root2){
method node (line 12) | node(int data){
FILE: DSA Essentials Solutions/Linked List/BubbleSortOnLinkedList.cpp
class node (line 5) | class node
method node (line 10) | node(int data)
function len (line 18) | int len(node* head)
function node (line 30) | node* bubble_sort_LinkedList_itr(node* head)
method node (line 10) | node(int data)
FILE: DSA Essentials Solutions/Linked List/DeleteTail.cpp
class node (line 7) | class node{
method node (line 12) | node(int data){
function node (line 18) | node* deleteTail(node * head){
method node (line 12) | node(int data){
FILE: DSA Essentials Solutions/Linked List/KthLastElement.cpp
class node (line 7) | class node{
method node (line 12) | node(int data){
function kthLastElement (line 18) | int kthLastElement(node * head,int k){
FILE: DSA Essentials Solutions/Queues/FirstNonRepeatingLetter.cpp
function firstnonrepeating (line 7) | vector<char> firstnonrepeating(vector<char> str)
FILE: DSA Essentials Solutions/Queues/InterleaveTwoHalvesOfQueue.cpp
function interLeave (line 6) | queue<int> interLeave(queue<int> q){
FILE: DSA Essentials Solutions/Queues/SortQueueWithConstantSpace.cpp
function minIndex (line 4) | int minIndex(queue<int> &q, int sortedIndex)
function insertMinToRear (line 24) | void insertMinToRear(queue<int> &q, int min_index)
function sortQueue (line 40) | void sortQueue(queue<int> &q)
function sortqueue (line 49) | queue<int> sortqueue(queue<int> &q)
FILE: DSA Essentials Solutions/Recursion/2DArrayMerge.cpp
function merge_row (line 6) | void merge_row(vector<vector<int>> &mat,int i, int cs, int cm, int ce){
function merge_col (line 38) | void merge_col(vector<vector<int>> &mat,int j, int rs, int rm, int re){
function merge (line 69) | void merge(int m, int n, vector<vector<int>> &mat, int rs, int rm, int r...
function merge_sort (line 86) | void merge_sort(int m, int n, vector<vector<int>> &mat, int rs, int re, ...
function mergeSort (line 112) | vector<vector<int>> mergeSort(int m, int n, vector<vector<int>> v){
FILE: DSA Essentials Solutions/Recursion/AllOccurences.cpp
function helper (line 10) | void helper(int k, vector<int> v, int i){
function findAllOccurences (line 22) | vector<int> findAllOccurences(int k, vector<int> v){
FILE: DSA Essentials Solutions/Recursion/BinaryStrings.cpp
function helper (line 8) | void helper(string str,int n,int i){
function binaryStrings (line 29) | vector<string> binaryStrings(int n){
FILE: DSA Essentials Solutions/Recursion/FriendsParty.cpp
function help (line 6) | int help(int n)
function friendsPairing (line 13) | int friendsPairing(int n){
FILE: DSA Essentials Solutions/Recursion/PrintIncreasingNumbers.cpp
function help (line 6) | void help(int i, int n, vector<int> &v)
function increasingNumbers (line 13) | vector<int> increasingNumbers(int N) {
FILE: DSA Essentials Solutions/Recursion/TilingProblem.cpp
function tiles (line 6) | int tiles(int n,int m){
function tillingProblem (line 13) | int tillingProblem(int n, int m){
FILE: DSA Essentials Solutions/Stacks/DuplicateParenthesis.cpp
function duplicateParentheses (line 7) | bool duplicateParentheses(string str){
FILE: DSA Essentials Solutions/Stacks/MaximumRectangularAreaInHistogram.cpp
function getMaxArea (line 6) | int getMaxArea(vector<int> hist)
FILE: DSA Essentials Solutions/Stacks/NextGreaterElement.cpp
function nextGreaterElement (line 6) | vector<int> nextGreaterElement(vector<int> arr){
FILE: DSA Essentials Solutions/Stacks/ReverseANumberUsingStack.cpp
function reverse (line 6) | int reverse(int n){
FILE: DSA Essentials Solutions/Stacks/StockSpanProblem.cpp
function stockSpanner (line 6) | vector<int> stockSpanner(vector<int> &a){
FILE: DSA Essentials Solutions/Strings/ArePermutation.cpp
function arePermutation (line 8) | bool arePermutation(string str1, string str2)
FILE: DSA Essentials Solutions/Strings/BinaryStringToNumber.cpp
function binaryToDecimal (line 9) | int binaryToDecimal(string n)
FILE: DSA Essentials Solutions/Strings/CheckPalindrome.cpp
function isPalindrome (line 7) | bool isPalindrome(string str)
FILE: DSA Essentials Solutions/Strings/RemoveDuplicates.cpp
function string (line 7) | string removeDuplicate(string s){
FILE: DSA Essentials Solutions/Strings/StringCompression.cpp
function compress (line 8) | int compress(vector<char>& chars) {
FILE: DSA Essentials Solutions/Strings/VowelFind.cpp
function string (line 7) | string vowel(string S){
FILE: DSA Essentials Solutions/Trie/PrefixStrings.cpp
class node (line 4) | class node{
method node (line 9) | node(char a){
class Trie (line 14) | class Trie{
method insert (line 18) | void insert(string str){
method dfs (line 30) | void dfs(node*temp, vector<string> &v, string word ){
method find (line 47) | vector<string> find(string str){
function findPrefixStrings (line 73) | vector<string> findPrefixStrings(vector<string> words, string prefix){
FILE: DSA Essentials Solutions/Vectors/MakeZeroes.cpp
function makeZeroes (line 4) | vector<vector<int>> makeZeroes(vector<vector<int>> arr){
FILE: DSA Essentials Solutions/Vectors/RotateImage.cpp
function rotate (line 4) | void rotate(vector<vector<int>>& matrix) {
FILE: DSA Essentials Solutions/Vectors/SortFruits.cpp
function comp (line 4) | bool comp(pair<string,int> a, pair<string, int> b){
function sortFruits (line 8) | vector<pair<string,int>> sortFruits(vector<pair<string,int>> v, string S){
FILE: DSA Essentials Solutions/Vectors/SortingCabs.cpp
function comp (line 4) | bool comp(pair<int,int> a, pair<int, int> b){
function sortCabs (line 10) | vector<pair<int,int>> sortCabs(vector<pair<int,int>> v){
Condensed preview — 76 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (64K chars).
[
{
"path": "DSA Essentials Solutions/2d Arrays/PascalsTriangle.cpp",
"chars": 844,
"preview": "//Expected Time Complexity: O(n^3)\n\n//Hint: Use Binomial Coeffiecients\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\ni"
},
{
"path": "DSA Essentials Solutions/2d Arrays/SubmatrixSum.cpp",
"chars": 1190,
"preview": "//Hint: Pre Compute Cumilative Sums of every index i,j.\n\n//Expected Time Complexity:\n// Pre Computing : O(N^2)\n// Querie"
},
{
"path": "DSA Essentials Solutions/2d Arrays/WavePrint.cpp",
"chars": 1078,
"preview": "//Expected Time Complexity: O(n^2)\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n \n vector<int> WavePrint(int m, int n,"
},
{
"path": "DSA Essentials Solutions/Arrays/K-rotate.cpp",
"chars": 381,
"preview": "//Expected Time Complexity: O(n)\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> kRotate(vector<int> a, int k"
},
{
"path": "DSA Essentials Solutions/Arrays/LargestElement.cpp",
"chars": 275,
"preview": "//Expected Time Complexity: O(N)\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nint largestElement(vector<int> A) {\n "
},
{
"path": "DSA Essentials Solutions/Arrays/LowerBound.cpp",
"chars": 465,
"preview": "//Expected Time Complexity: O(logN)\n//Hint: Binary Search\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n\nint lowerBoun"
},
{
"path": "DSA Essentials Solutions/Arrays/MaximumSumSubarray.cpp",
"chars": 457,
"preview": "//Time complexity: O(n)\n//Hint: Kadane's Algorithm\n\n#include <bits/stdc++.h>\nusing namespace std ;\n\nint maxSumSubarray(v"
},
{
"path": "DSA Essentials Solutions/Arrays/SortedPairSum.cpp",
"chars": 552,
"preview": "//Expected Time Complexity: O(N)\n//Hint: Two Pointer Approach\n\n#include<bits/stdc++.h>\nusing namespace std;\n\npair<int, i"
},
{
"path": "DSA Essentials Solutions/Backtracking/N-QueenWays.cpp",
"chars": 1420,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n\n\nint cnt;\n\nint isSafe(int N, int mat[][20], int r, int c)\n{\n // return"
},
{
"path": "DSA Essentials Solutions/Backtracking/RatAndMice.cpp",
"chars": 972,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid ratchase(vector<string> a,vector<vector<int>> &b,vector<vector<int>> "
},
{
"path": "DSA Essentials Solutions/Backtracking/UniqueSubset.cpp",
"chars": 541,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\nset<vector<int>> s;\nvoid recur(vector<int> &nums, vector<int> ans, int i)\n"
},
{
"path": "DSA Essentials Solutions/Backtracking/WordBreakProblem.cpp",
"chars": 659,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nint cnt = 0;\nvector<string> v;\nvoid help(string s, int n, string res, vect"
},
{
"path": "DSA Essentials Solutions/Backtracking/WordSearch.cpp",
"chars": 919,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\nbool chk;\nvoid recur(vector<vector<char>>& board, string &word, int i, int"
},
{
"path": "DSA Essentials Solutions/Basic Sorting Algorithms/Chopsticks.cpp",
"chars": 340,
"preview": "//Expected Complexity: O(N logN)\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nint pairSticks(vector<int> length, int "
},
{
"path": "DSA Essentials Solutions/Basic Sorting Algorithms/DefenseKingdom.cpp",
"chars": 924,
"preview": "//Expected Time Complexity= O(N log N)\n//Find the largest distance from both W and H axis and take their product.\n\n\n\n#in"
},
{
"path": "DSA Essentials Solutions/Basic Sorting Algorithms/OptimisedBubbleSort.cpp",
"chars": 520,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> optimizedBubbleSort(vector<int> arr){\n // your code goes h"
},
{
"path": "DSA Essentials Solutions/Basic Sorting Algorithms/SortingCartesianProducts.cpp",
"chars": 197,
"preview": "//Expected Time Complexity :O(N log N)\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nvector<pair<int, int>> sortCartes"
},
{
"path": "DSA Essentials Solutions/Basic Sorting Algorithms/SortingWithComparator.cpp",
"chars": 280,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\nbool compare(int a, int b){\n return a > b;\n}\nvector<int> sortingWithComp"
},
{
"path": "DSA Essentials Solutions/BinarySearchTree/DeleteInBST.cpp",
"chars": 1739,
"preview": "#include <iostream>\nusing namespace std;\nclass node{\n public:\n int data;\n node*left;\n node*right;\n no"
},
{
"path": "DSA Essentials Solutions/BinarySearchTree/IsBST.cpp",
"chars": 772,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass Node\n{\n public:\n int key;\n Node *left;\n Node *right;\n\n Node"
},
{
"path": "DSA Essentials Solutions/BinarySearchTree/MirrorABST.cpp",
"chars": 648,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass Node\n{\n public:\n int key;\n Node *left;\n Node *right;\n\n Node"
},
{
"path": "DSA Essentials Solutions/BinaryTree/ExpressionTree.cpp",
"chars": 691,
"preview": "//Expected Time Complexity: O(n)\n \n\n#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Node {\n string key;\n N"
},
{
"path": "DSA Essentials Solutions/BinaryTree/K-thLevel.cpp",
"chars": 1647,
"preview": "//Expected Time Complexity: O(n)\n\n#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Node {\n int key;\n Node* l"
},
{
"path": "DSA Essentials Solutions/BinaryTree/MinDepth.cpp",
"chars": 628,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Node {\n int key;\n Node* left, *right;\n};\n\nint minDepth(Node"
},
{
"path": "DSA Essentials Solutions/BinaryTree/RemoveHalfNodes.cpp",
"chars": 843,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Node {\n int key;\n Node* left, *right;\n};\n\nvoid inorder(Node"
},
{
"path": "DSA Essentials Solutions/BinaryTree/SumOfNodes.cpp",
"chars": 395,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Node {\n int key;\n Node* left, *right;\n};\n\nint sumBT(Node* r"
},
{
"path": "DSA Essentials Solutions/BinaryTree/SymmetricTree.cpp",
"chars": 1113,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Node {\n int key;\n Node* left, *right;\n};\nbool isSymmetric(N"
},
{
"path": "DSA Essentials Solutions/BinaryTree/TargetPathSum.cpp",
"chars": 843,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Node {\n int val;\n Node* left, *right;\n};\n\nvector<vector<int"
},
{
"path": "DSA Essentials Solutions/Bit Manipulation/EarthLevels.cpp",
"chars": 728,
"preview": "// Expected Time Complexity : O(Log n)\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nlong long convertDecimalToBinary("
},
{
"path": "DSA Essentials Solutions/Bit Manipulation/ModuloExponentiation.cpp",
"chars": 604,
"preview": "// Expected Time Complexity : O(Log N)\n// Hint: if a is even, than x^a can be written as (x^(a/2))*(x^(a/2))\n\n#include <"
},
{
"path": "DSA Essentials Solutions/Bit Manipulation/SubsetSumQueries.cpp",
"chars": 461,
"preview": "// Expected Time Complexity : O(1) for each query\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nvector<bool> subsetSum"
},
{
"path": "DSA Essentials Solutions/Bit Manipulation/Xoring.cpp",
"chars": 206,
"preview": "// Expected Time Complexity : O(N)\n// xor of two same elements is 0\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nint x"
},
{
"path": "DSA Essentials Solutions/Divide and Conquer/2DArrayMerge.cpp",
"chars": 2322,
"preview": "\n#include<bits/stdc++.h>\nusing namespace std;\n\nvoid merge_row(vector<vector<int>> &mat,int i, int cs, int cm, int ce){\n "
},
{
"path": "DSA Essentials Solutions/Divide and Conquer/BinarySearchUsingRecursion.cpp",
"chars": 1028,
"preview": "//Expected Time Complexity: O(logn)\n\n#include <bits/stdc++.h>\nusing namespace std;\n \n#include <bits/stdc++.h>\nusing nam"
},
{
"path": "DSA Essentials Solutions/Dynamic Programming/CoinChange.cpp",
"chars": 512,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nlong long coinChange(int s, int n , vector<int> a, long long dp[500][100])"
},
{
"path": "DSA Essentials Solutions/Dynamic Programming/MinimumPartitioning.cpp",
"chars": 1285,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n \nint findMin(vector<int> arr)\n{\n int n = arr.size();\n int sum = 0;"
},
{
"path": "DSA Essentials Solutions/Dynamic Programming/OptimalGameStrategy.cpp",
"chars": 385,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n\nint game(int n, vector<int> v, int s, int e){\n\n if(s==e || s==e-1){\n "
},
{
"path": "DSA Essentials Solutions/Dynamic Programming/Vacation.cpp",
"chars": 1609,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<ll>;\nusing"
},
{
"path": "DSA Essentials Solutions/Graphs/AllPathsFromSourceToTarget.cpp",
"chars": 576,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\nvoid dfs(vector<vector<int>>& graph, vector<vector<int>>& result, vector<in"
},
{
"path": "DSA Essentials Solutions/Graphs/FindStarInGraph.cpp",
"chars": 516,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nint findCenter(vector<vector<int>>& v) {\n \n pair<int,"
},
{
"path": "DSA Essentials Solutions/Graphs/KeysAndRooms.cpp",
"chars": 815,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool canVisitAllRooms(vector<vector<int>>& rooms) {\n unordered_map<"
},
{
"path": "DSA Essentials Solutions/Hashing/ArrayIntersection.cpp",
"chars": 573,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nvector<int> intersection(vector<int>& nums1, vector<int>& nums2) {\n "
},
{
"path": "DSA Essentials Solutions/Hashing/KSumSubarray.cpp",
"chars": 397,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nint longestSubarrayKSum(vector<int> arr,int k){\n int n = arr.size();\n\t"
},
{
"path": "DSA Essentials Solutions/Heaps/MaximumProduct.cpp",
"chars": 283,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nint maxProduct(vector<int>& nums) {\n priority_queue<int> q;\n "
},
{
"path": "DSA Essentials Solutions/Heaps/ReduceArraySizeToHalf.cpp",
"chars": 492,
"preview": "\n#include<bits/stdc++.h>\nusing namespace std;\n\nint minSetSize(vector<int>& arr) {\n int n=arr.size();\n prio"
},
{
"path": "DSA Essentials Solutions/Heaps/RelativeRanks.cpp",
"chars": 811,
"preview": " #include<bits/stdc++.h>\nusing namespace std;\n vector<string> findRelativeRanks(vector<int>& score) {\n priority"
},
{
"path": "DSA Essentials Solutions/Heaps/WeakestRows.cpp",
"chars": 756,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nvector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n prior"
},
{
"path": "DSA Essentials Solutions/Linked List/AlternateMerge.cpp",
"chars": 1007,
"preview": "//Expected Time Complexity: O(n)\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n\nclass node{\npublic:\n\tint data;\n\tnode* n"
},
{
"path": "DSA Essentials Solutions/Linked List/BubbleSortOnLinkedList.cpp",
"chars": 1410,
"preview": "//Expected Time Complexity: O(n^2)\n\n#include <iostream>\nusing namespace std;\nclass node\n{\npublic:\n int data;\n node"
},
{
"path": "DSA Essentials Solutions/Linked List/DeleteTail.cpp",
"chars": 671,
"preview": "//Expected Time Complexity: O(n)\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n\nclass node{\npublic:\n\tint data;\n\tnode* n"
},
{
"path": "DSA Essentials Solutions/Linked List/KthLastElement.cpp",
"chars": 536,
"preview": "//Expected Time Complexity: O(n)\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n\nclass node{\npublic:\n\tint data;\n\tnode* n"
},
{
"path": "DSA Essentials Solutions/Queues/FirstNonRepeatingLetter.cpp",
"chars": 505,
"preview": "//Expected Time Complexity: O(n)\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX_CHAR = 26;\n\nvector<char> f"
},
{
"path": "DSA Essentials Solutions/Queues/InterleaveTwoHalvesOfQueue.cpp",
"chars": 455,
"preview": "//Expected Time Complexity: O(n)\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nqueue<int> interLeave(queue<int> q){\n "
},
{
"path": "DSA Essentials Solutions/Queues/SortQueueWithConstantSpace.cpp",
"chars": 1045,
"preview": "#include <bits/stdc++.h>\r\nusing namespace std;\r\n\r\nint minIndex(queue<int> &q, int sortedIndex)\r\n{\r\n int min_index = -"
},
{
"path": "DSA Essentials Solutions/Recursion/2DArrayMerge.cpp",
"chars": 2365,
"preview": "Hint: Divide, sort row-wise, sort col-wise\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nvoid merge_row(vector<vector<i"
},
{
"path": "DSA Essentials Solutions/Recursion/AllOccurences.cpp",
"chars": 395,
"preview": "//Expected Time Complexity: O(N)\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> vec;\n\n\nvoid helper(int k, "
},
{
"path": "DSA Essentials Solutions/Recursion/BinaryStrings.cpp",
"chars": 561,
"preview": "//Expected Time Complexity: O(2^n)\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<string> v;\n\nvoid helper(string"
},
{
"path": "DSA Essentials Solutions/Recursion/FriendsParty.cpp",
"chars": 260,
"preview": "//Expected Time Complexity: O(2^n)\n\n#include <iostream>\nusing namespace std;\n\nint help(int n)\n{\n if (n <= 0) return 0"
},
{
"path": "DSA Essentials Solutions/Recursion/PrintIncreasingNumbers.cpp",
"chars": 280,
"preview": "//Expected Time Complexity: O(n)\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nvoid help(int i, int n, vector<int> &v)\n"
},
{
"path": "DSA Essentials Solutions/Recursion/TilingProblem.cpp",
"chars": 261,
"preview": "//Expected Time Complexity: O(2^n)\n\n#include <iostream>\nusing namespace std;\n\nint tiles(int n,int m){\n if(n<m) return"
},
{
"path": "DSA Essentials Solutions/Stacks/DuplicateParenthesis.cpp",
"chars": 639,
"preview": "\n//Expected Time Complexity: O(n)\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nbool duplicateParentheses(string str){\n"
},
{
"path": "DSA Essentials Solutions/Stacks/MaximumRectangularAreaInHistogram.cpp",
"chars": 910,
"preview": "//Expected Time Complexity: O(n)\n\n#include<bits/stdc++.h>\nusing namespace std;\n \nint getMaxArea(vector<int> hist)\n{\n "
},
{
"path": "DSA Essentials Solutions/Stacks/NextGreaterElement.cpp",
"chars": 456,
"preview": "\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> nextGreaterElement(vector<int> arr){\n int n = arr.size();"
},
{
"path": "DSA Essentials Solutions/Stacks/ReverseANumberUsingStack.cpp",
"chars": 424,
"preview": "//Expected Time Complexity: O(n)\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint reverse(int n){\n \n int number"
},
{
"path": "DSA Essentials Solutions/Stacks/StockSpanProblem.cpp",
"chars": 387,
"preview": "//Expected Time Complexity: O(n)\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> stockSpanner(vector<int> &a"
},
{
"path": "DSA Essentials Solutions/Strings/ArePermutation.cpp",
"chars": 700,
"preview": "//Expected Time Complexity= O(N log N)\n//Hint: Permuatations are just different arrangements of same alphabets. Can you "
},
{
"path": "DSA Essentials Solutions/Strings/BinaryStringToNumber.cpp",
"chars": 463,
"preview": "Expected Time Complexity : O(N)\n\n\n#include <iostream>\n#include <string>\nusing namespace std;\n \n// Function to convert bi"
},
{
"path": "DSA Essentials Solutions/Strings/CheckPalindrome.cpp",
"chars": 396,
"preview": "Expected Time Complexity : O(N)\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nbool isPalindrome(string str)\n{\n // S"
},
{
"path": "DSA Essentials Solutions/Strings/RemoveDuplicates.cpp",
"chars": 264,
"preview": "Expected Time Complexity : O(N)\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nstring removeDuplicate(string s){\n /"
},
{
"path": "DSA Essentials Solutions/Strings/StringCompression.cpp",
"chars": 608,
"preview": "Expected Time Complexity : O(N)\n\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nint compress(vector<char>& chars) {\n "
},
{
"path": "DSA Essentials Solutions/Strings/VowelFind.cpp",
"chars": 269,
"preview": "Expected Time Complexity : O(N)\n\n\n#include<bits/stdc++.h>\nusing namespace std;\n\nstring vowel(string S){\n // your code"
},
{
"path": "DSA Essentials Solutions/Trie/PrefixStrings.cpp",
"chars": 1653,
"preview": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass node{\n public:\n char ch;\n unordered_map<char,node*> next;\n"
},
{
"path": "DSA Essentials Solutions/Vectors/MakeZeroes.cpp",
"chars": 588,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nvector<vector<int>> makeZeroes(vector<vector<int>> arr){\n // your code "
},
{
"path": "DSA Essentials Solutions/Vectors/RotateImage.cpp",
"chars": 444,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid rotate(vector<vector<int>>& matrix) {\n int n = matrix.size();\n"
},
{
"path": "DSA Essentials Solutions/Vectors/SortFruits.cpp",
"chars": 336,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool comp(pair<string,int> a, pair<string, int> b){\n return a.second < "
},
{
"path": "DSA Essentials Solutions/Vectors/SortingCabs.cpp",
"chars": 371,
"preview": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool comp(pair<int,int> a, pair<int, int> b){\n float x = sqrt((a.first*"
}
]
About this extraction
This page contains the full source code of the coding-minutes/dsa-essentials-solutions-cpp GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 76 files (53.1 KB), approximately 18.1k tokens, and a symbol index with 143 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.