├── Day 1-9 ├── Day 1 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe ├── Day 2 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe ├── Day 3 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6 │ └── a.exe ├── Day 4 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe ├── Day 5 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 6 │ ├── .vscode │ │ └── c_cpp_properties.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ ├── Q7.cpp │ └── a.exe ├── Day 7 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 8 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe └── Day 9 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 10-19 ├── Day 10 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 11 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe ├── Day 12 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe ├── Day 13 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 14 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 15 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe ├── Day 16 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── a.exe ├── Day 17 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 18 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp └── Day 19 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 20-26 ├── Day 20 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 21 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 22 │ ├── .vscode │ │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 23 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── Q7.cpp ├── Day 24 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ └── Q6.cpp ├── Day 25 │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ └── Q7.cpp └── Day 26 │ ├── .vscode │ └── settings.json │ ├── Q1.cpp │ ├── Q2.cpp │ ├── Q3.cpp │ ├── Q4.cpp │ ├── Q5.cpp │ ├── Q6.cpp │ ├── Q7.cpp │ └── a.exe ├── LICENSE ├── SDE PROBLEMS.pdf └── Template.cpp /Day 1-9/Day 1/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | } 5 | } -------------------------------------------------------------------------------- /Day 1-9/Day 1/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find duplicates in an array of N integers 7 | 8 | void arrayCheck(int a[],int n) 9 | { 10 | int i=0; 11 | while(i>n; 33 | int a[n]; 34 | for(int i=0;i>a[i]; 37 | } 38 | arrayCheck(a,n); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Day 1-9/Day 1/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Sort an array of 0,1,2 in O(1) time & space 7 | 8 | void sort012Naive(int a[],int n) 9 | { 10 | int c0=0,c1=0,c2=0; 11 | for(int i=0;i>n; 57 | int a[n]; 58 | for(int i=0;i>a[i]; 61 | } 62 | 63 | sort012Opti(a,n); 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Day 1-9/Day 1/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Repeat and Missing Number 7 | 8 | void repmis(int a[],int n) 9 | { 10 | int i=0; 11 | while(i rep,mis; 20 | for(i=0;i>n; 37 | int a[n]; 38 | for(int i=0;i>a[i]; 41 | } 42 | 43 | repmis(a,n); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Day 1-9/Day 1/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Merge two sorted arrays in O(1) space 7 | 8 | void naiveMerge(int a[],int b[],int m,int n) 9 | { 10 | for(int i=n-1;i>=0;i--) 11 | { 12 | int j,last=a[m-1]; 13 | for(j=m-2;j>=0&&a[j]>b[i];j--) 14 | a[j+1]=a[j]; 15 | 16 | if(j!=m-2||last>b[i]) 17 | { 18 | a[j+1]=b[i]; 19 | b[i]=last; 20 | } 21 | } 22 | 23 | return; 24 | } 25 | 26 | int nextGap(int gap) 27 | { 28 | if(gap<=1) 29 | return 0; 30 | 31 | return (gap/2)+(gap%2); 32 | } 33 | 34 | void optiMerge(int a[],int b[],int m,int n) 35 | { 36 | int i,j,gap=m+n; 37 | 38 | for(gap=nextGap(gap);gap>0;gap=nextGap(gap)) 39 | { 40 | for(i=0;i+gapa[i+gap]) 43 | swap(a[i],a[i+gap]); 44 | } 45 | 46 | for(j=(gap>m)?gap-m:0;ib[j]) 49 | swap(a[i],b[j]); 50 | } 51 | 52 | if(jb[j+gap]) 57 | swap(b[j],b[j+gap]); 58 | } 59 | } 60 | } 61 | 62 | return; 63 | } 64 | 65 | int main() 66 | { 67 | boost; 68 | int m,n; 69 | cin>>m>>n; 70 | int a[m],b[n]; 71 | for(int i=0;i>a[i]; 74 | } 75 | for(int j=0;j>b[j]; 78 | } 79 | 80 | optiMerge(a,b,m,n); 81 | 82 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Kadane's Algorithm 7 | 8 | void notKadane(int a[],int n) 9 | { 10 | int maxx=a[0]; 11 | int curr=a[0]; 12 | 13 | for(int i=1;i>n; 27 | int a[n]; 28 | for(int i=0;i>a[i]; 31 | } 32 | 33 | notKadane(a,n); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Day 1-9/Day 1/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Merge overlapping subintervals 7 | 8 | void mergeIntervals(vector> v) 9 | { 10 | if(v.empty()) 11 | return; 12 | 13 | stack> s; 14 | 15 | sort(v.begin(),v.end()); 16 | s.push(v[0]); 17 | 18 | for(auto it=v.begin()+1;it!=v.end();it++) 19 | { 20 | if(it->first>s.top().second) 21 | s.push(*it); 22 | else if(it->second>s.top().second) 23 | s.top().second=it->second; 24 | } 25 | 26 | stack> res; 27 | while(!s.empty()) 28 | { 29 | res.push(s.top()); 30 | s.pop(); 31 | } 32 | 33 | while(!res.empty()) 34 | { 35 | cout<>t; 47 | while(t--) 48 | { 49 | int n; 50 | cin>>n; 51 | vector> v; 52 | for(int i=0;i>x>>y; 56 | v.push_back({x,y}); 57 | } 58 | mergeIntervals(v); 59 | 60 | cout<<"\n"; 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Day 1-9/Day 1/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/getoxdev/SDE_Interview_Problems/d9203a22ed295b7a5e8ee45f236281c95975e23f/Day 1-9/Day 1/a.exe -------------------------------------------------------------------------------- /Day 1-9/Day 2/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | } 5 | } -------------------------------------------------------------------------------- /Day 1-9/Day 2/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define R 1000 5 | #define C 1000 6 | #define deb(x) cout << #x << "=" << x << endl 7 | using namespace std; 8 | //Set matrix zeroes 9 | 10 | int main() 11 | { 12 | boost; 13 | int r,c; 14 | cin>>r>>c; 15 | 16 | int m[r][c]; 17 | for(int i=0;i>m[i][j]; 21 | } 22 | 23 | int row[r]={0},col[c]={0}; 24 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Pascal's Triangle 7 | 8 | void betterPascal(int n) 9 | { 10 | int a[n][n]; 11 | 12 | for(int line=0;line>n; 51 | optimisedPascal(n); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Day 1-9/Day 2/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Next Permutation 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | vector a(n); 14 | for(int i=0;i>a[i]; 16 | 17 | int k,l; 18 | for(k=n-2;k>=0;k--) 19 | { 20 | if(a[k]=0;l--) 29 | { 30 | if(a[l]>a[k]) 31 | break; 32 | } 33 | 34 | swap(a[l],a[k]); 35 | reverse(a.begin()+k+1,a.end()); 36 | } 37 | 38 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Inversion of array using Merge Sort 7 | 8 | long long int merge(long long a[],int l,int m,int r) 9 | { 10 | int i=l,j=m,k=l; 11 | long long int ct=0; 12 | long long temp[r+1]; 13 | 14 | while(i<=m-1 && j<=r) 15 | { 16 | if(a[i]<=a[j]) 17 | temp[k++]=a[i++]; 18 | else 19 | { 20 | temp[k++]=a[j++]; 21 | ct+=m-i; //Most important step 22 | } 23 | } 24 | 25 | while(i<=m-1) 26 | temp[k++]=a[i++]; 27 | 28 | while(j<=r) 29 | temp[k++]=a[j++]; 30 | 31 | for(i=l;i<=r;i++) 32 | a[i]=temp[i]; 33 | 34 | return ct; 35 | } 36 | 37 | long long int mergeSort(long long a[],int l,int r) 38 | { 39 | long long int ct=0; 40 | 41 | if(l>t; 63 | while(t--) 64 | { 65 | int n; 66 | cin>>n; 67 | long long int a[n]; 68 | for(int i=0;i>a[i]; 70 | 71 | long long int ct = inversionCount(a,n); 72 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Buy and Sell Stock 7 | 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int n; 15 | cin>>n; 16 | int a[n]; 17 | for(int i=0;i>a[i]; 19 | 20 | vector> v; 21 | int i=0; 22 | 23 | while(i=a[i-1])) 34 | i++; 35 | 36 | int sell = i-1; 37 | 38 | v.push_back({buy,sell}); 39 | } 40 | 41 | if(v.empty()) 42 | cout<<"No Profit\n"; 43 | else 44 | { 45 | for(auto it=v.begin();it!=v.end();it++) 46 | { 47 | cout<<'('<first<<' '<second<<") "; 48 | } 49 | cout<<"\n"; 50 | } 51 | } 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Day 1-9/Day 2/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Rotate matrix 7 | 8 | int main() 9 | { 10 | boost; 11 | int m,n; 12 | cin>>m>>n; 13 | int mat[m][n]; 14 | 15 | //Anti-clockwise 16 | for(int i=0;i>mat[i][j]; 20 | } 21 | 22 | //Transpose 23 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Excel column number 7 | 8 | int main() 9 | { 10 | boost; 11 | string s; 12 | cin>>s; 13 | int num=0,n=s.size(); 14 | 15 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find n^x in O(logn) time 7 | 8 | int powerN(int n,int x) 9 | { 10 | if(x==0) 11 | return 1; 12 | 13 | if(x%2==0) 14 | return powerN(n,x/2)*powerN(n,x/2); 15 | else 16 | return n*powerN(n,x/2)*powerN(n,x/2); 17 | } 18 | 19 | int powerlogN(int n,int x) 20 | { 21 | if(x==0) 22 | return 1; 23 | 24 | int temp=powerlogN(n,x/2); 25 | 26 | if(x%2==0) 27 | return temp*temp; 28 | else 29 | return n*temp*temp; 30 | } 31 | 32 | int main() 33 | { 34 | boost; 35 | int n,x; 36 | cin>>n>>x; 37 | 38 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Count trailing zeroes in factorial of a number 7 | 8 | int main() 9 | { 10 | boost; 11 | int n,count=0; 12 | cin>>n; 13 | 14 | for(int i=5;(n/i)>=1;i*=5) 15 | count+=n/i; 16 | 17 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find GCD in O(logn) time 7 | 8 | int gcdN(int x,int y) 9 | { 10 | if(x==0) 11 | return y; 12 | if(y==0) 13 | return x; 14 | 15 | if(x==y) 16 | return x; 17 | 18 | if(x>y) 19 | return (x-y,y); 20 | else 21 | return (x,y-x); 22 | } 23 | 24 | int gcdlogN(int x,int y) 25 | { 26 | if(x==0) 27 | return y; 28 | 29 | return gcdlogN(y%x,x); 30 | } 31 | 32 | int main() 33 | { 34 | boost; 35 | int x,y; 36 | cin>>x>>y; 37 | 38 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Unique paths in a grid 7 | 8 | int noOfPaths(int m,int n) 9 | { 10 | int path=1; 11 | for(int i=n;i>m>>n; 25 | 26 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Two Sum problem 7 | 8 | int main() 9 | { 10 | boost; 11 | int n,x; 12 | cin>>n>>x; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 16 | 17 | set s; 18 | int flag=-1; 19 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Four Sum problem 7 | 8 | vector> fourSum(vector &a, int k) 9 | { 10 | int n=a.size(); 11 | if(n<4) 12 | return {{}}; 13 | 14 | set> res; 15 | multimap> mp; 16 | 17 | for(int i=0;i p=itr->second; 27 | if(p.first!=i && p.first!=j && p.second!=i && p.second!=j) 28 | { 29 | vector temp = {a[i],a[j],a[p.first],a[p.second]}; 30 | sort(temp.begin(),temp.end()); 31 | res.insert(temp); 32 | } 33 | } 34 | 35 | mp.emplace(sum,make_pair(i,j)); 36 | } 37 | } 38 | 39 | vector> ans; 40 | for(auto itr=res.begin();itr!=res.end();itr++) 41 | ans.push_back(*itr); 42 | 43 | return ans; 44 | } 45 | 46 | int main() 47 | { 48 | int n,k; 49 | cin>>n>>k; 50 | 51 | int a[n]; 52 | for(int i=0;i>a[i]; 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Day 1-9/Day 4/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Longest Consecutive Sequence 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | set s; 15 | for(int i=0;i>a[i]; 18 | s.insert(a[i]); 19 | } 20 | 21 | int ans=0; 22 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Longest subarray with sum 0 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 16 | 17 | int sum=0,maxLen=0; 18 | unordered_map mp; 19 | mp[0]=-1; 20 | 21 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Count number of subarrays with given XOR 7 | 8 | int main() 9 | { 10 | boost; 11 | int n,m; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 16 | 17 | cin>>m; 18 | 19 | int xorA[n]; 20 | xorA[0]=a[0]; 21 | for(int i=1;i mp; 25 | 26 | ll ans=0; 27 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Longest substring without repeat 7 | 8 | int main() 9 | { 10 | boost; 11 | int t; 12 | cin>>t; 13 | while(t--) 14 | { 15 | string str; 16 | cin>>str; 17 | 18 | int n = str.size(); 19 | int res = 0; 20 | 21 | for (int i = 0; i < n; i++) 22 | { 23 | vector visited(256); 24 | 25 | for (int j = i; j < n; j++) 26 | { 27 | if (visited[str[j]] == true) 28 | break; 29 | else 30 | { 31 | res = max(res, j - i + 1); 32 | visited[str[j]] = true; 33 | } 34 | } 35 | } 36 | 37 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Reverse a LinkedList 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node *next; 12 | }; 13 | 14 | struct Node* reverseList(struct Node *head) 15 | { 16 | Node *prev=NULL, *curr=head, *nxt=NULL; 17 | 18 | while(curr!=NULL) 19 | { 20 | nxt=curr->next; 21 | curr->next=prev; 22 | prev=curr; 23 | curr=nxt; 24 | } 25 | 26 | head=prev; 27 | 28 | return head; 29 | } 30 | 31 | int main() 32 | { 33 | boost; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Day 1-9/Day 5/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find middle of the LinkedList 7 | 8 | struct Node { 9 | int data; 10 | Node* next; 11 | 12 | Node(int x){ 13 | data = x; 14 | next = NULL; 15 | } 16 | 17 | }; 18 | 19 | int getMiddle(Node *head) 20 | { 21 | if(head==NULL) 22 | return -1; 23 | 24 | Node *slowPtr = head; 25 | Node *fastPtr = head; 26 | 27 | while(fastPtr!=NULL && fastPtr->next!=NULL) 28 | { 29 | fastPtr=fastPtr->next->next; 30 | slowPtr=slowPtr->next; 31 | } 32 | 33 | return slowPtr->data; 34 | } 35 | 36 | int main() 37 | { 38 | boost; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Day 1-9/Day 5/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Merge two sorted LinkedList 7 | 8 | struct Node { 9 | int data; 10 | struct Node *next; 11 | 12 | Node(int x) { 13 | data = x; 14 | next = NULL; 15 | } 16 | }; 17 | 18 | Node* sortedMerge(Node* a, Node* b) 19 | { 20 | Node *result=NULL; 21 | 22 | if(a==NULL) 23 | return (b); 24 | if(b==NULL) 25 | return (a); 26 | 27 | if(a->data<=b->data) 28 | { 29 | result=a; 30 | result->next=sortedMerge(a->next,b); 31 | } 32 | else 33 | { 34 | result=b; 35 | result->next=sortedMerge(a,b->next); 36 | } 37 | 38 | return (result); 39 | } 40 | 41 | int main() 42 | { 43 | boost; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Day 1-9/Day 5/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Remove nth node from back of LinkedList 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node* next; 12 | }; 13 | 14 | Node* deleteBack(Node* head,int k) 15 | { 16 | Node *first = head,*second = head; 17 | for (int i = 0; i < k; i++) 18 | { 19 | if (second->next == NULL) 20 | { 21 | if (i == k - 1) 22 | head = head->next; 23 | return head; 24 | } 25 | second = second->next; 26 | } 27 | 28 | while (second->next != NULL) 29 | { 30 | first = first->next; 31 | second = second->next; 32 | } 33 | 34 | first->next = first->next->next; 35 | return head; 36 | } 37 | 38 | int main() 39 | { 40 | boost; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Day 1-9/Day 5/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Delete the Node when it is given 7 | 8 | struct Node { 9 | int data; 10 | struct Node *next; 11 | Node(int x) { 12 | data = x; 13 | next = NULL; 14 | } 15 | }; 16 | 17 | // The function may assume that node exists in linked list and is not last node 18 | void deleteNode(Node *node) 19 | { 20 | Node *temp=node; 21 | temp=temp->next; 22 | 23 | node->data=temp->data; 24 | node->next=temp->next; 25 | 26 | return; 27 | } 28 | 29 | int main() 30 | { 31 | boost; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Day 1-9/Day 5/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Add two numbers as LinkedList 7 | 8 | struct Node { 9 | int data; 10 | struct Node* next; 11 | Node(int x) { 12 | data = x; 13 | next = NULL; 14 | } 15 | }; 16 | 17 | Node* addTwoLists(struct Node* l1, struct Node* l2) 18 | { 19 | int c=0; 20 | int t=(l1->data)+(l2->data)+c; 21 | Node *head=new Node((t>9)?t%10:t); 22 | Node *ptr=head; 23 | c=t/10; 24 | l1=l1->next; 25 | l2=l2->next; 26 | 27 | while(l1 && l2) 28 | { 29 | t=(l1->data)+(l2->data)+c; 30 | Node *temp=new Node((t>9)?t%10:t); 31 | c=t/10; 32 | ptr->next=temp; 33 | ptr=temp; 34 | l1=l1->next; 35 | l2=l2->next; 36 | } 37 | 38 | while(l1) 39 | { 40 | t=(l1->data)+c; 41 | Node *temp=new Node((t>9)?t%10:t); 42 | c=t/10; 43 | ptr->next=temp; 44 | ptr=temp; 45 | l1=l1->next; 46 | } 47 | 48 | while(l2) 49 | { 50 | t=(l2->data)+c; 51 | Node *temp=new Node((t>9)?t%10:t); 52 | c=t/10; 53 | ptr->next=temp; 54 | ptr=temp; 55 | l2=l2->next; 56 | } 57 | 58 | if(c) 59 | { 60 | Node *temp=new Node(c); 61 | ptr->next=temp; 62 | ptr=temp; 63 | } 64 | 65 | return head; 66 | } 67 | 68 | 69 | int main() 70 | { 71 | boost; 72 | return 0; 73 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [ 9 | "_DEBUG", 10 | "UNICODE", 11 | "_UNICODE" 12 | ], 13 | "cStandard": "gnu17", 14 | "cppStandard": "c++17", 15 | "intelliSenseMode": "windows-gcc-x64" 16 | } 17 | ], 18 | "version": 4 19 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find intersection point of a Y LinkedList 7 | 8 | struct Node { 9 | int data; 10 | struct Node *next; 11 | Node(int x) { 12 | data = x; 13 | next = NULL; 14 | } 15 | }; 16 | 17 | int getLen(Node *head) 18 | { 19 | Node *temp=head; 20 | int len=1; 21 | 22 | while(temp->next!=NULL) 23 | { 24 | len++; 25 | temp=temp->next; 26 | } 27 | 28 | return len; 29 | } 30 | 31 | int intersectNode(int d, Node* head1, Node* head2) 32 | { 33 | Node *curr1=head1,*curr2=head2; 34 | 35 | for(int i=0;inext; 41 | } 42 | 43 | while(curr1!=NULL && curr2!=NULL) 44 | { 45 | if(curr1==curr2) 46 | return curr1->data; 47 | 48 | curr1=curr1->next; 49 | curr2=curr2->next; 50 | } 51 | 52 | return -1; 53 | } 54 | 55 | int intersectPoint(Node* head1, Node* head2) 56 | { 57 | int len1=getLen(head1),len2=getLen(head2),d=0; 58 | Node *curr1=head1,*curr2=head2; 59 | 60 | if(len1>len2) 61 | { 62 | d=len1-len2; 63 | return intersectNode(d,head1,head2); 64 | } 65 | else if(len2>len1) 66 | { 67 | d=len2-len1; 68 | return intersectNode(d,head2,head1); 69 | } 70 | else 71 | { 72 | while(curr1!=NULL && curr2!=NULL) 73 | { 74 | if(curr1==curr2) 75 | return curr1->data; 76 | 77 | curr1=curr1->next; 78 | curr2=curr2->next; 79 | } 80 | } 81 | 82 | return -1; 83 | } 84 | 85 | int main() 86 | { 87 | boost; 88 | return 0; 89 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Check if a LinkedList is palindrome or not 7 | 8 | struct Node { 9 | int data; 10 | struct Node *nxt; 11 | Node(int x) { 12 | data = x; 13 | nxt = NULL; 14 | } 15 | }; 16 | 17 | struct Node* reverseList(struct Node *head) 18 | { 19 | Node *prev=NULL, *curr=head, *next=NULL; 20 | 21 | while(curr!=NULL) 22 | { 23 | next=curr->nxt; 24 | curr->nxt=prev; 25 | prev=curr; 26 | curr=next; 27 | } 28 | 29 | head=prev; 30 | 31 | return head; 32 | } 33 | 34 | bool isPalindrome(Node* head) 35 | { 36 | Node *slowPtr=head,*fastPtr=head,*prevSlowPtr=NULL,*shalf=NULL,*fhalf=head; 37 | 38 | if(head->nxt==NULL) 39 | return true; 40 | 41 | while(fastPtr!=NULL && fastPtr->nxt!=NULL) 42 | { 43 | prevSlowPtr=slowPtr; 44 | slowPtr=slowPtr->nxt; 45 | fastPtr=fastPtr->nxt->nxt; 46 | } 47 | 48 | shalf=slowPtr; 49 | prevSlowPtr->nxt=NULL; 50 | 51 | if(fastPtr!=NULL) 52 | { 53 | shalf=slowPtr->nxt; 54 | } 55 | 56 | shalf=reverseList(shalf); 57 | 58 | if(shalf==NULL || fhalf==NULL) 59 | return false; 60 | else 61 | { 62 | while(fhalf!=NULL && shalf!=NULL) 63 | { 64 | if(fhalf->data != shalf->data) 65 | return false; 66 | 67 | fhalf=fhalf->nxt; 68 | shalf=shalf->nxt; 69 | } 70 | } 71 | 72 | return true; 73 | } 74 | 75 | int main() 76 | { 77 | boost; 78 | return 0; 79 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Reverse a LinkedList in groups 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node *next; 12 | }; 13 | 14 | Node* reverseInGroups(Node *head,int k) 15 | { 16 | if(head==NULL || head->next==NULL) 17 | return head; 18 | 19 | int len=0; 20 | Node *temp=head; 21 | while(temp) 22 | { 23 | len++; 24 | temp=temp->next; 25 | } 26 | 27 | if(len>=k) 28 | { 29 | int ct=0; 30 | Node *prev=NULL, *curr=head, *nxt=NULL; 31 | while(curr!=NULL && ctnext; 34 | curr->next=prev; 35 | prev=curr; 36 | curr=nxt; 37 | ct++; 38 | } 39 | 40 | if(nxt!=NULL) 41 | head->next=reverseInGroups(nxt,k); 42 | 43 | return prev; 44 | } 45 | 46 | return head; 47 | } 48 | 49 | int main() 50 | { 51 | boost; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Detect a cycle and remove it 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node* next; 12 | 13 | Node(int val) 14 | { 15 | data = val; 16 | next = NULL; 17 | } 18 | }; 19 | 20 | void removeLoop(Node* head) 21 | { 22 | Node *slowPtr=head, *fastPtr=head; 23 | 24 | if(head==NULL || head->next==NULL) 25 | return; 26 | 27 | while(fastPtr!=NULL && fastPtr->next!=NULL) 28 | { 29 | slowPtr=slowPtr->next; 30 | fastPtr=fastPtr->next->next; 31 | 32 | if(slowPtr==fastPtr) 33 | break; 34 | } 35 | 36 | if(slowPtr==head) 37 | { 38 | while(slowPtr->next!=head) 39 | slowPtr=slowPtr->next; 40 | 41 | slowPtr->next = NULL; 42 | } 43 | else if(slowPtr==fastPtr) 44 | { 45 | slowPtr=head; 46 | while(slowPtr->next!=fastPtr->next) 47 | { 48 | slowPtr=slowPtr->next; 49 | fastPtr=fastPtr->next; 50 | } 51 | 52 | fastPtr->next=NULL; 53 | } 54 | 55 | return; 56 | } 57 | 58 | int main() 59 | { 60 | boost; 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Flattening of a LinkedList 7 | 8 | struct Node{ 9 | int data; 10 | struct Node * next; 11 | struct Node * bottom; 12 | 13 | Node(int x){ 14 | data = x; 15 | next = NULL; 16 | bottom = NULL; 17 | } 18 | 19 | }; 20 | 21 | Node* merge(Node *a, Node *b) 22 | { 23 | Node *result=NULL; 24 | 25 | if(a==NULL) 26 | return b; 27 | if(b==NULL) 28 | return a; 29 | 30 | if(a->data<=b->data) 31 | { 32 | result=a; 33 | result->bottom=merge(a->bottom,b); 34 | } 35 | else 36 | { 37 | result=b; 38 | result->bottom=merge(a,b->bottom); 39 | } 40 | 41 | result->next=NULL; 42 | 43 | return result; 44 | } 45 | 46 | Node *flatten(Node *root) 47 | { 48 | if(root==NULL || root->next==NULL) 49 | return root; 50 | 51 | return merge(root,flatten(root->next)); 52 | } 53 | 54 | int main() 55 | { 56 | boost; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Rotate a LinkedList 7 | 8 | struct Node { 9 | int data; 10 | struct Node *next; 11 | Node(int x) { 12 | data = x; 13 | next = NULL; 14 | } 15 | }; 16 | 17 | Node* rotate(Node* head, int k) 18 | { 19 | Node *kNode,*curr=head; 20 | int ct=1; 21 | 22 | while(curr!=NULL && ctnext; 25 | ct++; 26 | } 27 | 28 | if(curr==NULL) 29 | return head; 30 | 31 | kNode=curr; 32 | 33 | while(curr->next!=NULL) 34 | curr=curr->next; 35 | 36 | curr->next=head; 37 | head=kNode->next; 38 | kNode->next=NULL; 39 | 40 | return head; 41 | } 42 | 43 | int main() 44 | { 45 | boost; 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/Q7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Clone a Linked List with random and next pointer 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node *next; 12 | struct Node *random; 13 | 14 | Node(int x) { 15 | data = x; 16 | next = NULL; 17 | random = NULL; 18 | } 19 | }; 20 | 21 | Node* clone(Node *head) 22 | { 23 | Node *curr=head,*temp; 24 | 25 | while(curr) 26 | { 27 | temp=curr->next; 28 | curr->next = new Node(curr->data); 29 | curr->next->next=temp; 30 | curr=temp; 31 | } 32 | 33 | curr=head; 34 | 35 | while(curr) 36 | { 37 | if(curr->next) 38 | curr->next->random = (curr->random)?curr->random->next:curr->random; 39 | 40 | curr = (curr->next)?curr->next->next:curr->next; 41 | } 42 | 43 | Node* copy = head->next; 44 | temp=copy; 45 | 46 | while(head && copy) 47 | { 48 | head->next = (head->next)?head->next->next:head->next; 49 | copy->next = (copy->next)?copy->next->next:copy->next; 50 | 51 | head=head->next; 52 | copy=copy->next; 53 | } 54 | 55 | return temp; 56 | } 57 | 58 | int main() 59 | { 60 | boost; 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Day 1-9/Day 6/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/getoxdev/SDE_Interview_Problems/d9203a22ed295b7a5e8ee45f236281c95975e23f/Day 1-9/Day 6/a.exe -------------------------------------------------------------------------------- /Day 1-9/Day 7/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Merge two sorted LinkedLists 7 | 8 | struct Node { 9 | int data; 10 | struct Node *next; 11 | 12 | Node(int x) { 13 | data = x; 14 | next = NULL; 15 | } 16 | }; 17 | 18 | Node* sortedMerge(Node* a, Node* b) 19 | { 20 | Node *result=NULL; 21 | 22 | if(a==NULL) 23 | return b; 24 | if(b==NULL) 25 | return a; 26 | 27 | if(a->data<=b->data) 28 | { 29 | result=a; 30 | result->next=sortedMerge(a->next,b); 31 | } 32 | else 33 | { 34 | result=b; 35 | result->next=sortedMerge(a,b->next); 36 | } 37 | 38 | return result; 39 | } 40 | int main() 41 | { 42 | boost; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Day 1-9/Day 7/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find the starting point of a loop 7 | 8 | struct Node { 9 | int data; 10 | struct Node *next; 11 | 12 | Node(int x) { 13 | data = x; 14 | next = NULL; 15 | } 16 | }; 17 | 18 | Node* startLoop(Node *head) 19 | { 20 | Node *slow=head,*fast=head; 21 | 22 | if(head==NULL || head->next==NULL) 23 | return NULL; 24 | 25 | while(fast && fast->next) 26 | { 27 | slow=slow->next; 28 | fast=fast->next; 29 | 30 | if(slow==fast) 31 | break; 32 | } 33 | 34 | //Loop not found 35 | if(slow!=fast) 36 | return NULL; 37 | 38 | //Loop found 39 | slow=head; 40 | while(slow!=fast) 41 | { 42 | slow=slow->next; 43 | fast=fast->next; 44 | } 45 | 46 | return slow; 47 | } 48 | 49 | int main() 50 | { 51 | boost; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Day 1-9/Day 7/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //3 Sum 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 16 | 17 | set> temp; 18 | sort(a+0,a+n); 19 | 20 | for(int i=0;i v; 30 | v.push_back(a[i]); 31 | v.push_back(a[j]); 32 | v.push_back(a[k]); 33 | 34 | temp.insert(v); 35 | 36 | int x=a[j],y=a[k]; 37 | while(jj && a[k]==y) 40 | k--; 41 | } 42 | else if(a[i]+a[j]+a[k]<0) 43 | j++; 44 | else 45 | k--; 46 | } 47 | } 48 | 49 | for(auto it=temp.begin();it!=temp.end();it++) 50 | { 51 | for(auto i=(*it).begin();i!=(*it).end();i++) 52 | cout<<*i<<" "; 53 | 54 | cout<<"\n"; 55 | } 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Day 1-9/Day 7/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Trapping Rainwater 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int h[n]; 14 | for(int i=0;i>h[i]; 16 | 17 | int l=0,r=n-1; 18 | 19 | int ans=0,lmax=INT_MIN,rmax=INT_MIN; 20 | while(l=lmax) 25 | lmax=h[l]; 26 | else 27 | ans+=lmax-h[l]; 28 | 29 | l++; 30 | } 31 | else 32 | { 33 | if(h[r]>=rmax) 34 | rmax=h[r]; 35 | else 36 | ans+=rmax-h[r]; 37 | 38 | r--; 39 | } 40 | } 41 | 42 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Remove duplicates from a sorted array 7 | 8 | int removeDuplicates(int a[],int n) 9 | { 10 | if(n==0 || n==1) 11 | return n; 12 | 13 | int j=0; 14 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Max continuous number of 1s 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 16 | 17 | int sum=0,curr=0; 18 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //N meetings in one room 7 | 8 | static bool cmp(pair a,pair b) 9 | { 10 | return a.second> p; 16 | 17 | for(int i=0;if) 27 | { 28 | ct++; 29 | f=p[i].second; 30 | } 31 | } 32 | 33 | return ct; 34 | } 35 | 36 | int main() 37 | { 38 | boost; 39 | int n; 40 | cin>>n; 41 | int s[n],e[n]; 42 | for(int i=0;i>s[i]; 44 | for(int i=0;i>e[i]; 46 | 47 | maxMeetings(s,e,n); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Day 1-9/Day 8/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Activity Selection 7 | 8 | static bool cmp(pair a,pair b) 9 | { 10 | return a.second> p; 16 | 17 | for(int i=0;if) 27 | { 28 | ct++; 29 | f=p[i].second; 30 | } 31 | } 32 | 33 | return ct; 34 | } 35 | 36 | int main() 37 | { 38 | boost; 39 | int n; 40 | cin>>n; 41 | int s[n],e[n]; 42 | for(int i=0;i>s[i]; 44 | for(int i=0;i>e[i]; 46 | 47 | maxActivity(s,e,n); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Day 1-9/Day 8/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Greedy algo to find minimum number of coins 7 | 8 | int minCoins(int v) 9 | { 10 | int denom[] = {1,2,5,10,20,50,100,500,2000}; 11 | 12 | int c=0; 13 | for(int i=8;i>=0;i--) 14 | { 15 | while(v>=denom[i]) 16 | { 17 | v-=denom[i]; 18 | c++; 19 | } 20 | } 21 | 22 | return c+v; 23 | } 24 | 25 | int main() 26 | { 27 | boost; 28 | int v; 29 | cin>>v; 30 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Fractional Knapsack problem 7 | 8 | struct Item{ 9 | int value; 10 | int weight; 11 | }; 12 | 13 | static bool cmp(Item a,Item b) 14 | { 15 | double vala=double(a.value)/double(a.weight); 16 | double valb=double(b.value)/double(b.weight); 17 | 18 | return vala>valb; 19 | } 20 | 21 | double fractionalKnapsack(int w,Item a[],int n) 22 | { 23 | sort(a+0,a+n,cmp); 24 | 25 | double val=0; 26 | for(int i=0;i=a[i].weight) 29 | { 30 | w-=a[i].weight; 31 | val+=double(a[i].value); 32 | } 33 | else 34 | { 35 | val+=(double(a[i].value)/double(a[i].weight))*double(w); 36 | w=0; 37 | break; 38 | } 39 | } 40 | 41 | return val; 42 | } 43 | 44 | int main() 45 | { 46 | boost; 47 | int w; 48 | cin>>w; 49 | int n; 50 | cin>>n; 51 | Item a[n]; 52 | for(int i=0;i>a[i].value>>a[i].weight; 54 | 55 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Minimum number of platforms required for a railway 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int arr[n],dep[n]; 14 | for(int i=0;i>arr[i]; 17 | } 18 | for(int i=0;i>dep[i]; 21 | } 22 | 23 | sort(arr, arr + n); 24 | sort(dep, dep + n); 25 | 26 | int plat= 1, res= 0; 27 | int i = 1, j = 0; 28 | 29 | while (i < n && j < n) { 30 | if (arr[i] <= dep[j]) { 31 | plat++; 32 | i++; 33 | } 34 | 35 | else if (arr[i] > dep[j]) { 36 | plat--; 37 | j++; 38 | } 39 | 40 | if (plat > res) 41 | res = plat; 42 | } 43 | 44 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Job Sequencing problem 7 | 8 | struct Job 9 | { 10 | int id; // Job Id 11 | int dead; // Deadline of job 12 | int profit; // Profit if job is over before or on deadline 13 | }; 14 | 15 | static bool cmp(Job a,Job b) 16 | { 17 | return a.profit>b.profit; 18 | } 19 | 20 | vector JobScheduling(Job a[], int n) 21 | { 22 | sort(a+0,a+n,cmp); 23 | 24 | int sz=0; 25 | for(int i=0;i=1;j--) 35 | { 36 | if(d[j]==-1) 37 | { 38 | d[j]=a[i].id; 39 | ct++; 40 | ans+=a[i].profit; 41 | break; 42 | } 43 | } 44 | } 45 | 46 | return {ct,ans}; 47 | } 48 | 49 | int main() 50 | { 51 | boost; 52 | int n; 53 | cin>>n; 54 | Job a[n]; 55 | for(int i=0;i>a[i].id>>a[i].dead>>a[i].profit; 57 | 58 | vector res=JobScheduling(a,n); 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Day 1-9/Day 8/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/getoxdev/SDE_Interview_Problems/d9203a22ed295b7a5e8ee45f236281c95975e23f/Day 1-9/Day 8/a.exe -------------------------------------------------------------------------------- /Day 1-9/Day 9/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //N Queens problem 7 | 8 | const int n = 11; 9 | bool isSafe(int m[n][n],int r,int c,int s) 10 | { 11 | for(int j=0;j=0 && j>=0;i--,j--) 18 | { 19 | if(m[i][j]) 20 | return false; 21 | } 22 | 23 | for(int i=r,j=c;i=0;i++,j--) 24 | { 25 | if(m[i][j]) 26 | return false; 27 | } 28 | 29 | return true; 30 | } 31 | 32 | bool solveUtil(int m[n][n],int c,int s) 33 | { 34 | //Print your chessboard here 35 | if(c>=s) 36 | return true; 37 | 38 | bool r = false; 39 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Sudoku 7 | 8 | bool isSafe(vector> &m,int r,int c,int num) 9 | { 10 | for(int i=0;i<9;i++) 11 | { 12 | if(m[i][c]==num && i!=r) 13 | return false; 14 | 15 | if(m[r][i]==num && i!=c) 16 | return false; 17 | } 18 | 19 | int sR=r-(r%3),sC=c-(c%3); 20 | for (int i=0;i<3;i++) 21 | { 22 | for (int j=0;j<3;j++) 23 | { 24 | if(m[i+sR][j+sC]==num && (i+sR)!=r && (j+sC)!=c) 25 | return false; 26 | } 27 | } 28 | 29 | return true; 30 | } 31 | 32 | bool solveUtil(vector> &m) 33 | { 34 | for (int i=0;i<9;i++) 35 | { 36 | for (int j=0;j<9;j++) 37 | { 38 | if(m[i][j]==0) 39 | { 40 | for(int num=1;num<=9;num++) 41 | { 42 | if(isSafe(m,i,j,num)) 43 | { 44 | m[i][j]=num; 45 | 46 | if(solveUtil(m)) 47 | return true; 48 | else 49 | m[i][j]=0; 50 | } 51 | } 52 | return false; 53 | } 54 | } 55 | } 56 | 57 | return true; 58 | } 59 | 60 | void solveSudoku(vector>& board) 61 | { 62 | vector> m(9,vector(9)); 63 | for(int i=0;i<9;i++) 64 | { 65 | for(int j=0;j<9;j++) 66 | { 67 | if(board[i][j]=='.') 68 | m[i][j]=0; 69 | else 70 | m[i][j]=int(board[i][j])-48; 71 | } 72 | } 73 | 74 | solveUtil(m); 75 | 76 | for(int i=0;i<9;i++) 77 | { 78 | for(int j=0;j<9;j++) 79 | board[i][j]=char(m[i][j]+48); 80 | } 81 | 82 | return; 83 | } 84 | 85 | int main() 86 | { 87 | boost; 88 | return 0; 89 | } -------------------------------------------------------------------------------- /Day 1-9/Day 9/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //M-Coloring Problem 7 | 8 | bool isPossible(bool graph[101][101],int node,int v,int m,vector &color) 9 | { 10 | for(int i=0;i &color) 20 | { 21 | if(node==v) 22 | return true; 23 | 24 | for(int i=1;i<=m;i++) 25 | { 26 | if(isPossible(graph,node,v,i,color)) 27 | { 28 | color[node]=i; 29 | 30 | if(coloring(graph,node+1,v,m,color)) 31 | return true; 32 | 33 | color[node]=0; 34 | } 35 | } 36 | 37 | return false; 38 | } 39 | 40 | //Function to determine if graph can be coloured with at most m colours such 41 | //that no two adjacent vertices of graph are coloured with same colour. 42 | bool graphColoring(bool graph[101][101], int m, int v) 43 | { 44 | vector color(v,0); 45 | 46 | return coloring(graph,0,v,m,color); 47 | } 48 | 49 | int main() 50 | { 51 | boost; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Day 1-9/Day 9/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Rat in a Maze 7 | 8 | void solvePath(int i,int j,vector> m,vector> &visit,string temp,vector &res) 9 | { 10 | if(i==m.size()-1 && j==m.size()-1) 11 | { 12 | if(!temp.empty()) 13 | res.push_back(temp); 14 | 15 | return; 16 | } 17 | 18 | //Left 19 | if((j-1)>=0 && m[i][j-1]==1 && visit[i][j-1]==0) 20 | { 21 | visit[i][j]=1; 22 | solvePath(i,j-1,m,visit,temp+'L',res); 23 | visit[i][j]=0; 24 | } 25 | 26 | //Right 27 | if((j+1)=0 && m[i-1][j]==1 && visit[i-1][j]==0) 36 | { 37 | visit[i][j]=1; 38 | solvePath(i-1,j,m,visit,temp+'U',res); 39 | visit[i][j]=0; 40 | } 41 | 42 | //Down 43 | if((i+1) findPath(vector> &m, int n) 54 | { 55 | vector res; 56 | vector> visit(n,vector(n,0)); 57 | 58 | if(m[0][0]==1) 59 | solvePath(0,0,m,visit,"",res); 60 | 61 | sort(res.begin(),res.end()); 62 | return res; 63 | } 64 | 65 | int main() 66 | { 67 | boost; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Day 1-9/Day 9/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Print all permutations of a String/Array 7 | 8 | void recurNaive(vector a,vector &temp,map &mp,vector> &res) 9 | { 10 | if(temp.size()==a.size()) 11 | { 12 | res.push_back(temp); 13 | return; 14 | } 15 | 16 | for(int i=0;i &a,int ind,vector> &res) 32 | { 33 | if(ind==a.size()) 34 | { 35 | res.push_back(a); 36 | return; 37 | } 38 | 39 | for(int i=ind;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Word Break (Print all ways) 7 | 8 | void solveWord(string s,string t,set d,vector &res) 9 | { 10 | if(s.empty()) 11 | { 12 | t.pop_back(); 13 | res.push_back(t); 14 | return; 15 | } 16 | 17 | for(int i=0;i wordBreak(string s, vector& dict) 31 | { 32 | set d; 33 | for(int i=0;i res; 37 | solveWord(s,"",d,res); 38 | 39 | return res; 40 | } 41 | 42 | int main() 43 | { 44 | boost; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Day 10-19/Day 10/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Combination Sum-1 7 | 8 | void comSum(vector &a,int index,int t,vector &temp,vector> &ans) 9 | { 10 | if(index==a.size()) 11 | { 12 | if(t==0) 13 | ans.push_back(temp); 14 | 15 | return; 16 | } 17 | 18 | //Choosing the element 19 | if(a[index]<=t) 20 | { 21 | temp.push_back(a[index]); 22 | comSum(a,index,t-a[index],temp,ans); 23 | temp.pop_back(); 24 | } 25 | 26 | //Not choosing the element 27 | comSum(a,index+1,t,temp,ans); 28 | 29 | return; 30 | } 31 | 32 | int main() 33 | { 34 | boost; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Day 10-19/Day 10/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Combination Sum-2 7 | 8 | void comSumNaive(vector &a,int index,int t,vector &temp,set> &res) 9 | { 10 | if(index==a.size()) 11 | { 12 | if(t==0) 13 | res.insert(temp); 14 | 15 | return; 16 | } 17 | 18 | //Choosing the element 19 | if(a[index]<=t) 20 | { 21 | temp.push_back(a[index]); 22 | comSumNaive(a,index+1,t-a[index],temp,res); 23 | temp.pop_back(); 24 | } 25 | 26 | //Not choosing the element 27 | comSumNaive(a,index+1,t,temp,res); 28 | 29 | return; 30 | } 31 | 32 | void comSumOpti(vector &a,int ind,int t,vector &temp,vector> &res) 33 | { 34 | if(t==0) 35 | res.push_back(temp); 36 | 37 | for(int i=ind;iind) 40 | continue; 41 | 42 | if(a[i]>t) 43 | break; 44 | 45 | temp.push_back(a[i]); 46 | comSumOpti(a,i+1,t-a[i],temp,res); 47 | temp.pop_back(); 48 | } 49 | 50 | return; 51 | } 52 | 53 | vector> combinationSum2(vector &a, int target) 54 | { 55 | sort(a.begin(),a.end()); 56 | vector> res; 57 | vector temp; 58 | 59 | comSumOpti(a,0,target,temp,res); 60 | 61 | return res; 62 | } 63 | 64 | int main() 65 | { 66 | boost; 67 | return 0; 68 | } -------------------------------------------------------------------------------- /Day 10-19/Day 10/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Palindrome Partioning 7 | 8 | bool isPalin(string a,int i,int j) 9 | { 10 | while(i &temp,vector> &res) 20 | { 21 | if(ind==s.size()) 22 | { 23 | res.push_back(temp); 24 | return; 25 | } 26 | 27 | for(int i=ind;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Subset Sum-1 7 | 8 | void subsetSumsUtil(vector a,vector &res,int sum,int ind) 9 | { 10 | if(ind==a.size()) 11 | { 12 | res.push_back(sum); 13 | return; 14 | } 15 | 16 | //Choosing the element 17 | subsetSumsUtil(a,res,sum+a[ind],ind+1); 18 | //Not choosing the element 19 | subsetSumsUtil(a,res,sum,ind+1); 20 | 21 | return; 22 | } 23 | 24 | vector subsetSums(vector a, int n) 25 | { 26 | vector res; 27 | subsetSumsUtil(a,res,0,0); 28 | 29 | sort(res.begin(),res.end()); 30 | return res; 31 | } 32 | 33 | int main() 34 | { 35 | boost; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Day 10-19/Day 10/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Subset Sum-2 7 | 8 | void subsetSumsUtil(vector a,int ind,vector &temp,vector> &res) 9 | { 10 | res.push_back(temp); 11 | 12 | for(int i=ind;i> subsetsWithDup(vector &a) 26 | { 27 | sort(a.begin(),a.end()); 28 | vector temp; 29 | vector> res; 30 | subsetSumsUtil(a,0,temp,res); 31 | 32 | return res; 33 | } 34 | 35 | int main() 36 | { 37 | boost; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Day 10-19/Day 10/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //kth Permutation Sequence 7 | 8 | string getPermutation(int n, int k) 9 | { 10 | int fact=1; 11 | vector nums; 12 | for(int i=1;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //nth root of an integer (Use Binary Search) 7 | 8 | void nthroot(double x,int n) 9 | { 10 | double low,high; 11 | if(x>=0 && x<=1) 12 | { 13 | low=x; 14 | high=1; 15 | } 16 | else if(x>1) 17 | { 18 | low=1; 19 | high=x; 20 | } 21 | else 22 | { 23 | cout<<"Nth Root not possible!"; 24 | return; 25 | } 26 | 27 | double ep = 0.0001,g = (low+high)/2; 28 | 29 | while(abs(pow(g,n)-x)>=ep) 30 | { 31 | if(pow(g,n)>x) 32 | high=g; 33 | else 34 | low=g; 35 | 36 | g = (low+high)/2; 37 | } 38 | 39 | cout<>x>>n; 49 | nthroot(x,n); 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Day 10-19/Day 11/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Matrix median 7 | 8 | int median(vector> &mat, int r, int c) 9 | { 10 | int minn = INT_MAX,maxx = INT_MIN; 11 | 12 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find the element that appears once in a sorted array and the rest elements appears twice 7 | 8 | int findOnce(int a[], int n) 9 | { 10 | int low=0,high=n-1; 11 | 12 | while(high>=low) 13 | { 14 | int mid = (low+high)/2; 15 | 16 | if(low==high) 17 | return a[low]; 18 | 19 | if(mid%2==0) 20 | { 21 | if(a[mid]==a[mid+1]) 22 | low=mid+2; 23 | else 24 | high=mid; 25 | } 26 | else 27 | { 28 | if(a[mid]==a[mid-1]) 29 | low=mid+1; 30 | else 31 | high=mid-1; 32 | } 33 | } 34 | 35 | return -1; 36 | } 37 | 38 | int main() 39 | { 40 | boost; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Day 10-19/Day 11/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Search element in a sorted and rotated array 7 | 8 | int binarySearch(int ele, int a[],int l, int r) 9 | { 10 | int mid; 11 | while(l<=r) 12 | { 13 | mid=l + (r-l)/2; //To avoid overflow 14 | 15 | if(ele==a[mid]) 16 | { 17 | return mid; 18 | } 19 | else if(elea[mid]) 24 | { 25 | l=mid+1; 26 | } 27 | } 28 | return -1; 29 | } 30 | 31 | int main() 32 | { 33 | boost; 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | int n; 39 | cin>>n; 40 | int a[n]; 41 | for(int i=0;i>a[i]; 44 | } 45 | int ele; 46 | cin>>ele; 47 | 48 | int l=0,r=n-1,mid,prev,next,index; 49 | while(l<=r) 50 | { 51 | mid = l + (r-l)/2; //To avoid overflow 52 | prev = (mid+n-1)%n; 53 | next = (mid+1)%n; 54 | 55 | if(a[mid]=a[l] && a[mid]<=a[r]) 63 | { 64 | index=(l%n); 65 | break; 66 | } 67 | else if(a[l]<=a[mid]) 68 | l=mid+1; 69 | else if(a[mid]<=a[r]) 70 | r=mid-1; 71 | } 72 | } 73 | 74 | l=0,r=n-1; 75 | int val1 = binarySearch(ele,a,l,index-1); 76 | int val2 = binarySearch(ele,a,index,r); 77 | if(val1!=-1) 78 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //kth element of two sorted arrays 7 | 8 | int kth(vector a,vector b,int l1,int l2,int r1,int r2,int k) 9 | { 10 | int m1=l1+(r1-l1)/2; 11 | int m2=l2+(r2-l2)/2; 12 | int kt=(m1-l1+1)+(m2-l2+1); 13 | 14 | //a is empty 15 | if(l1>r1) 16 | return b[l2+k-1]; 17 | 18 | //b is empty 19 | if(l2>r2) 20 | return a[l1+k-1]; 21 | 22 | if(kt<=k) 23 | { 24 | if(a[m1] a(arr1,arr1+n); 41 | vector b(arr2,arr2+m); 42 | 43 | return kth(a,b,0,0,a.size()-1,b.size()-1,k); 44 | } 45 | 46 | int main() 47 | { 48 | boost; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Day 10-19/Day 11/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Median of two sorted arrays 7 | 8 | double findMedianSortedArrays(vector& nums1, vector& nums2) 9 | { 10 | if(nums1.size()>nums2.size()) 11 | return findMedianSortedArrays(nums2,nums1); 12 | 13 | int x=nums1.size(),y=nums2.size(); 14 | int l=0,h=x; 15 | 16 | while(l<=h) 17 | { 18 | int px=l+(h-l)/2; 19 | int py=((x+y+1)/2)-px; 20 | 21 | int rx,ry,lx,ly; 22 | 23 | lx = (px==0)?INT_MIN:nums1[px-1]; 24 | ly = (py==0)?INT_MIN:nums2[py-1]; 25 | 26 | rx = (px==x)?INT_MAX:nums1[px]; 27 | ry = (py==y)?INT_MAX:nums2[py]; 28 | 29 | if(lx<=ry && ly<=rx) 30 | { 31 | if((x+y)%2==0) 32 | return double((double(max(lx,ly))+double(min(rx,ry)))/2); 33 | else 34 | return double(max(lx,ly)); 35 | } 36 | else if(lx>ry) 37 | h=px-1; 38 | else 39 | l=px+1; 40 | } 41 | 42 | return double(0); 43 | } 44 | 45 | int main() 46 | { 47 | boost; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Day 10-19/Day 11/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/getoxdev/SDE_Interview_Problems/d9203a22ed295b7a5e8ee45f236281c95975e23f/Day 10-19/Day 11/a.exe -------------------------------------------------------------------------------- /Day 10-19/Day 12/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Check if a number is a power of 2 or not in O(1) time 7 | 8 | bool isPowerOf2(int n) 9 | { 10 | if(n==0) 11 | return false; 12 | 13 | return (n&(n-1))?false:true; 14 | } 15 | 16 | int main() 17 | { 18 | boost; 19 | int n; 20 | cin>>n; 21 | 22 | if(isPowerOf2(n)) 23 | cout<<"True"; 24 | else 25 | cout<<"False"; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Day 10-19/Day 12/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Count total set bits 7 | 8 | int totalBits(int n) 9 | { 10 | int i=0; 11 | int ct=0; 12 | 13 | while(n>=(1<>n; 42 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Divide integers without / operator 7 | 8 | ll divide(ll dividend,ll divisor) 9 | { 10 | ll sign = ((dividend<0)^(divisor<0))?-1:1; 11 | 12 | dividend = abs(dividend); 13 | divisor = abs(divisor); 14 | 15 | ll quotient=0,temp=0; 16 | 17 | for(ll i=31;i>=0;i--) 18 | { 19 | if((temp+(divisor<>a>>b; 34 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Power Set 7 | 8 | void powerSet(int a[],int n) 9 | { 10 | int powN = pow(2,n); 11 | 12 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find MSB in O(1) time 7 | 8 | int msb(int n) 9 | { 10 | int k=floor(log2(n)); 11 | 12 | return 1<>n; 20 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find square of a number without using multiplication or division operators 7 | 8 | int sqr(int n) 9 | { 10 | if(n==0) 11 | return 0; 12 | if(n==1) 13 | return 1; 14 | 15 | if(n<0) 16 | n= -n; 17 | 18 | int x = floor(n>>1); 19 | 20 | if(n&1) 21 | return (sqr(x)<<2)+(x<<2)+1; 22 | else 23 | return (sqr(x)<<2); 24 | } 25 | 26 | int main() 27 | { 28 | boost; 29 | int n; 30 | cin>>n; 31 | 32 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Implement Stack and Queue 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node* nxt; 12 | }; 13 | 14 | Node *top=NULL,*front=NULL,*rear=NULL; 15 | 16 | void pushStack(int x) 17 | { 18 | Node *temp=new Node(); 19 | temp->data=x; 20 | temp->nxt=NULL; 21 | 22 | if(top==NULL) 23 | top=temp; 24 | else 25 | { 26 | temp->nxt=top; 27 | top=temp; 28 | } 29 | 30 | return; 31 | } 32 | 33 | int popStack() 34 | { 35 | if(top==NULL) 36 | return -1; 37 | 38 | int temp=top->data; 39 | 40 | top=top->nxt; 41 | 42 | return temp; 43 | } 44 | 45 | int topStack() 46 | { 47 | if(top==NULL) 48 | return -1; 49 | 50 | return top->data; 51 | } 52 | 53 | void pushQueue(int x) 54 | { 55 | Node *temp=new Node(); 56 | temp->data=x; 57 | temp->nxt=NULL; 58 | 59 | if(front==NULL || rear==NULL) 60 | { 61 | front=temp; 62 | rear=temp; 63 | } 64 | else 65 | { 66 | rear->nxt=temp; 67 | rear=temp; 68 | } 69 | 70 | return; 71 | } 72 | 73 | int popQueue() 74 | { 75 | if(front==NULL || rear==NULL) 76 | return -1; 77 | 78 | int temp=front->data; 79 | 80 | front=front->nxt; 81 | 82 | if(front==NULL) 83 | rear=front; 84 | 85 | return temp; 86 | } 87 | 88 | int frontQueue() 89 | { 90 | if(front==NULL) 91 | return -1; 92 | 93 | return front->data; 94 | } 95 | 96 | int rearQueue() 97 | { 98 | if(rear==NULL) 99 | return -1; 100 | 101 | return rear->data; 102 | } 103 | 104 | int main() 105 | { 106 | boost; 107 | return 0; 108 | } -------------------------------------------------------------------------------- /Day 10-19/Day 13/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Breadth First Search 7 | 8 | vector bfs(vector g[],int v) 9 | { 10 | vector res; 11 | 12 | bool visit[v]; 13 | memset(visit,false,sizeof(visit)); 14 | 15 | queue gq; 16 | 17 | visit[0]=true; 18 | gq.push(0); 19 | 20 | while(!gq.empty()) 21 | { 22 | int i=gq.front(); 23 | res.push_back(i); 24 | gq.pop(); 25 | 26 | for(auto it=g[i].begin();it!=g[i].end();it++) 27 | { 28 | if(!visit[*it]) 29 | { 30 | gq.push(*it); 31 | visit[*it]=true; 32 | } 33 | } 34 | } 35 | 36 | return res; 37 | } 38 | 39 | int main() 40 | { 41 | boost; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Day 10-19/Day 13/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Implement Stack using Queue 7 | 8 | queue q1,q2; 9 | void push(int x) 10 | { 11 | q2.push(x); 12 | while(!q1.empty()) 13 | { 14 | q2.push(q1.front()); 15 | q1.pop(); 16 | } 17 | 18 | queue temp=q1; 19 | q1=q2; 20 | q2=temp; 21 | 22 | return; 23 | } 24 | 25 | int pop() 26 | { 27 | int x=q1.front(); 28 | q1.pop(); 29 | 30 | return x; 31 | } 32 | 33 | int top() 34 | { 35 | return q1.front(); 36 | } 37 | 38 | int main() 39 | { 40 | boost; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Day 10-19/Day 13/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Implement Queue using Stack 7 | 8 | stack s1,s2; 9 | void push(int x) 10 | { 11 | while(!s1.empty()) 12 | { 13 | s2.push(s1.top()); 14 | s1.pop(); 15 | } 16 | 17 | s1.push(x); 18 | 19 | while(!s2.empty()) 20 | { 21 | s1.push(s2.top()); 22 | s2.pop(); 23 | } 24 | 25 | return; 26 | } 27 | 28 | int pop() 29 | { 30 | int x=s1.top(); 31 | s1.pop(); 32 | 33 | return x; 34 | } 35 | 36 | int front() 37 | { 38 | return s1.top(); 39 | } 40 | 41 | int rear() 42 | { 43 | stack temp=s1; 44 | int x; 45 | 46 | while(!s1.empty()) 47 | { 48 | x=s1.top(); 49 | s1.pop(); 50 | } 51 | 52 | return x; 53 | } 54 | 55 | int main() 56 | { 57 | boost; 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Day 10-19/Day 13/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Check for balanced parentheses 7 | 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | string s; 15 | cin>>s; 16 | 17 | stack p; 18 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Next Greater Element 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 17 | } 18 | 19 | vector v; 20 | stack s; 21 | 22 | for(int i=n-1;i>=0;i--) 23 | { 24 | if(s.size()==0) 25 | { 26 | v.push_back(-1); 27 | } 28 | else if(s.top()>a[i]) 29 | { 30 | v.push_back(s.top()); 31 | } 32 | else if(s.top()<=a[i]) 33 | { 34 | while(s.size()>0 && s.top()<=a[i]) 35 | { 36 | s.pop(); 37 | } 38 | if(s.size()==0) 39 | { 40 | v.push_back(-1); 41 | } 42 | else 43 | { 44 | v.push_back(s.top()); 45 | } 46 | } 47 | s.push(a[i]); 48 | } 49 | 50 | reverse(v.begin(),v.end()); 51 | 52 | for(auto it=v.begin();it!=v.end();it++) 53 | { 54 | cout<<*it<<" "; 55 | } 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Day 10-19/Day 14/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "any": "cpp", 5 | "array": "cpp", 6 | "atomic": "cpp", 7 | "bit": "cpp", 8 | "*.tcc": "cpp", 9 | "bitset": "cpp", 10 | "cctype": "cpp", 11 | "cfenv": "cpp", 12 | "charconv": "cpp", 13 | "chrono": "cpp", 14 | "cinttypes": "cpp", 15 | "clocale": "cpp", 16 | "cmath": "cpp", 17 | "codecvt": "cpp", 18 | "complex": "cpp", 19 | "condition_variable": "cpp", 20 | "csetjmp": "cpp", 21 | "csignal": "cpp", 22 | "cstdarg": "cpp", 23 | "cstddef": "cpp", 24 | "cstdint": "cpp", 25 | "cstdio": "cpp", 26 | "cstdlib": "cpp", 27 | "cstring": "cpp", 28 | "ctime": "cpp", 29 | "cuchar": "cpp", 30 | "cwchar": "cpp", 31 | "cwctype": "cpp", 32 | "deque": "cpp", 33 | "forward_list": "cpp", 34 | "list": "cpp", 35 | "map": "cpp", 36 | "set": "cpp", 37 | "unordered_map": "cpp", 38 | "unordered_set": "cpp", 39 | "vector": "cpp", 40 | "exception": "cpp", 41 | "algorithm": "cpp", 42 | "functional": "cpp", 43 | "iterator": "cpp", 44 | "memory": "cpp", 45 | "memory_resource": "cpp", 46 | "numeric": "cpp", 47 | "optional": "cpp", 48 | "random": "cpp", 49 | "ratio": "cpp", 50 | "regex": "cpp", 51 | "string": "cpp", 52 | "string_view": "cpp", 53 | "system_error": "cpp", 54 | "tuple": "cpp", 55 | "type_traits": "cpp", 56 | "utility": "cpp", 57 | "fstream": "cpp", 58 | "future": "cpp", 59 | "initializer_list": "cpp", 60 | "iomanip": "cpp", 61 | "iosfwd": "cpp", 62 | "istream": "cpp", 63 | "limits": "cpp", 64 | "mutex": "cpp", 65 | "new": "cpp", 66 | "ostream": "cpp", 67 | "scoped_allocator": "cpp", 68 | "shared_mutex": "cpp", 69 | "sstream": "cpp", 70 | "stdexcept": "cpp", 71 | "streambuf": "cpp", 72 | "thread": "cpp", 73 | "typeindex": "cpp", 74 | "typeinfo": "cpp", 75 | "valarray": "cpp", 76 | "variant": "cpp" 77 | } 78 | } -------------------------------------------------------------------------------- /Day 10-19/Day 14/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Next Smaller Element 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | 15 | for(int i=0;i>a[i]; 17 | 18 | vector v; 19 | stack s; 20 | 21 | for(int i=0;i=a[i]) 28 | { 29 | while(s.size()>0 && s.top()>=a[i]) 30 | s.pop(); 31 | 32 | if(s.size()==0) 33 | v.push_back(-1); 34 | else 35 | v.push_back(s.top()); 36 | } 37 | 38 | s.push(a[i]); 39 | } 40 | 41 | for(auto it=v.begin();it!=v.end();it++) 42 | cout<<*it<<" "; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Day 10-19/Day 14/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //LRU Cache 7 | 8 | //private: 9 | list lru; 10 | unordered_map::iterator>> mp; 11 | int siz; 12 | 13 | /*public: 14 | LRUCache(int cap) 15 | { 16 | siz=cap; 17 | }*/ 18 | 19 | int get(int key) 20 | { 21 | if(mp.find(key)!=mp.end()) 22 | { 23 | lru.erase(mp[key].second); 24 | lru.push_front(key); 25 | mp[key].second=lru.begin(); 26 | 27 | return mp[key].first; 28 | } 29 | 30 | return -1; 31 | } 32 | 33 | void put(int key, int val) 34 | { 35 | if(mp.find(key)==mp.end()) 36 | { 37 | if(lru.size()==siz) 38 | { 39 | mp.erase(lru.back()); 40 | lru.pop_back(); 41 | } 42 | } 43 | else 44 | lru.erase(mp[key].second); 45 | 46 | lru.push_front(key); 47 | mp[key]={val,lru.begin()}; 48 | 49 | return; 50 | } 51 | 52 | int main() 53 | { 54 | boost; 55 | 56 | int cap; 57 | cin>>cap; 58 | siz=cap; 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Day 10-19/Day 14/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Largest rectangle in histogram 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 17 | } 18 | 19 | //NSR index 20 | vector r; 21 | stack> sr; 22 | 23 | for(int i=n-1;i>=0;i--) 24 | { 25 | if(sr.size()==0) 26 | { 27 | r.push_back(n); 28 | } 29 | else if(sr.top().first=a[i]) 34 | { 35 | while(sr.size()>0 && sr.top().first>=a[i]) 36 | { 37 | sr.pop(); 38 | } 39 | if(sr.size()==0) 40 | { 41 | r.push_back(n); 42 | } 43 | else 44 | { 45 | r.push_back(sr.top().second); 46 | } 47 | } 48 | sr.push({a[i],i}); 49 | } 50 | 51 | reverse(r.begin(),r.end()); 52 | 53 | //NSL index 54 | vector l; 55 | stack> sl; 56 | 57 | for(int i=0;i=a[i]) 68 | { 69 | while(sl.size()>0 && sl.top().first>=a[i]) 70 | { 71 | sl.pop(); 72 | } 73 | if(sl.size()==0) 74 | { 75 | l.push_back(-1); 76 | } 77 | else 78 | { 79 | l.push_back(sl.top().second); 80 | } 81 | } 82 | sl.push({a[i],i}); 83 | } 84 | 85 | vector area; 86 | 87 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Sliding window maximum 7 | 8 | vector maxSlidingWindow(vector& a, int k) 9 | { 10 | if(a.size()==1) 11 | return a; 12 | 13 | int i=0,j=0,n=a.size(); 14 | list l; 15 | vector res; 16 | 17 | while(j 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Implement Min Stack 7 | 8 | stack s; 9 | int minEle; 10 | 11 | void push(int a) 12 | { 13 | if(s.empty()) 14 | { 15 | s.push(a); 16 | minEle=a; 17 | } 18 | else 19 | { 20 | if(a>=minEle) 21 | s.push(a); 22 | else 23 | { 24 | s.push(2*a -minEle); 25 | minEle=a; 26 | } 27 | } 28 | 29 | return; 30 | } 31 | 32 | bool isFull(int n) 33 | { 34 | if(s.size()==n) 35 | return true; 36 | 37 | return false; 38 | } 39 | 40 | bool isEmpty() 41 | { 42 | if(s.empty()) 43 | return true; 44 | 45 | return false; 46 | } 47 | 48 | int pop() 49 | { 50 | int ans; 51 | 52 | if(s.top()>=minEle) 53 | ans = s.top(); 54 | else 55 | { 56 | minEle = 2*minEle - s.top(); 57 | ans = minEle; 58 | } 59 | 60 | s.pop(); 61 | 62 | return ans; 63 | } 64 | 65 | int getMin() 66 | { 67 | return minEle; 68 | } 69 | 70 | int main() 71 | { 72 | boost; 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Day 10-19/Day 14/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Rotten Oranges (Using BFS) 7 | 8 | bool isValid(int i, int j, int R, int C) 9 | { 10 | return (i >= 0 && j >= 0 && i < R && j < C); 11 | } 12 | 13 | int orangesRotting(vector>& grid) 14 | { 15 | queue> q; 16 | int ans=0,m=grid.size(),n=grid[0].size(); 17 | 18 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string s; 11 | cin>>s; 12 | 13 | vector v; 14 | string temp=""; 15 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | int lcs(string a, string b) 8 | { 9 | int m=a.size(),n=b.size(); 10 | int DP[m+2][n+2]; 11 | 12 | for(int i=0;i>a; 40 | reverse(a.begin(),a.end()); 41 | string b=a; 42 | 43 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | int val(char r) 8 | { 9 | if(r=='I') 10 | return 1; 11 | if(r=='V') 12 | return 5; 13 | if(r=='X') 14 | return 10; 15 | if(r=='L') 16 | return 50; 17 | if(r=='C') 18 | return 100; 19 | if(r=='D') 20 | return 500; 21 | if(r=='M') 22 | return 1000; 23 | 24 | return 0; 25 | } 26 | 27 | int main() 28 | { 29 | boost; 30 | string s; 31 | cin>>s; 32 | 33 | int n=s.size(),res=0; 34 | for(int i=0;i=s2) 43 | res+=s1; 44 | else 45 | { 46 | res+=(s2-s1); 47 | i++; 48 | } 49 | } 50 | else 51 | res+=s1; 52 | } 53 | 54 | cout<>num; 58 | 59 | string str; 60 | int m[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000}; 61 | string sym[] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; 62 | 63 | int j=12; 64 | while(num) 65 | { 66 | int temp=num/m[j]; 67 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | int atoi(char* s) 8 | { 9 | if(*s=='\0') 10 | return -1; 11 | 12 | int sign=1,i=0; 13 | if(s[0]=='-') 14 | { 15 | sign=-1; 16 | i++; 17 | } 18 | 19 | int res=0; 20 | while(s[i]!='\0') 21 | { 22 | if(s[i]>='0' && s[i]<='9') 23 | res = (res*10) + (s[i]-'0'); 24 | else 25 | return -1; 26 | 27 | i++; 28 | } 29 | 30 | return sign*res; 31 | } 32 | 33 | int strstr(string s, string x) 34 | { 35 | int m=s.size(),n=x.size(); 36 | 37 | if(x.size()==0) 38 | return 0; 39 | 40 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | string longestCommonPrefix(vector& strs) 8 | { 9 | string res; 10 | int minLen=INT_MAX; 11 | 12 | if(strs.empty()) 13 | return res; 14 | 15 | for(auto it=strs.begin();it!=strs.end();it++) 16 | minLen = min(int((*it).size()),minLen); 17 | 18 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | vector rabinKarp(string pat, string txt) 8 | { 9 | vector res; 10 | int m=pat.size(),n=txt.size(),j,p=0,t=0,h=1; 11 | 12 | if(m>n) 13 | return res; 14 | else if(m==n) 15 | { 16 | if(pat==txt) 17 | res.push_back(0); 18 | return res; 19 | } 20 | 21 | for(int i=1;i<=m-1;i++) 22 | h=(h*256)%101; 23 | 24 | for(int i=0;i>txt>>pat; 61 | 62 | vector ans = rabinKarp(pat,txt); 63 | for(auto it=ans.begin();it!=ans.end();it++) 64 | cout<<*it<<" "; 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Day 10-19/Day 15/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/getoxdev/SDE_Interview_Problems/d9203a22ed295b7a5e8ee45f236281c95975e23f/Day 10-19/Day 15/a.exe -------------------------------------------------------------------------------- /Day 10-19/Day 16/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | void zarray(string pat,int z[]) 8 | { 9 | int m = pat.size(),l,r,k; 10 | z[0]=0; 11 | l=r=0; 12 | 13 | for(int i=1;ir) 16 | { 17 | l=r=i; 18 | 19 | while(r 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | void lpsArray(string pat,int lps[]) 8 | { 9 | int index=0,i=1; 10 | lps[0]=0; 11 | 12 | while(i kmp(string txt, string pat) 36 | { 37 | vector res; 38 | int m=pat.size(),n=txt.size(); 39 | int lps[m]; 40 | 41 | lpsArray(pat,lps); 42 | 43 | int i=0,j=0; 44 | 45 | while(i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | void piarray(string pat,vector &lps) 8 | { 9 | int index=0,i=1; 10 | lps[0]=0; 11 | 12 | while(i lps; 39 | string str=s+"$"; 40 | reverse(s.begin(),s.end()); 41 | str+=s; 42 | 43 | piarray(str,lps); 44 | 45 | return (s.size()-lps.back()); 46 | } 47 | 48 | int main() 49 | { 50 | boost; 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Day 10-19/Day 16/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | bool areAnagram(string a,string b) 7 | { 8 | int cta[256]={0}; 9 | int ctb[256]={0}; 10 | 11 | if(a.size()!=b.size()) 12 | return false; 13 | 14 | if(a.empty() || b.empty()) 15 | return false; 16 | 17 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | string countAndSay(int n) 8 | { 9 | vector DP(n+2); 10 | 11 | DP[0]="0"; 12 | DP[1]="1"; 13 | 14 | for(int i=2;i<=n;i++) 15 | { 16 | string temp = DP[i-1]+'$'; 17 | int siz = DP[i-1].size(); 18 | 19 | int ct=1; 20 | for(int j=1;j 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | int compareVersion(string v1, string v2) 8 | { 9 | vector s1,s2; 10 | int temp=0; 11 | 12 | for(auto it=v1.begin();it!=v1.end();it++) 13 | { 14 | if((*it)=='.') 15 | { 16 | s1.push_back(temp); 17 | temp=0; 18 | } 19 | else 20 | temp=temp*10+((*it)-'0'); 21 | } 22 | 23 | s1.push_back(temp); 24 | 25 | temp=0; 26 | for(auto it=v2.begin();it!=v2.end();it++) 27 | { 28 | if((*it)=='.') 29 | { 30 | s2.push_back(temp); 31 | temp=0; 32 | } 33 | else 34 | temp=temp*10+((*it)-'0'); 35 | } 36 | 37 | s2.push_back(temp); 38 | 39 | if(s1.size()>s2.size()) 40 | { 41 | int siz=s1.size()-s2.size(); 42 | for(int i=0;is2[i]) 55 | return 1; 56 | else if(s1[i] 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Inorder Traversal (with and without recursion) 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | //Recursion 20 | void inOrder(Node* root,vector &v) 21 | { 22 | if(root==NULL) 23 | return; 24 | 25 | inOrder(root->left,v); 26 | v.push_back(root->data); 27 | inOrder(root->right,v); 28 | 29 | return; 30 | } 31 | 32 | //Iterative 33 | vector inorder(Node *root) 34 | { 35 | vector res; 36 | stack> s; 37 | s.push({root,1}); 38 | 39 | while(!s.empty()) 40 | { 41 | if(s.top().second==1) 42 | { 43 | Node *temp=s.top().first; 44 | res.push_back(temp->data); 45 | s.top().second++; 46 | 47 | if(temp->left) 48 | s.push({temp->left,1}); 49 | } 50 | else if(s.top().second==2) 51 | { 52 | Node *temp=s.top().first; 53 | s.top().second++; 54 | 55 | if(temp->right) 56 | s.push({temp->right,1}); 57 | } 58 | else if(s.top().second==3) 59 | s.pop(); 60 | } 61 | 62 | return res; 63 | } 64 | 65 | int main() 66 | { 67 | boost; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Day 10-19/Day 17/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Preorder Traversal (with and without recursion) 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | //Recursion 20 | void preOrder(Node* root,vector &v) 21 | { 22 | if(root==NULL) 23 | return; 24 | 25 | v.push_back(root->data); 26 | preOrder(root->left,v); 27 | preOrder(root->right,v); 28 | 29 | return; 30 | } 31 | 32 | //Iteration 33 | vector preorder(Node *root) 34 | { 35 | vector res; 36 | stack> s; 37 | s.push({root,1}); 38 | 39 | while(!s.empty()) 40 | { 41 | if(s.top().second==1) 42 | { 43 | Node *temp=s.top().first; 44 | s.top().second++; 45 | 46 | if(temp->left) 47 | s.push({temp->left,1}); 48 | } 49 | else if(s.top().second==2) 50 | { 51 | Node *temp=s.top().first; 52 | res.push_back(temp->data); 53 | s.top().second++; 54 | 55 | if(temp->right) 56 | s.push({temp->right,1}); 57 | } 58 | else if(s.top().second==3) 59 | s.pop(); 60 | } 61 | 62 | return res; 63 | } 64 | 65 | int main() 66 | { 67 | boost; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Day 10-19/Day 17/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Postorder Traversal (with and without recursion) 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | //Recursion 20 | void postOrder(Node* root,vector &v) 21 | { 22 | if(root==NULL) 23 | return; 24 | 25 | postOrder(root->left,v); 26 | postOrder(root->right,v); 27 | v.push_back(root->data); 28 | 29 | return; 30 | } 31 | 32 | //Iteration 33 | vector postorder(Node *root) 34 | { 35 | vector res; 36 | stack> s; 37 | s.push({root,1}); 38 | 39 | while(!s.empty()) 40 | { 41 | if(s.top().second==1) 42 | { 43 | Node *temp=s.top().first; 44 | s.top().second++; 45 | 46 | if(temp->left) 47 | s.push({temp->left,1}); 48 | } 49 | else if(s.top().second==2) 50 | { 51 | Node *temp=s.top().first; 52 | s.top().second++; 53 | 54 | if(temp->right) 55 | s.push({temp->right,1}); 56 | } 57 | else if(s.top().second==3) 58 | { 59 | Node *temp=s.top().first; 60 | res.push_back(temp->data); 61 | s.pop(); 62 | } 63 | } 64 | 65 | return res; 66 | } 67 | 68 | int main() 69 | { 70 | boost; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Day 10-19/Day 17/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Left View of Binary Tree 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | vector leftView(Node *root) 20 | { 21 | vector res; 22 | 23 | if(!root) 24 | return res; 25 | 26 | queue q; 27 | q.push(root); 28 | 29 | while(!q.empty()) 30 | { 31 | int sz=q.size(); 32 | for(int i=0;idata); 39 | 40 | if(temp->left) 41 | q.push(temp->left); 42 | 43 | if(temp->right) 44 | q.push(temp->right); 45 | } 46 | } 47 | 48 | return res; 49 | } 50 | 51 | int main() 52 | { 53 | boost; 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Day 10-19/Day 17/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Bottom View of Binary Tree 7 | 8 | struct Node{ 9 | 10 | int data; 11 | Node *left,*right; 12 | 13 | Node(int key) 14 | { 15 | data=key; 16 | left=right=NULL; 17 | } 18 | }; 19 | 20 | void bottomView(Node *root, int chgt, int hd, map> &mt) 21 | { 22 | if(root==NULL) 23 | return; 24 | 25 | if(mt.find(hd)==mt.end()) 26 | mt[hd]={root->data,chgt}; 27 | else 28 | { 29 | pair p = mt[hd]; 30 | 31 | if(chgt>=p.second) 32 | mt[hd]={root->data,chgt}; 33 | } 34 | 35 | bottomView(root->left,chgt+1,hd-1,mt); 36 | bottomView(root->right,chgt+1,hd+1,mt); 37 | 38 | return; 39 | } 40 | 41 | int main() 42 | { 43 | boost; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Day 10-19/Day 17/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Top View of Binary Tree 7 | 8 | struct Node{ 9 | 10 | int data; 11 | Node *left,*right; 12 | 13 | Node(int key) 14 | { 15 | data=key; 16 | left=right=NULL; 17 | } 18 | }; 19 | 20 | void topView(Node *root, int clvl, int hd, map> &mt) 21 | { 22 | if(root==NULL) 23 | return; 24 | 25 | if(mt.find(hd)==mt.end()) 26 | mt[hd]={root->data,clvl}; 27 | else 28 | { 29 | pair p = mt[hd]; 30 | 31 | if(clvldata,clvl}; 33 | } 34 | 35 | topView(root->left,clvl+1,hd-1,mt); 36 | topView(root->right,clvl+1,hd+1,mt); 37 | 38 | return; 39 | } 40 | 41 | int main() 42 | { 43 | boost; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Day 10-19/Day 18/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Level-Order Traversal/Level-Order Traversal in Spiral form 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x) 15 | { 16 | data = x; 17 | left = right = NULL; 18 | } 19 | }; 20 | 21 | int height(Node* root) 22 | { 23 | if(root==NULL) 24 | return 0; 25 | 26 | int lh=height(root->left)+1; 27 | int rh=height(root->right)+1; 28 | 29 | return max(lh,rh); 30 | } 31 | 32 | vector levelOrder(Node* node) 33 | { 34 | vector res; 35 | 36 | if(!node) 37 | return res; 38 | 39 | queue q; 40 | q.push(node); 41 | 42 | while(!q.empty()) 43 | { 44 | int sz=q.size(); 45 | 46 | for(int i=0;idata); 51 | 52 | if(temp->left) 53 | q.push(temp->left); 54 | 55 | if(temp->right) 56 | q.push(temp->right); 57 | } 58 | } 59 | 60 | return res; 61 | } 62 | 63 | void printSpiralUtil(Node *root,int level,bool bl) 64 | { 65 | if(root==NULL) 66 | return; 67 | 68 | if(level==1) 69 | cout<data<<" "; 70 | else if(level>1) 71 | { 72 | if(bl) 73 | { 74 | printSpiralUtil(root->left,level-1,bl); 75 | printSpiralUtil(root->right,level-1,bl); 76 | } 77 | else 78 | { 79 | printSpiralUtil(root->right,level-1,bl); 80 | printSpiralUtil(root->left,level-1,bl); 81 | } 82 | } 83 | 84 | return; 85 | } 86 | 87 | void printSpiral(Node *root) 88 | { 89 | int h=height(root); 90 | bool bl=true; 91 | 92 | for(int i=1;i<=h;i++) 93 | { 94 | printSpiralUtil(root,i,bl); 95 | bl=!bl; 96 | } 97 | 98 | return; 99 | } 100 | 101 | int main() 102 | { 103 | boost; 104 | return 0; 105 | } -------------------------------------------------------------------------------- /Day 10-19/Day 18/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Height of a Binary Tree 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x) 15 | { 16 | data = x; 17 | left = right = NULL; 18 | } 19 | }; 20 | 21 | int height(Node* root) 22 | { 23 | if(root==NULL) 24 | return 0; 25 | 26 | int lh=height(root->left)+1; 27 | int rh=height(root->right)+1; 28 | 29 | return max(lh,rh); 30 | } 31 | 32 | int main() 33 | { 34 | boost; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Day 10-19/Day 18/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Diameter of a Binary Tree 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x) 15 | { 16 | data = x; 17 | left = right = NULL; 18 | } 19 | }; 20 | 21 | int diameter(Node* root,int &ans) 22 | { 23 | if(root==NULL) 24 | return 0; 25 | 26 | int lh = diameter(root->left,ans); 27 | int rh = diameter(root->right,ans); 28 | 29 | //Largest diameter for the current node 30 | ans=max(ans,1+lh+rh); 31 | 32 | return max(lh,rh)+1; 33 | } 34 | 35 | int main() 36 | { 37 | boost; 38 | 39 | int ans = INT_MIN; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Day 10-19/Day 18/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Check if Binary Tree is height balanced or not 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x) 15 | { 16 | data = x; 17 | left = right = NULL; 18 | } 19 | }; 20 | 21 | int height(Node* root) 22 | { 23 | if(root==NULL) 24 | return 0; 25 | 26 | int lh=height(root->left); 27 | int rh=height(root->right); 28 | 29 | return max(lh,rh)+1; 30 | } 31 | 32 | bool balanced(Node* root) 33 | { 34 | if(root==NULL) 35 | return true; 36 | 37 | int lh=height(root->left); 38 | int rh=height(root->right); 39 | 40 | if(abs(lh-rh)<=1 && balanced(root->left) && balanced(root->right)) 41 | return true; 42 | 43 | return false; 44 | } 45 | 46 | int main() 47 | { 48 | boost; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Day 10-19/Day 18/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Lowest Common Ancestor in a Binary Tree 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x){ 15 | data = x; 16 | left = right = NULL; 17 | } 18 | }; 19 | 20 | bool findPath(Node *root, vector &path, int k) 21 | { 22 | if(root==NULL) 23 | return false; 24 | 25 | path.push_back(root->data); 26 | 27 | if(root->data==k) 28 | return true; 29 | 30 | if((root->left && findPath(root->left,path,k))||(root->right && findPath(root->right,path,k))) 31 | return true; 32 | 33 | path.pop_back(); 34 | 35 | return false; 36 | } 37 | 38 | /* If n1 and n2 are present, return pointer 39 | to LCA. If both are not present, return 40 | NULL.*/ 41 | 42 | Node* lca(Node* root ,int n1 ,int n2 ) 43 | { 44 | vector path1,path2; 45 | 46 | if(!findPath(root,path1,n1) || !findPath(root,path2,n2)) 47 | return NULL; 48 | 49 | int i; 50 | for(i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Check if two trees are identical or not 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x){ 15 | data = x; 16 | left = right = NULL; 17 | } 18 | }; 19 | 20 | /* Should return true if trees with roots as r1 and 21 | r2 are identical */ 22 | bool isIdentical(Node *r1, Node *r2) 23 | { 24 | if(r1==NULL && r2==NULL) 25 | return true; 26 | 27 | if(r1!=NULL && r2!=NULL) 28 | { 29 | if(r1->data==r2->data && isIdentical(r1->left,r2->left) && isIdentical(r1->right,r2->right)) 30 | return true; 31 | } 32 | 33 | return false; 34 | } 35 | int main() 36 | { 37 | boost; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Day 10-19/Day 19/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Maximum Path Sum 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x){ 15 | data = x; 16 | left = right = NULL; 17 | } 18 | }; 19 | 20 | int maxNode(Node *root, int &res) 21 | { 22 | if(root==NULL) 23 | return 0; 24 | 25 | int lh = maxNode(root->left,res); 26 | int rh = maxNode(root->right,res); 27 | 28 | int temp=max(root->data,root->data+max(lh,rh)); 29 | 30 | res=max(res,max(temp,root->data+lh+rh)); 31 | 32 | return temp; 33 | } 34 | 35 | int maxPath(Node* node) 36 | { 37 | int res=INT_MIN; 38 | int temp=maxNode(node,res); 39 | 40 | return res; 41 | } 42 | 43 | int main() 44 | { 45 | boost; 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Day 10-19/Day 19/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Construct Binary Tree from inorder and preorder 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | Node* getTree(int in[],int pre[],int inStart,int inEnd,int &preIndex) 20 | { 21 | if(inStart>inEnd) 22 | return NULL; 23 | 24 | Node *root = new Node(pre[preIndex++]); 25 | 26 | int inIndex; 27 | for(int i=inStart;i<=inEnd;i++) 28 | { 29 | if(in[i]==root->data) 30 | { 31 | inIndex=i; 32 | break; 33 | } 34 | } 35 | 36 | root->left=getTree(in,pre,inStart,inIndex-1,preIndex); 37 | root->right=getTree(in,pre,inIndex+1,inEnd,preIndex); 38 | 39 | return root; 40 | } 41 | 42 | Node* buildTree(int in[],int pre[], int n) 43 | { 44 | int preIndex=0; 45 | Node *root = getTree(in,pre,0,n-1,preIndex); 46 | 47 | return root; 48 | } 49 | 50 | int main() 51 | { 52 | boost; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Day 10-19/Day 19/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Construct Binary Tree from inorder and postorder 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x){ 15 | data = x; 16 | left = right = NULL; 17 | } 18 | }; 19 | 20 | Node* getTree(int in[],int post[],int inStart,int inEnd,int &postIndex) 21 | { 22 | if(inStart>inEnd) 23 | return NULL; 24 | 25 | Node *root = new Node(post[postIndex--]); 26 | 27 | if(inStart==inEnd) 28 | return root; 29 | 30 | int inIndex; 31 | for(int i=inStart;i<=inEnd;i++) 32 | { 33 | if(in[i]==root->data) 34 | { 35 | inIndex=i; 36 | break; 37 | } 38 | } 39 | 40 | root->right = getTree(in,post,inIndex+1,inEnd,postIndex); 41 | root->left = getTree(in,post,inStart,inIndex-1,postIndex); 42 | 43 | return root; 44 | } 45 | 46 | Node *buildTree(int in[], int post[], int n) 47 | { 48 | int postIndex=n-1; 49 | Node *root=getTree(in,post,0,n-1,postIndex); 50 | 51 | return root; 52 | } 53 | 54 | int main() 55 | { 56 | boost; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Day 10-19/Day 19/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Symmetric Binary Tree 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | bool sym(Node *root1, Node *root2) 20 | { 21 | if(!root1 && !root2) 22 | return true; 23 | 24 | if(root1 && root2) 25 | return (sym(root1->left,root2->right) && sym(root1->right,root2->left)); 26 | 27 | return false; 28 | } 29 | 30 | // Return true or false denoting whether the tree is symmetric or not 31 | bool isSymmetric(struct Node* root) 32 | { 33 | if(sym(root,root)) 34 | return true; 35 | else 36 | return false; 37 | } 38 | 39 | int main() 40 | { 41 | boost; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Day 10-19/Day 19/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Flatten Binary Tree to LinkedList 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | void flatten(Node *root) 20 | { 21 | if(!root) 22 | return; 23 | 24 | stack st; 25 | st.push(root); 26 | 27 | while(!st.empty()) 28 | { 29 | Node *temp=st.top(); 30 | st.pop(); 31 | 32 | if(temp->right) 33 | st.push(temp->right); 34 | 35 | if(temp->left) 36 | st.push(temp->left); 37 | 38 | if(!st.empty()) 39 | temp->right=st.top(); 40 | 41 | temp->left=NULL; 42 | } 43 | 44 | return; 45 | } 46 | 47 | int main() 48 | { 49 | boost; 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Day 10-19/Day 19/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Check if Binary Tree is a mirror of itself or not 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | bool sym(Node *root1, Node *root2) 20 | { 21 | if(!root1 && !root2) 22 | return true; 23 | 24 | if(root1 && root2) 25 | return (sym(root1->left,root2->right) && sym(root1->right,root2->left)); 26 | 27 | return false; 28 | } 29 | 30 | bool isMirror(struct Node* root) 31 | { 32 | if(sym(root,root)) 33 | return true; 34 | else 35 | return false; 36 | } 37 | 38 | int main() 39 | { 40 | boost; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Day 20-26/Day 20/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | } 5 | } -------------------------------------------------------------------------------- /Day 20-26/Day 20/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Populate next right pointers of a Tree 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node *left=NULL,*right=NULL,*nextRight=NULL; 12 | }; 13 | 14 | Node* connect(Node* root) 15 | { 16 | if(!root) 17 | return root; 18 | 19 | queue q; 20 | q.push(root); 21 | 22 | while(!q.empty()) 23 | { 24 | int sz=q.size(); 25 | for(int i=0;inextRight=q.front(); 32 | 33 | if(temp->left) 34 | q.push(temp->left); 35 | 36 | if(temp->right) 37 | q.push(temp->right); 38 | } 39 | } 40 | 41 | return root; 42 | } 43 | 44 | int main() 45 | { 46 | boost; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Day 20-26/Day 20/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Search given key in a BST 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | Node* searchBST(Node *root,int key) 20 | { 21 | if(root==NULL || root->data==key) 22 | return root; 23 | 24 | if(keydata) 25 | return searchBST(root->left,key); 26 | else 27 | return searchBST(root->right,key); 28 | } 29 | 30 | int main() 31 | { 32 | boost; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Day 20-26/Day 20/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Construct BST from given keys 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | Node* getBST(vector nums,int l,int r) 20 | { 21 | if(l>r) 22 | return NULL; 23 | 24 | int m=l+(r-l)/2; 25 | Node* root = new Node(nums[m]); 26 | 27 | root->left = getBST(nums,l,m-1); 28 | root->right = getBST(nums,m+1,r); 29 | 30 | return root; 31 | } 32 | 33 | Node* sortedArrayToBST(vector& nums) 34 | { 35 | return getBST(nums,0,nums.size()-1); 36 | } 37 | 38 | int main() 39 | { 40 | boost; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Day 20-26/Day 20/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Check if a Binary Tree is a BST or not 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | bool checkBST(Node *root,Node *l,Node *r) 20 | { 21 | if(root==NULL) 22 | return true; 23 | 24 | if(l!=NULL && root->data<=l->data) 25 | return false; 26 | 27 | if(r!=NULL && root->data>=r->data) 28 | return false; 29 | 30 | return checkBST(root->left,l,root) && checkBST(root->right,root,r); 31 | } 32 | 33 | bool isBST(Node* root) 34 | { 35 | return checkBST(root,NULL,NULL); 36 | } 37 | 38 | int main() 39 | { 40 | boost; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Day 20-26/Day 20/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find LCA of two nodes in a BST 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | // Returns the LCA of the nodes with values n1 and n2 in the BST rooted at 'root' 20 | Node* LCA(Node *root, int n1, int n2) 21 | { 22 | if(root->datadataright,n1,n2); 24 | 25 | if(root->data>n1 && root->data>n2) 26 | return LCA(root->left,n1,n2); 27 | 28 | return root; 29 | } 30 | 31 | int main() 32 | { 33 | boost; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Day 20-26/Day 20/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find the inorder predecessor/successor of a given Key in BST 7 | 8 | struct Node 9 | { 10 | int key; 11 | Node *left; 12 | Node *right; 13 | 14 | Node(int val) 15 | { 16 | key = val; 17 | left = right = NULL; 18 | } 19 | }; 20 | 21 | void findPreSuc(Node* root, Node*& pre, Node*& suc, int key) 22 | { 23 | if(root==NULL) 24 | return; 25 | 26 | if(root->key==key) 27 | { 28 | if(root->left!=NULL) 29 | { 30 | //Go to max value of the left subtree 31 | Node *t1=root->left; 32 | while(t1->right) 33 | t1=t1->right; 34 | pre=t1; 35 | } 36 | 37 | if(root->right!=NULL) 38 | { 39 | //Go to min value of the right subtree 40 | Node *t2=root->right; 41 | while(t2->left) 42 | t2=t2->left; 43 | suc=t2; 44 | } 45 | } 46 | 47 | //Go to right subtree 48 | if(root->keyright,pre,suc,key); 52 | } 53 | //Go to left subtree 54 | else if(root->key>key) 55 | { 56 | suc=root; 57 | findPreSuc(root->left,pre,suc,key); 58 | } 59 | 60 | return; 61 | } 62 | 63 | int main() 64 | { 65 | boost; 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Day 20-26/Day 21/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Floor and Ceil in a BST 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node *left; 12 | Node *right; 13 | 14 | Node(int val) 15 | { 16 | data = val; 17 | left = right = NULL; 18 | } 19 | }; 20 | 21 | int floor(Node* root, int key) 22 | { 23 | if(root==NULL) 24 | return INT_MAX; 25 | 26 | if(root->data==key) 27 | return root->data; 28 | 29 | //Go to left subtree 30 | if(root->data>key) 31 | return floor(root->left,key); 32 | 33 | //Go to right subtree 34 | int f=floor(root->right,key); 35 | 36 | return (f<=key)?f:root->data; 37 | } 38 | 39 | int ceil(Node* root,int key) 40 | { 41 | if(root==NULL) 42 | return INT_MIN; 43 | 44 | if(root->data==key) 45 | return root->data; 46 | 47 | //Go to right subtree 48 | if(root->dataright,key); 50 | 51 | //Go to left subtree 52 | int c=floor(root->left,key); 53 | 54 | return (c>=key)?c:root->data; 55 | } 56 | 57 | void floorCeil(Node *root, int key,int &f,int &c) 58 | { 59 | if(!root) 60 | return; 61 | 62 | while(root) 63 | { 64 | if(root->data==key) 65 | { 66 | f=root->data; 67 | c=root->data; 68 | return; 69 | } 70 | //Go to left subtree to find floor 71 | else if(root->data>key) 72 | { 73 | c=root->data; 74 | root=root->left; 75 | } 76 | //Go to right subtree to find ceil 77 | else if(root->datadata; 80 | root=root->right; 81 | } 82 | } 83 | 84 | return; 85 | } 86 | 87 | int main() 88 | { 89 | boost; 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Day 20-26/Day 21/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find kth smallest and kth largest element in a BST 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node *left; 12 | Node *right; 13 | 14 | Node(int val) 15 | { 16 | data = val; 17 | left = right = NULL; 18 | } 19 | }; 20 | 21 | void inOrder(Node *root,vector &v) 22 | { 23 | if(!root) 24 | return; 25 | 26 | inOrder(root->left,v); 27 | v.push_back(root->data); 28 | inOrder(root->right,v); 29 | 30 | return; 31 | } 32 | 33 | int kthSmallest(Node* root, int k) 34 | { 35 | vector v; 36 | inOrder(root,v); 37 | 38 | return v[k-1]; 39 | } 40 | 41 | int kthLargest(Node* root, int k) 42 | { 43 | vector v; 44 | inOrder(root,v); 45 | reverse(v.begin(),v.end()); 46 | 47 | return v[k-1]; 48 | } 49 | 50 | int main() 51 | { 52 | boost; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Day 20-26/Day 21/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Find a pair with given sum in the BST 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | bool inOrderSearch(Node *root,int sum,set &s) 20 | { 21 | if(root==NULL) 22 | return false; 23 | 24 | if(inOrderSearch(root->left,sum,s)) 25 | return true; 26 | 27 | if(s.find(sum-(root->data))!=s.end()) 28 | return true; 29 | else 30 | s.insert(root->data); 31 | 32 | return inOrderSearch(root->right,sum,s); 33 | } 34 | 35 | // root : the root Node of the given BST 36 | // target : the target sum 37 | int isPairPresent(struct Node *root, int target) 38 | { 39 | set s; 40 | 41 | if(inOrderSearch(root,target,s)) 42 | return 1; 43 | 44 | return 0; 45 | } 46 | 47 | int main() 48 | { 49 | boost; 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Day 20-26/Day 21/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //BST Iterator 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node *left; 12 | Node *right; 13 | 14 | Node(int val) { 15 | data = val; 16 | left = right = NULL; 17 | } 18 | }; 19 | stack s; 20 | 21 | void pushLeft(Node *node) 22 | { 23 | while(node!=NULL) 24 | { 25 | s.push(node); 26 | node = node->left; 27 | } 28 | 29 | return; 30 | } 31 | 32 | void initialize(Node* root) 33 | { 34 | pushLeft(root); 35 | } 36 | 37 | int next() 38 | { 39 | int x = s.top()->data; 40 | Node *curr = (s.top())->right; 41 | s.pop(); 42 | pushLeft(curr); 43 | 44 | return x; 45 | } 46 | 47 | bool hasNext() 48 | { 49 | if(s.empty()) 50 | return false; 51 | 52 | return true; 53 | } 54 | 55 | int main() 56 | { 57 | boost; 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Day 20-26/Day 21/Q5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Size of the largest BST in a Binary Tree 7 | 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | struct Info{ 20 | int siz; 21 | int mn; 22 | int mx; 23 | int ans; 24 | bool isBST; 25 | }; 26 | 27 | Info checkBST(Node *root) 28 | { 29 | if(root==NULL) 30 | return {0,INT_MAX,INT_MIN,0,true}; 31 | 32 | if(root->left==NULL && root->right==NULL) 33 | return {1,root->data,root->data,1,true}; 34 | 35 | Info lTree=checkBST(root->left); 36 | Info rTree=checkBST(root->right); 37 | 38 | Info curr; 39 | curr.siz=1+lTree.siz+rTree.siz; 40 | curr.mn=min(root->data,min(lTree.mn,rTree.mn)); 41 | curr.mx=max(root->data,max(lTree.mx,rTree.mx)); 42 | 43 | if(lTree.isBST && rTree.isBST && root->data>lTree.mx && root->data 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Serialize and Deserialize a Binary Tree 7 | 8 | struct TreeNode 9 | { 10 | int val; 11 | TreeNode *left; 12 | TreeNode *right; 13 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 14 | }; 15 | 16 | // Encodes a binary tree to a string 17 | string serialize(TreeNode* root) 18 | { 19 | if(root==NULL) 20 | return "NULL"; 21 | 22 | string lefts=serialize(root->left); 23 | string rights=serialize(root->right); 24 | 25 | return to_string(root->val)+"-"+lefts+"-"+rights; 26 | } 27 | 28 | TreeNode* deserializeHelper(queue &q) 29 | { 30 | if(q.empty()) 31 | return NULL; 32 | 33 | string num=q.front(); 34 | q.pop(); 35 | 36 | if(num=="NULL") 37 | return NULL; 38 | 39 | TreeNode* node=new TreeNode(stoi(num)); 40 | node->left=deserializeHelper(q); 41 | node->right=deserializeHelper(q); 42 | 43 | return node; 44 | } 45 | // Decodes the encoded string to a binary tree 46 | TreeNode* deserialize(string data) 47 | { 48 | queue q; 49 | string temp=""; 50 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Binary Tree to Doubly Linked List 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node* left; 12 | struct Node* right; 13 | 14 | Node(int x){ 15 | data = x; 16 | left = right = NULL; 17 | } 18 | }; 19 | 20 | void recurTree(Node *root,Node **prev,Node **head) 21 | { 22 | if(root==NULL) 23 | return; 24 | 25 | recurTree(root->left,prev,head); 26 | 27 | if((*prev)==NULL) 28 | (*head)=root; 29 | else 30 | { 31 | root->left=(*prev); 32 | (*prev)->right=root; 33 | } 34 | 35 | (*prev)=root; 36 | 37 | recurTree(root->right,prev,head); 38 | 39 | return; 40 | } 41 | 42 | //Function to convert binary tree to doubly linked list and return it. 43 | Node * bToDLL(Node *root) 44 | { 45 | Node *head=NULL,*prev=NULL; 46 | recurTree(root,&prev,&head); 47 | 48 | return head; 49 | } 50 | 51 | int main() 52 | { 53 | boost; 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Day 20-26/Day 22/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Median in a stream of running integers 7 | 8 | priority_queue q1; 9 | priority_queue,greater> q2; 10 | 11 | void insertHeap(int num) 12 | { 13 | if(q1.empty()||q1.top()>num) 14 | q1.push(num); 15 | else 16 | q2.push(num); 17 | 18 | if(q1.size()>q2.size()+1) 19 | { 20 | q2.push(q1.top()); 21 | q1.pop(); 22 | } 23 | else if(q2.size()>q1.size()+1) 24 | { 25 | q1.push(q2.top()); 26 | q2.pop(); 27 | } 28 | 29 | return; 30 | } 31 | 32 | // Function to return getMedian 33 | double getMedian() 34 | { 35 | if(q1.size()>q2.size()) 36 | return double(q1.top()); 37 | else if(q2.size()>q1.size()) 38 | return double(q2.top()); 39 | else 40 | return double((double(q1.top())+double(q2.top()))/2); 41 | } 42 | 43 | int main() 44 | { 45 | boost; 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Day 20-26/Day 22/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //kth largest element in a stream 7 | 8 | vector kthLargest(int k, int arr[], int n) 9 | { 10 | priority_queue,greater> q; 11 | vector res; 12 | for(int i=0;ik) 16 | q.pop(); 17 | 18 | if(q.size() 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Distinct numbers in a window 7 | 8 | vector countDistinct (int a[], int n, int k) 9 | { 10 | vector res; 11 | int i=0,j=0; 12 | map mp; 13 | while(j 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //kth largest element in an unsorted array 7 | 8 | int findKthLargest(vector& v, int k) 9 | { 10 | priority_queue,greater> q; 11 | 12 | for(auto it=v.begin();it!=v.end();it++) 13 | { 14 | q.push(*it); 15 | if(q.size()>k) 16 | q.pop(); 17 | } 18 | 19 | return q.top(); 20 | } 21 | 22 | int main() 23 | { 24 | boost; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Day 20-26/Day 22/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Flood-Fill Algorithm 7 | 8 | void floodFillUtil(vector> &img,int x,int y,int pC,int nC) 9 | { 10 | int m=img.size(); 11 | int n=img[0].size(); 12 | 13 | if(x<0 || x>=m || y<0 || y>=n) 14 | return; 15 | 16 | if(img[x][y]!=pC) 17 | return; 18 | 19 | if(img[x][y]==nC) 20 | return; 21 | 22 | img[x][y]=nC; 23 | 24 | floodFillUtil(img,x+1,y,pC,nC); 25 | floodFillUtil(img,x-1,y,pC,nC); 26 | floodFillUtil(img,x,y+1,pC,nC); 27 | floodFillUtil(img,x,y-1,pC,nC); 28 | 29 | return; 30 | } 31 | 32 | vector> floodFill(vector> &image, int sr, int sc, int newColor) 33 | { 34 | vector> res=image; 35 | floodFillUtil(res,sr,sc,res[sr][sc],newColor); 36 | 37 | return res; 38 | } 39 | 40 | int main() 41 | { 42 | boost; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Day 20-26/Day 23/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | struct Node 8 | { 9 | int val; 10 | vector neighbors; 11 | }; 12 | 13 | Node* cloneGraph(Node* node) 14 | { 15 | if(node==NULL) 16 | return node; 17 | 18 | map mp; 19 | queue q; 20 | 21 | q.push(node); 22 | Node* temp=new Node; 23 | temp->val=node->val; 24 | mp[node]=temp; 25 | 26 | while(!q.empty()) 27 | { 28 | Node* u = q.front(); 29 | q.pop(); 30 | 31 | vector v = u->neighbors; 32 | 33 | for(int i=0;ival=v[i]->val; 40 | mp[v[i]]=t; 41 | } 42 | 43 | mp[u]->neighbors.push_back(mp[v[i]]); 44 | } 45 | } 46 | 47 | return mp[node]; 48 | } 49 | 50 | int main() 51 | { 52 | boost; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Day 20-26/Day 23/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | void dfsUtil(vector g[],int v,bool visit[]) 8 | { 9 | visit[v]=true; 10 | cout< g[], int v) 22 | { 23 | bool visit[v]; 24 | memset(visit,false,sizeof(visit)); 25 | 26 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | vector bfs(vector g[],int v) 8 | { 9 | vector res; 10 | 11 | bool visit[v]; 12 | memset(visit,false,sizeof(visit)); 13 | 14 | queue gq; 15 | 16 | visit[0]=true; 17 | gq.push(0); 18 | 19 | while(!gq.empty()) 20 | { 21 | int i=gq.front(); 22 | res.push_back(i); 23 | gq.pop(); 24 | 25 | for(auto it=g[i].begin();it!=g[i].end();it++) 26 | { 27 | if(!visit[*it]) 28 | { 29 | gq.push(*it); 30 | visit[*it]=true; 31 | } 32 | } 33 | } 34 | 35 | return res; 36 | } 37 | 38 | int main() 39 | { 40 | boost; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Day 20-26/Day 23/Q4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | bool isCyclicUDUtil(vector g[],int v,bool visit[],int parent) 8 | { 9 | visit[v]=true; 10 | 11 | for(auto it=g[v].begin();it!=g[v].end();it++) 12 | { 13 | if(!visit[*it]) 14 | { 15 | if(isCyclicUDUtil(g,*it,visit,v)) 16 | return true; 17 | } 18 | else if(*it!=parent) 19 | return true; 20 | } 21 | } 22 | 23 | bool isCyclicUD(vector g[], int v) 24 | { 25 | bool visit[v]; 26 | memset(visit,false,sizeof(visit)); 27 | 28 | for(int i=0;i g[],int v,bool visit[],bool revisit[]) 41 | { 42 | if(visit[v]!=true) 43 | { 44 | visit[v]=true; 45 | revisit[v]=true; 46 | 47 | for(auto it=g[v].begin();it!=g[v].end();it++) 48 | { 49 | if(!visit[*it]) 50 | { 51 | if(isCyclicDUtil(g,*it,visit,revisit)) 52 | return true; 53 | } 54 | else if(revisit[*it]) 55 | return true; 56 | } 57 | } 58 | 59 | revisit[v]=false; 60 | return false; 61 | } 62 | 63 | bool isCyclicD(vector g[],int v) 64 | { 65 | bool visit[v],revisit[v]; 66 | memset(visit,false,sizeof(visit)); 67 | memset(revisit,false,sizeof(revisit)); 68 | 69 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | void topoSortUtil(int v,bool visit[],vector g[],stack &s) 8 | { 9 | visit[v]=true; 10 | 11 | for(auto it=g[v].begin();it!=g[v].end();it++) 12 | { 13 | if(!visit[*it]) 14 | topoSortUtil(*it,visit,g,s); 15 | } 16 | 17 | s.push(v); 18 | 19 | return; 20 | } 21 | 22 | vector topoSort(int v, vector g[]) { 23 | vector res; 24 | stack s; 25 | 26 | bool visit[v]; 27 | memset(visit,false,sizeof(visit)); 28 | 29 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Number of Islands 7 | 8 | void dfs(vector> &grid,int i,int j) 9 | { 10 | if((i<0) || (i>=grid.size()) || (j<0) || (j>=grid[0].size()) || grid[i][j]=='0') 11 | return; 12 | 13 | grid[i][j]='0'; 14 | 15 | dfs(grid,i-1,j); 16 | dfs(grid,i+1,j); 17 | dfs(grid,i,j-1); 18 | dfs(grid,i,j+1); 19 | 20 | return; 21 | } 22 | 23 | int numIslands(vector> &grid) 24 | { 25 | int m=grid.size(),n=grid[0].size(); 26 | int ct=0; 27 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | bool isBipartite(int v, vectoradj[]) 8 | { 9 | vector color(v,-1); 10 | 11 | for(int i=0;i q; 16 | color[i]=0; 17 | q.push(i); 18 | 19 | while(!q.empty()) 20 | { 21 | int node=q.front(); 22 | q.pop(); 23 | 24 | for(auto it=adj[node].begin();it!=adj[node].end();it++) 25 | { 26 | if(color[*it]==-1) 27 | { 28 | color[*it]=1-color[node]; 29 | q.push(*it); 30 | } 31 | else 32 | { 33 | if(color[*it]==color[node]) 34 | return false; 35 | } 36 | } 37 | } 38 | } 39 | } 40 | 41 | return true; 42 | } 43 | 44 | int main() 45 | { 46 | boost; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Day 20-26/Day 24/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //SCC using Kosaraju's Algo 7 | 8 | void topoSort(int node,vector adj[],vector &visit,stack &s) 9 | { 10 | visit[node]=1; 11 | 12 | for(auto it=adj[node].begin();it!=adj[node].end();it++) 13 | { 14 | if(visit[*it]==0) 15 | topoSort(*it,adj,visit,s); 16 | } 17 | s.push(node); 18 | 19 | return; 20 | } 21 | 22 | void dfs(int node,vector adj[],vector &visit,vector &t) 23 | { 24 | t.push_back(node); 25 | visit[node]=1; 26 | 27 | for(auto it=adj[node].begin();it!=adj[node].end();it++) 28 | { 29 | if(visit[*it]==0) 30 | dfs(*it,adj,visit,t); 31 | } 32 | 33 | return; 34 | } 35 | 36 | void kosarajuSCC(int v,vector adj[]) 37 | { 38 | stack s; 39 | vector visit(v,0); 40 | 41 | for(int i=0;i transpose[v]; 48 | for(int i=0;i> res; 56 | while(!s.empty()) 57 | { 58 | int x=s.top(); 59 | s.pop(); 60 | 61 | if(visit[x]==0) 62 | { 63 | vector t; 64 | dfs(x,transpose,visit,t); 65 | res.push_back(t); 66 | } 67 | } 68 | 69 | return; 70 | } 71 | 72 | int main() 73 | { 74 | boost; 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Day 20-26/Day 24/Q2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Dijkstra's Algorithm 7 | 8 | void dijkstraAlgo(int v,vector> adj[],int s) 9 | { 10 | vector visit(v,0); 11 | priority_queue,vector>,greater>> minh; 12 | 13 | vector dist(v,INT_MAX); 14 | dist[s]=0; 15 | minh.push({0,s}); 16 | 17 | while(!minh.empty()) 18 | { 19 | int node=minh.top().second; 20 | minh.pop(); 21 | 22 | for(auto it=adj[node].begin();it!=adj[node].end();it++) 23 | { 24 | int nextNode=(*it).first; 25 | int nextDist=(*it).second; 26 | 27 | if(dist[nextNode]>dist[node]+nextDist) 28 | { 29 | dist[nextNode]=dist[node]+nextDist; 30 | minh.push({dist[nextNode],nextNode}); 31 | } 32 | } 33 | } 34 | 35 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Bellman Ford Algorithm 7 | 8 | bool negativeCycle(int n,vector> edges) 9 | { 10 | vector dist(n,INT_MAX); 11 | dist[0]=0; 12 | 13 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Floyd Warshall Algorithm 7 | 8 | void shortest_distance(vector> &mat) 9 | { 10 | for(int v=0;v 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //MST using Prim's Algorithm 7 | 8 | void primMST(int v,vector> adj[]) 9 | { 10 | vector parent(v,-1); 11 | vector key(v,INT_MAX); 12 | vector mst(v,false); 13 | 14 | key[0]=0; 15 | parent[0]=-1; 16 | for(int i=1;iwgt) 36 | { 37 | parent[node]=ind; 38 | key[node]=wgt; 39 | } 40 | } 41 | } 42 | 43 | return; 44 | } 45 | 46 | void primMSTOpti(int v,vector> adj[]) 47 | { 48 | vector parent(v,-1); 49 | vector key(v,INT_MAX); 50 | vector mst(v,false); 51 | 52 | priority_queue,vector>,greater>> minh; 53 | key[0]=0; 54 | parent[0]=-1; 55 | minh.push({0,0}); 56 | 57 | for(int i=1;iwgt) 70 | { 71 | parent[node]=ind; 72 | key[node]=wgt; 73 | minh.push({key[node],node}); 74 | } 75 | } 76 | } 77 | 78 | return; 79 | } 80 | 81 | int main() 82 | { 83 | boost; 84 | return 0; 85 | } -------------------------------------------------------------------------------- /Day 20-26/Day 24/Q6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //MST using Kruskal's Algo 7 | 8 | struct nodeMST 9 | { 10 | int u; 11 | int v; 12 | int wt; 13 | 14 | nodeMST(int first,int second,int weight) 15 | { 16 | u=first; 17 | v=second; 18 | wt=weight; 19 | } 20 | }; 21 | 22 | static bool comp(nodeMST a,nodeMST b) 23 | { 24 | return a.wt &parent) 28 | { 29 | if(node==parent[node]) 30 | return node; 31 | 32 | return parent[node]=findParent(parent[node],parent); //Path Compression 33 | } 34 | 35 | void unionByRank(int u,int v,vector &parent,vector &rank) 36 | { 37 | u=findParent(u,parent),v=findParent(v,parent); 38 | 39 | //Union by Rank 40 | if(rank[u]> adj[]) 54 | { 55 | vector edges; 56 | for(int i=0;i parent(v); 65 | vector rank(v,0); 66 | 67 | for(int i=0;i> mst; 71 | for(auto it=edges.begin();it!=edges.end();it++) 72 | { 73 | if(findParent((*it).u,parent)!=findParent((*it).v,parent)) 74 | { 75 | mst.push_back({(*it).u,(*it).v}); 76 | unionByRank((*it).u,(*it).v,parent,rank); 77 | } 78 | } 79 | 80 | return; 81 | } 82 | 83 | int main() 84 | { 85 | boost; 86 | return 0; 87 | } -------------------------------------------------------------------------------- /Day 20-26/Day 25/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define mod 1000000007 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int n; 13 | cin>>n; 14 | ll a[n]; 15 | for(int i=0;i>a[i]; 17 | 18 | ll minVal=a[0],maxVal=a[0],prod=a[0]; 19 | 20 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int a[n]; 13 | for(int i=0;i>a[i]; 15 | 16 | int lis[n]; 17 | lis[0]=1; 18 | 19 | for(int i=1;ia[j] && lis[i] 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Longest Common Subsequence Bottom-Up DP 7 | int main() 8 | { 9 | boost; 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int m,n; 15 | cin>>m>>n; 16 | string a,b; 17 | cin>>a>>b; 18 | 19 | int DP[m+2][n+2]; 20 | 21 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | boost; 10 | 11 | int t[5][8]; 12 | int wt[] = {1,3,4,5}; 13 | int val[] = {1,4,5,7}; 14 | int W=7,n=4; 15 | int x; 16 | 17 | //Bottom-Up approach 18 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int m,n; 11 | cin>>m>>n; 12 | string a,b; 13 | cin>>a>>b; 14 | 15 | int DP[m+2][n+2]; 16 | 17 | for(int i=0;i 2 | #define lli long long int 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | lli a[n]; 14 | for(int i=0;i>a[i]; 16 | 17 | lli sum[n]; 18 | sum[0]=a[0]; 19 | 20 | for(int i=1;ia[j] && sum[i] 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | int DP[102][102]; 7 | 8 | int solveMCM(int a[],int i,int j) 9 | { 10 | if(i>=j) 11 | return 0; 12 | 13 | if(DP[i][j]!=-1) 14 | return DP[i][j]; 15 | 16 | int minn=INT_MAX; 17 | 18 | for(int k=i;k>t; 32 | while(t--) 33 | { 34 | memset(DP,-1,sizeof(DP)); 35 | int n; 36 | cin>>n; 37 | int a[n]; 38 | 39 | for(int i=0;i>a[i]; 41 | 42 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Maximum Path Sum in a Matrix 7 | 8 | int main() 9 | { 10 | boost; 11 | int m,n; 12 | cin>>m>>n; 13 | 14 | int mat[m][n]; 15 | for(int i=0;i>mat[i][j]; 19 | } 20 | 21 | for(int i=1;i0 && j0) 28 | mat[i][j]+=max(mat[i-1][j],mat[i-1][j-1]); 29 | else if(j 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Coin change (Max no. of coins) 7 | 8 | int main() 9 | { 10 | boost; 11 | int n,sum; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 16 | cin>>sum; 17 | 18 | int t[n+2][sum+2]; 19 | 20 | //Bottom-Up approach 21 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Subset sum 7 | 8 | int main() 9 | { 10 | boost; 11 | int n,sum; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 17 | } 18 | cin>>sum; 19 | bool DP[n+2][sum+2]; 20 | for(int i=1;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Rod cutting 7 | 8 | int main() 9 | { 10 | boost; 11 | int n; 12 | cin>>n; 13 | int len[n],prc[n]; 14 | for(int i=0;i>len[i]; 16 | for(int i=0;i>prc[i]; 18 | 19 | int t[n+2][n+2]; 20 | int x; 21 | 22 | //Bottom-Up approach 23 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Egg Dropping 7 | 8 | int DP[12][52]; 9 | int solveEgg(int e,int f) 10 | { 11 | if(e==1) 12 | return f; 13 | 14 | if(f==0||f==1) 15 | return f; 16 | 17 | if(DP[e][f]!=-1) 18 | return DP[e][f]; 19 | 20 | int minn=INT_MAX,eb,edb; 21 | 22 | for(int k=1;k<=f;k++) 23 | { 24 | if(DP[e-1][k-1]!=-1) 25 | eb=DP[e-1][k-1]; 26 | else 27 | { 28 | eb = solveEgg(e-1,k-1); 29 | DP[e-1][k-1]=eb; 30 | } 31 | 32 | if(DP[e][f-k]!=-1) 33 | edb=DP[e][f-k]; 34 | else 35 | { 36 | edb = solveEgg(e,f-k); 37 | DP[e][f-k]=edb; 38 | } 39 | 40 | int temp=1+max(eb,edb); 41 | 42 | minn=min(temp,minn); 43 | } 44 | 45 | return DP[e][f]=minn; 46 | } 47 | 48 | int main() 49 | { 50 | int t; 51 | cin>>t; 52 | while(t--) 53 | { 54 | int n,f; 55 | cin>>n>>f; 56 | 57 | memset(DP,-1,sizeof(DP)); 58 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Word Break 7 | int wordBreak(string s, vector &b) 8 | { 9 | set d; 10 | for(int i=0;i 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | //Palindrome Partitioning (Using MCM) 7 | bool isPalin(string s,int i,int j) 8 | { 9 | while(i<=j) 10 | { 11 | if(s[i]!=s[j]) 12 | return false; 13 | 14 | i++; 15 | j--; 16 | } 17 | 18 | return true; 19 | } 20 | 21 | int DP[1002][1002]; 22 | 23 | int solvePal(string s,int i,int j) 24 | { 25 | if(i>=j) 26 | return 0; 27 | 28 | if(isPalin(s,i,j)) 29 | return 0; 30 | 31 | if(DP[i][j]!=-1) 32 | return DP[i][j]; 33 | 34 | int ans=INT_MAX; 35 | 36 | int left,right,temp; 37 | for(int k=i;k>t; 62 | while(t--) 63 | { 64 | memset(DP,-1,sizeof(DP)); 65 | string s; 66 | cin>>s; 67 | cout< 2 | #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3 | #define ll long long 4 | #define deb(x) cout << #x << "=" << x << endl 5 | using namespace std; 6 | int main() 7 | { 8 | boost; 9 | return 0; 10 | } --------------------------------------------------------------------------------