├── README.md ├── ds ├── arrays │ ├── missing-number-in-array.cpp │ ├── sort-array-with-0-and-1.cpp │ ├── reverse-in-group-of-k.cpp │ ├── print-maximum-number-of-a-using-given-four-keys.cpp │ ├── shuffle-array-of-elements-Fisher-Yates-algo.cpp │ ├── count-subarray-with-given-sum-with-negative.cpp │ ├── kadane-alogrithm.cpp │ ├── sort-array-with-0-1-2.cpp │ ├── longest-subarray-with-given-sum.cpp │ ├── rearrange-array-alternately.cpp │ ├── leaders-in-an-array.cpp │ ├── merge-two-sorted-arrays.cpp │ ├── maximum-product-subarray.cpp │ ├── subarray-with-given-sum.cpp │ ├── trapping-rain-water-O(N)-space.cpp │ ├── min-number-of-jumps-O(1)-space.cpp │ ├── replace-0-to-get-longest-1.cpp │ ├── longest-palindromic-subsequence-O(1)space.cpp │ ├── longest-subarray-with-elements-in-consecutive-order-when-sorted.cpp │ ├── equilibrium-point.cpp │ ├── trapping-rain-water-O(1)-space.cpp │ ├── minimum-platforms-on-a-station.cpp │ ├── inversion-count.cpp │ ├── count-triplets-distinct-array.cpp │ ├── the-celebrity-problem-with-2-pointer.cpp │ ├── maximum-difference-between-sum-of-2-non-overlapping-subarrays.cpp │ ├── minimum-possible-sum-of-two-numbers-formed-from-digits-of-the-array.cpp │ ├── print-all-quadruplets-with-given-sum.cpp │ ├── number-follow-an-I-D-pattern.cpp │ └── given-zero-flips-allowed-find-max-consec-zeros.cpp ├── heap │ ├── heap-sort.cpp │ ├── sorting-a-k-sorted-array.cpp │ ├── kth-largest-number-in-stream.cpp │ ├── check-if-heap.cpp │ ├── basic-implementation.cpp │ ├── median-of-running-stream.cpp │ ├── huffman-encoding.cpp │ └── merge-k-sorted-linked-list.cpp ├── matrix │ ├── count-negative-element-in-row-wise-column-wise-sorted-matrix.cpp │ ├── find-element-in-row-wise-column-wise.cpp │ ├── spiral-order-traversal.cpp │ ├── create-spiral-matrix-from-array.cpp │ ├── largest-sum-submatrix-in-a-matrix.cpp │ └── shift-matrix-elements-by-1-spiral-order.cpp ├── strings │ ├── remove-a-bc-from-string-in-one-parse-in-place.cpp │ ├── check-anagrams.cpp │ ├── k-anagrams.cpp │ ├── reverse-words-in-a-sentence.cpp │ ├── all-permutations-of-a-string.cpp │ ├── isomorphic-strings.cpp │ ├── longest-common-prefix.cpp │ ├── longest-substring-with-no-repeating-characters.cpp │ ├── longest-k-unique-charecters-substring.cpp │ ├── check-has-repeated-subsequence.cpp │ ├── find-largest-word-in-dictionary.cpp │ ├── kmp-substring-search.cpp │ └── longest-palindromic-substring-manachers-algo.cpp ├── stacks-and-queues │ ├── longest-balanced-bracket-substring.cpp │ ├── next-greater-number.cpp │ ├── card-rotation.cpp │ ├── check-duplicate-parenthesis.cpp │ ├── number-follow-a-pattern.cpp │ ├── maximum-of-all-subarrays-of-size-k.cpp │ ├── merging-intervals.cpp │ ├── parathesis-checker.cpp │ ├── stack-using-2-queues.cpp │ ├── the-celebrity-problem.cpp │ ├── queue-using-2-stacks.cpp │ └── stack-with-min-O(1)-time.cpp ├── graph │ ├── min-number-of-jumps-bfs.cpp │ ├── depth-first-search.cpp │ ├── topological-sort.cpp │ ├── breadth-first-search.cpp │ ├── topological-sort-khan-algo.cpp │ ├── shortest-path-from-1-to-n-using-dijkstra.cpp │ ├── arrival-departure-time-dfs-order.cpp │ ├── cycle-detection-directed-graph.cpp │ ├── cycle-detection-undirected-graph.cpp │ └── given-sorted-dictionary-find-precedence-characters.cpp ├── trees │ ├── check-if-given-preorder-traversal-represents-BST.cpp │ ├── check-whether-binary-tree-full-binary-tree.cpp │ ├── bst-nodes-in-given-range.cpp │ ├── minimum-depth-binary-tree.cpp │ ├── max-sum-path-binary-tree.cpp │ ├── root-to-leaf-paths-sum.cpp │ ├── bottom-view.cpp │ └── check-if-bst.cpp ├── trie │ └── basic-implementation.cpp └── linked-list │ ├── sort-list-of-0-1-2.cpp │ └── single-linked-list-all.cpp └── algo ├── dynamic-programming ├── find-optimum-operation.cpp ├── number-of-ways-to-cover-dist-in-1-2-3-step.cpp ├── house-robber.cpp ├── coin-change-problem.cpp ├── subset-divisible-by-m.cpp ├── unbounded-knapsack.cpp ├── how-to-print-maximum-number-of-a-using-given-four-keys.cpp ├── longest-common-subsequence.cpp ├── longest-repeated-subsequence.cpp ├── count-increasing-subsequences.cpp ├── longest-increase-subsequence.cpp ├── min-number-of-coins-O(N)-space.cpp ├── subsequence-with-max-sum.cpp ├── shortest-common-supersequence.cpp ├── total-decoding-messages.cpp ├── longest-common-substring.cpp ├── distinct-transformations.cpp ├── optimal-strategy-in-a-game.cpp ├── egg-dropping-puzzle.cpp ├── edit-distance-dp.cpp ├── find-size-largest-square-sub-matrix-1s-present-given-binary-matrix.cpp ├── min-cost-path.cpp ├── minimum-insertions-to-make-it-palindrome.cpp ├── longest-possible-chunked-palindrome.cpp ├── longest-alternating-sequence.cpp ├── 0-1-Knapsack.cpp ├── longest-palindromic-subsequence.cpp ├── longest-palindromic-substring.cpp ├── count-of-strings-that-can-be-formed-using-a-b-and-c-under-given-constraints.cpp ├── paper-cut-min-squares.cpp ├── nth-super-ugly-number.cpp ├── printing-longest-common-substring.cpp ├── check-k-palindrome.cpp ├── optimal-cost-to-construct-BST.cpp ├── longest-bitonic-subsequence.cpp ├── minimum-points-to-reach-destination.cpp ├── minimum-cost-adjustment-in-an-array.cpp ├── maximum-sum-rectangle-in-a-matrix.cpp ├── min-number-of-coins-O(N^2)-space.cpp ├── ugly-number.cpp ├── path-in-matrix-with-fixed-cost.cpp ├── buy-sell-stock-k-times-at-most.cpp ├── word-break-problem.cpp ├── partition-with-min-diff.cpp ├── probability-alive-after-taking-n-steps-island.cpp ├── optimal-cost-to-construct-BST-bottom-up.cpp ├── printing-longest-common-subsequence.cpp ├── matrix-chain-multiplication.cpp ├── minimum-word-breaks.cpp ├── painters-partion-problem.cpp ├── max-length-chain-dp.cpp ├── subset-sum-problem.cpp ├── count-path-on-grid-with-given-cost.cpp ├── longest-sequence-formed-adjacent-numbers-matrix.cpp ├── count-longest-increasing-subsequences.cpp ├── dice-rolling.cpp ├── cutting-binary-strings.cpp ├── longest-arithmetic-sequence.cpp ├── interleaving-of-2-strings.cpp ├── palindrome-substring-min-cuts.cpp ├── divide-in-n-continous-group-such-that-difference-of-max-and-min-is-minimum.cpp ├── count-total-possible-combinations-n-digit-numbers-mobile-keypad.cpp └── boolean-parenthesis-to-make-expression-true.cpp ├── recursion ├── find-optimal-operation-brute.cpp ├── longest-common-subsequence-brute.cpp └── longest-common-substring-brute.cpp ├── divide-conquer ├── modulo-exponential.cpp ├── quick-sort-random-pivot.cpp ├── merge-sort.cpp ├── quick-select-k-largest-number.cpp └── inversion-count.cpp ├── bit-manupulation └── sum-of-bit-difference-among-all-pairs.cpp ├── hashing ├── triplet-with-zero-sum.cpp └── pair-with-given-sum.cpp ├── binary-search ├── find-frequency-of-elements-in-sorted-array.cpp ├── find-the-element-that-appears-once-in-sorted-array.cpp ├── kth-2-sorted-array.cpp ├── search-in-rotated-array.cpp ├── median-of-2-sorted-array-diff-size.cpp └── painters-partion-problem.cpp ├── greedy-programming ├── max-length-chain-reorderin-allowed-greedy.cpp ├── activity-selection-problem.cpp └── N-meetings-in-one-room.cpp └── backtracking ├── N-Queens-Problem.cpp ├── rat-in-a-maze.cpp ├── solve-sudoku.cpp └── word-boggle.cpp /README.md: -------------------------------------------------------------------------------- 1 | # DSA-Practice 2 | Practice Folder for preparing Data Structure and Algorithm questions. These are the questions I practiced during my preparation for my Google Interview. 3 | -------------------------------------------------------------------------------- /ds/arrays/missing-number-in-array.cpp: -------------------------------------------------------------------------------- 1 | #include 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 miss = 0; 13 | for(int i=1;i>x; 17 | miss^=(x^i); 18 | } 19 | miss^=n; 20 | cout< 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 dp[n+1]={0}; 13 | for(int i=1;i<=n;i++) 14 | { 15 | if(i%2 == 0) 16 | dp[i]=min(dp[i/2],dp[i-1])+1; 17 | else 18 | dp[i]=dp[i-1]+1; 19 | } 20 | cout< 2 | using namespace std; 3 | 4 | int solve(int n) 5 | { 6 | int a[n+1]; 7 | a[0]=1; 8 | a[1]=1; 9 | a[2]=2; 10 | for(int i=3;i<=n;i++) 11 | a[i]=a[i-1]+a[i-2]+a[i-3]; 12 | return a[n]; 13 | } 14 | 15 | int main() 16 | { 17 | int tc; 18 | cin>>tc; 19 | while(tc--) 20 | { 21 | int n; 22 | cin>>n; 23 | cout< 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]; 14 | int zero=0; 15 | //This is like Partition in QuickSort 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | int solve(int n) 5 | { 6 | if(n==0) 7 | return 0; 8 | else 9 | { 10 | if(n & 1) 11 | return solve(n-1)+1; 12 | else 13 | return min(solve(n-1),solve(n/2))+1; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | int n; 24 | cin>>n; 25 | int ans = solve(n); 26 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n,k; 10 | cin>>n>>k; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 13 | 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 n; 11 | cin>>n; 12 | long a[n]; 13 | for(int i=0;i>a[i]; 15 | long dp[n]; 16 | dp[0] = a[0]; 17 | dp[1] = max(a[0],a[1]); 18 | for(int i=2;i 2 | #include 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 | int a[n]; 14 | priority_queue,greater> minHeap; 15 | for(int i=0;i>a[i]; 18 | minHeap.push(a[i]); 19 | } 20 | int i=0; 21 | while(!minHeap.empty()) 22 | { 23 | a[i++]=minHeap.top(); 24 | minHeap.pop(); 25 | } 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /algo/dynamic-programming/coin-change-problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n,val; 10 | cin>>n; 11 | int coins[n]; 12 | for(int i=0;i>coins[i]; 13 | cin>>val; 14 | int sol[val+1]={0}; 15 | sol[0]=1; 16 | for(int j=0;j 2 | using namespace std; 3 | 4 | int power(int x,int y,int M) 5 | { 6 | if(y==0) 7 | return 1; 8 | else if(y==1) 9 | return x%M; 10 | else if(y & 1) 11 | return (x%M*power(x,y-1,M)%M)%M; 12 | else 13 | { 14 | int a = power(x,y/2,M)%M; 15 | return (a*a)%M; 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | int t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | int x,y,M; 26 | cin>>x>>y>>M; 27 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,m; 11 | cin>>n>>m; 12 | int a[n]; 13 | for(int i=0;i>a[i]; 15 | bool dp[n][m]; 16 | memset(dp,0,sizeof(dp)); 17 | dp[0][a[0]%m]=true; 18 | 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,W; 11 | cin>>n>>W; 12 | int val[n],wt[n]; 13 | for(int i=0;i>val[i]; 14 | for(int i=0;i>wt[i]; 15 | 16 | int knapsack[W+1]={0}; 17 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int dp[76]; 6 | for(int i=0;i<=6;i++) 7 | dp[i]=i; 8 | for(int i=7;i<=75;i++) 9 | { 10 | dp[i] = 0; 11 | for(int last_cut_copy_paste = i-3 ; last_cut_copy_paste>=1 ; last_cut_copy_paste--) 12 | dp[i] = max(dp[i],(i - last_cut_copy_paste - 1)*dp[last_cut_copy_paste]); 13 | } 14 | int t; 15 | cin>>t; 16 | while(t--) 17 | { 18 | int n; 19 | cin>>n; 20 | cout< 2 | using namespace std; 3 | 4 | int lcs(string a,string b,int n,int m) 5 | { 6 | if(n==0||m==0) 7 | return 0; 8 | else 9 | { 10 | if(a[n-1]==b[m-1]) 11 | return lcs(a,b,n-1,m-1)+1; 12 | else 13 | return max(lcs(a,b,n-1,m)+lcs(a,b,n,m-1)); 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | int n,m; 24 | cin>>n>>m; 25 | string a,b; 26 | cin>>a>>b; 27 | int ans = lcs(a,b,n,m); 28 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | int a[n]; 10 | srand(time(0)); 11 | for(int i=0;i>a[i]; 12 | 13 | cout<<"Orignal Array : "; 14 | for(int i=0;i=0;i--) 18 | { 19 | int random = rand()%(i+1); 20 | swap(a[i],a[random]); 21 | } 22 | 23 | cout<<"Shuffled Array : "; 24 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,k; 12 | cin>>n>>k; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 15 | priority_queue,greater> minHeap; 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int dp[76]; 6 | for(int i=0;i<=6;i++) 7 | dp[i]=i; 8 | for(int i=7;i<=75;i++) 9 | { 10 | dp[i] = 0; 11 | for(int last_cut_copy_paste = i-3 ; last_cut_copy_paste>=1 ; last_cut_copy_paste--) 12 | dp[i] = max(dp[i],(i - last_cut_copy_paste - 1)*dp[last_cut_copy_paste]); 13 | } 14 | int t; 15 | cin>>t; 16 | while(t--) 17 | { 18 | int n; 19 | cin>>n; 20 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | long long x; 13 | cin>>n>>x; 14 | int ans = 0,sum = 0; 15 | unordered_map map; 16 | while(n--) 17 | { 18 | int v; 19 | cin>>v; 20 | sum+=v; 21 | if(map.find(sum-x)!=map.end()) 22 | ans+=map[sum-x]; 23 | if(sum==x) 24 | ans++; 25 | map[sum]++; 26 | } 27 | cout< 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 | int sum_till_here = a[0]; 16 | int max_sum = a[0]; 17 | for(int i=1;imax_sum?sum_till_here:max_sum; 25 | } 26 | cout< 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 | int mat[m][n]; 13 | for(int i=0;i>mat[i][j]; 16 | int i = 0 , j = n-1; 17 | int ans = 0; 18 | while(i=0) 19 | { 20 | if(mat[i][j]<0) 21 | { 22 | ans+=j+1; 23 | i++; 24 | } 25 | else 26 | j--; 27 | } 28 | cout< 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]; 14 | int zero=0,i=0,two=n-1; 15 | //This is like Partition in QuickSort 16 | while(i<=two) 17 | { 18 | if(a[i]==0) 19 | swap(a[i++],a[zero++]); 20 | else if(a[i]==2) 21 | swap(a[i],a[two--]); 22 | else 23 | i++; 24 | } 25 | 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 n; 11 | cin>>n; 12 | int a[n]; 13 | for(int i=0;i>a[i]; 14 | int sum; 15 | cin>>sum; 16 | unordered_map m; 17 | int s=0; 18 | int ans=-1; 19 | m[0]=-1; 20 | 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 n,m; 11 | cin>>n>>m; 12 | string a,b; 13 | cin>>a>>b; 14 | int lcs[n+1][m+1]; 15 | for(int i=0;i<=n;i++) 16 | { 17 | for(int j=0;j<=m;j++) 18 | { 19 | if(i==0||j==0) 20 | lcs[i][j]=0; 21 | else if(a[i-1]==b[j-1]) 22 | lcs[i][j]=lcs[i-1][j-1]+1; 23 | else 24 | lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]); 25 | } 26 | } 27 | cout< 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 | string x; 13 | cin>>x; 14 | int ans[n+1][n+1]; 15 | for(int i=0;i<=n;i++) 16 | { 17 | for(int j=0;j<=n;j++) 18 | { 19 | if(i==0 || j==0) 20 | ans[i][j]=0; 21 | else if(x[i-1]==x[j-1] && i!=j) 22 | ans[i][j]=ans[i-1][j-1]+1; 23 | else 24 | ans[i][j]=max(ans[i-1][j],ans[i][j-1]); 25 | } 26 | } 27 | cout< 2 | using namespace std; 3 | 4 | int lcs(string a,string b,int n,int m,int count) 5 | { 6 | if(n==0||m==0) 7 | return count; 8 | else 9 | { 10 | if(a[n-1]==b[m-1]) 11 | count = lcs(a,b,n-1,m-1,count+1); 12 | else 13 | count = max(count,max(lcs(a,b,n-1,m,0)+lcs(a,b,n,m-1,0))); 14 | return count; 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | int t; 21 | cin>>t; 22 | while(t--) 23 | { 24 | int n,m; 25 | cin>>n>>m; 26 | string a,b; 27 | cin>>a>>b; 28 | int ans = lcs(a,b,n,m,0); 29 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | string x; 10 | cin>>x; 11 | int n = x.size(); 12 | int j = 0; 13 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 14 | long long dp[n]; 15 | dp[0] = 1; 16 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | long long a[n],max=-1e7; 12 | for(int i=0;i>a[i]; 14 | max = a[n-1]; 15 | max+=1; 16 | int s=0,l=n-1; 17 | 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 n; 11 | cin>>n; 12 | int a[n]; 13 | int lis[n]; 14 | for(int i=0;i>a[i]; 17 | lis[i]=1; 18 | } 19 | for(int i=1;ia[j] && lis[i] 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int types = 10; 13 | int val[] ={1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 14 | int sol[n+1]={0}; 15 | sol[0]=0; 16 | for(int i=1;i<=n;i++) 17 | { 18 | sol[i]= INT_MAX; 19 | for(int j=0;j 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 | int sol[n]; 14 | for(int i=0;i>a[i]; 17 | sol[i]=a[i]; 18 | } 19 | for(int i=1;ia[j] && sol[i] 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string x; 11 | cin>>x; 12 | stack s; 13 | s.push(-1); 14 | int ans = 0; 15 | for(int i=0;x[i];i++) 16 | { 17 | if(x[i]=='(') 18 | s.push(i); 19 | else 20 | { 21 | s.pop(); 22 | if(!s.empty()) 23 | ans = max(ans,i - s.top()); 24 | else 25 | s.push(i); 26 | } 27 | } 28 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 14 | 15 | int ans = 0; 16 | for(int i=0;i<32;i++) 17 | { 18 | int count = 0; 19 | for(int j=0;j 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string x,y; 11 | cin>>x>>y; 12 | int n=x.size(); 13 | int m=y.size(); 14 | int lcs[n+1][m+1]; 15 | for(int i=0;i<=n;i++) 16 | { 17 | for(int j=0;j<=m;j++) 18 | { 19 | if(i==0||j==0) 20 | lcs[i][j]=0; 21 | else if(x[i-1]==y[j-1]) 22 | lcs[i][j]=lcs[i-1][j-1]+1; 23 | else 24 | lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]); 25 | } 26 | } 27 | cout<<(x.size()+y.size()-lcs[n][m])<<"\n"; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /ds/arrays/leaders-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int arr[n]; 12 | bool vis[n]={false}; 13 | for(int i=0;i>arr[i]; 14 | int max=-1; 15 | for(int i=n-1;i>=0;i--) 16 | { 17 | if(arr[i]>=max) 18 | { 19 | max=arr[i]; 20 | vis[i]=true; 21 | } 22 | } 23 | 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 n,m; 11 | cin>>n>>m; 12 | int a[n],b[m],c[n+m]; 13 | for(int i=0;i>a[i]; 14 | for(int i=0;i>b[i]; 15 | int p1 = 0,p2 = 0, i = 0; 16 | while(p1 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string x; 11 | int n; 12 | cin>>n; 13 | cin>>x; 14 | int dp[n+1]={0}; 15 | dp[0]=1; 16 | dp[1]=1; 17 | if(x[0]=='0') 18 | cout<<"0\n"; 19 | else 20 | { 21 | for(int i=2;i<=n;i++) 22 | { 23 | if(x[i-1]>'0') 24 | dp[i]+=dp[i-1]; 25 | if(x[i-2]=='1'||(x[i-2]=='2' && x[i-1]<='6')) 26 | dp[i]+=dp[i-2]; 27 | } 28 | cout< 2 | using namespace std; 3 | 4 | bool areAnagrams(string str1, string str2) 5 | { 6 | if(str1.size()!=str2.size()) 7 | return false; 8 | int s1[26]={0},s2[26]={0}; 9 | for(int i=0;i>t; 23 | while(t--) 24 | { 25 | string str1,str2; 26 | cin>>str1>>str2; 27 | if (areAnagrams(str1, str2)) 28 | cout<<"YES\n"; 29 | else 30 | cout<<"NO\n"; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /algo/dynamic-programming/longest-common-substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,m; 11 | cin>>n>>m; 12 | string a,b; 13 | cin>>a>>b; 14 | int lcs[n+1][m+1]; 15 | int ans = 0; 16 | for(int i=0;i<=n;i++) 17 | { 18 | for(int j=0;j<=m;j++) 19 | { 20 | if(i==0||j==0) 21 | lcs[i][j]=0; 22 | else if(a[i-1]==b[j-1]) 23 | { 24 | lcs[i][j]=lcs[i-1][j-1]+1; 25 | ans = max(ans,lcs[i][j]); 26 | } 27 | else 28 | lcs[i][j]=0; 29 | } 30 | } 31 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | priority_queue,greater> pq; 12 | int n,k; 13 | cin>>n>>k; 14 | while(n--) 15 | { 16 | int x; 17 | cin>>x; 18 | if(pq.size()pq.top()) 23 | { 24 | pq.pop(); 25 | pq.push(x); 26 | } 27 | } 28 | 29 | if(pq.size()==k) 30 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | string x,y; 10 | cin>>x>>y; 11 | int n = x.size(),m = y.size(); 12 | int dp[n+1][m+1]; 13 | dp[0][0] = 1; 14 | for(int i=1;i<=n;i++) dp[i][0]=1; 15 | for(int i=1;i<=m;i++) dp[0][i]=0; 16 | for(int i=1;i<=n;i++) 17 | { 18 | for(int j=1;j<=m;j++) 19 | { 20 | if(x[i-1]==y[j-1]) 21 | dp[i][j] = dp[i-1][j-1] + dp[i-1][j]; 22 | else 23 | dp[i][j] = dp[i-1][j]; 24 | } 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int m,n,key; 11 | cin>>m>>n>>key; 12 | int mat[m][n]; 13 | for(int i=0;i>mat[i][j]; 16 | int i = 0 , j = n-1; 17 | bool flag = false; 18 | while(i=0) 19 | { 20 | if(mat[i][j]key) 23 | j--; 24 | else 25 | { 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 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 | int maxim = a[0]; 16 | int curr_min = a[0],curr_max = a[0]; 17 | 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 | int dp[n][n]; 16 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int k,n; 12 | cin>>k>>n; 13 | int dp[n+1][k+1]; 14 | for(int i=0;i<=k;i++) 15 | { 16 | dp[0][i]=0; 17 | dp[1][i]=1; 18 | } 19 | for(int i=1;i<=n;i++) 20 | dp[i][1]=i; 21 | for(int i=2;i<=n;i++) 22 | { 23 | for(int j=2;j<=k;j++) 24 | { 25 | int ans = 1000000; 26 | for(int z=1;z<=i;z++) 27 | ans = min(ans,max(dp[z-1][j-1],dp[i-z][j])); 28 | dp[i][j]=1+ans; 29 | } 30 | } 31 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | long long x; 12 | cin>>n>>x; 13 | long long a[n],curr=0; 14 | for(int i=0;i>a[i]; 15 | int start=0,end=-1; 16 | while(startx){ 24 | curr-=a[start]; 25 | start++; 26 | } 27 | else{ 28 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,m; 11 | cin>>n>>m; 12 | string x,y; 13 | cin>>x>>y; 14 | int dist[n+1][m+1]; 15 | for(int i=0;i<=n;i++) 16 | { 17 | for(int j=0;j<=m;j++) 18 | { 19 | if(i==0) 20 | dist[i][j]=j; 21 | else if(j==0) 22 | dist[i][j]=i; 23 | else if(x[i-1]==y[j-1]) 24 | dist[i][j]=dist[i-1][j-1]; 25 | else 26 | { 27 | dist[i][j]=min(min(dist[i-1][j],dist[i][j-1]),dist[i-1][j-1]) + 1; 28 | } 29 | } 30 | } 31 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 14 | int max_l[n],max_r[n]; 15 | max_l[0]=0; 16 | max_r[n-1]=0; 17 | for(int i=1;i=0;i--) 20 | max_r[i]=max(a[i+1],max_r[i+1]); 21 | int val = 0; 22 | for(int i=0;i 2 | #include 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 | long long a[n]; 14 | for(int i=0;i>a[i]; 15 | int nge[n]; 16 | stack st; 17 | for(int i=0;ia[st.top()]) 20 | { 21 | nge[st.top()]=i; 22 | st.pop(); 23 | } 24 | st.push(i); 25 | } 26 | while(!st.empty()) 27 | { 28 | nge[st.top()]=-1; 29 | st.pop(); 30 | } 31 | 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 | int a[m][n]; 13 | for(int i=0;i>a[i][j]; 16 | int dp[n][m]; 17 | int ans = 1; 18 | for(int i=0;i0 && j>0 && a[i][j]==1) 24 | dp[i][j]=1 + min(min(dp[i][j-1],dp[i-1][j]),dp[i-1][j-1])+1; 25 | ans = max(ans,dp[i][j]); 26 | } 27 | } 28 | cout< 2 | #include 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 | int a[n]; 14 | unordered_map map; 15 | for(int i=0;i>a[i]; 16 | for(int i=0;ic) 29 | ans+=(map[req_sum]-c); 30 | } 31 | } 32 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 13 | int jump = 0; 14 | int step = 1; 15 | int maxReachable = 0; 16 | for(int i=0;i=maxReachable) 24 | break; 25 | step = maxReachable - i; 26 | } 27 | } 28 | if(maxReachable>=n-1) 29 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | queue q; 14 | vector a(n+1); 15 | for(int i=1;i<=n;i++) 16 | { 17 | a[i] = i; 18 | q.push(i); 19 | } 20 | int i = 1; 21 | while(!q.empty()) 22 | { 23 | for(int j=1;j<=i;j++) 24 | { 25 | int x = q.front(); 26 | q.pop(); 27 | q.push(x); 28 | } 29 | a[q.front()] = i++; 30 | q.pop(); 31 | } 32 | for(i=1;i<=n;i++) 33 | cout< 2 | using namespace std; 3 | 4 | int solve(int a[],int n) 5 | { 6 | int last_zero = -1; 7 | int curr_zero = 0; 8 | int zero_ans = 0; 9 | int ans = -1; 10 | for(int i = 0;izero_ans) 22 | { 23 | zero_ans=curr_zero; 24 | ans = last_zero; 25 | } 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | int n; 36 | cin>>n; 37 | int a[n]; 38 | for(int i=0;i>a[i]; 39 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool checkDuplicate(string x) 6 | { 7 | stack st; 8 | int n = x.size(); 9 | for(int i=0;i>t; 29 | while(t--) 30 | { 31 | string x; 32 | cin>>x; 33 | bool ans = checkDuplicate(x); 34 | if(ans) 35 | cout<<"Duplicate Found\n"; 36 | else 37 | cout<<"Duplicate Not Found\n"; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /ds/heap/check-if-heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool checkMinHeap(int arr[],int n,int i) 5 | { 6 | if(i<=(n-2)/2) 7 | { 8 | int child1 = 2*i + 1; 9 | int child2 = 2*i + 2; 10 | if(child1arr[child1]) 11 | return false; 12 | if(child2arr[child2]) 13 | return false; 14 | bool v1 = child1>t; 25 | while(t--) 26 | { 27 | int n; 28 | cin>>n; 29 | int a[n]; 30 | for(int i=0;i>a[i]; 31 | cout<<(checkMinHeap(a,n,0)?"True":"False")<<"\n"; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /algo/dynamic-programming/min-cost-path.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | int a[m][n]; 13 | for(int i=0;i>a[i][j]; 16 | int dp[m][n]; 17 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | string x; 12 | cin>>x; 13 | int n = x.size(); 14 | int dp[n][n]; 15 | memset(dp,0,sizeof(dp)); 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | unordered_map m; 5 | int solve(string &x,int l, int r) 6 | { 7 | if(l>r) 8 | return 0; 9 | string key = x.substr(l,r-l+1); 10 | if(m.find(key)!=m.end()) 11 | return m[key]; 12 | int ans = 1; 13 | for(int len=1;len<=(r-l+1)/2;len++) 14 | { 15 | if(x.substr(l,len) == x.substr(r-len+1,len)) 16 | ans = max(ans,2 + solve(x,l+len,r-len)); 17 | } 18 | m[key] = ans; 19 | return m[key]; 20 | } 21 | 22 | int main() 23 | { 24 | int t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | string x; 29 | cin>>x; 30 | m.clear(); 31 | int n = x.size(); 32 | int ans = solve(x,0,n-1); 33 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string x; 11 | cin>>x; 12 | string decrypt=""; 13 | stack s; 14 | int n=x.size(); 15 | for(int i=0;x[i];i++) 16 | { 17 | s.push(i+1+'0'); 18 | while(!s.empty() && x[i]=='I') 19 | { 20 | decrypt+=s.top(); 21 | s.pop(); 22 | } 23 | } 24 | s.push('0'+x.size()+1); 25 | while(!s.empty()) 26 | { 27 | decrypt+=s.top(); 28 | s.pop(); 29 | } 30 | cout< 2 | using namespace std; 3 | 4 | bool areKAnagrams(string str1, string str2, int k) 5 | { 6 | if(str1.size()!=str2.size()) 7 | return false; 8 | int s1[26]={0},s2[26]={0}; 9 | int diff = 0; 10 | for(int i=0;i>t; 25 | while(t--) 26 | { 27 | string str1,str2; 28 | cin>>str1>>str2; 29 | int k; 30 | cin>>k; 31 | if (areKAnagrams(str1, str2, k)) 32 | cout<<"YES\n"; 33 | else 34 | cout<<"NO\n"; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /algo/hashing/pair-with-given-sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,sum; 12 | cin>>n>>sum; 13 | unordered_map map; 14 | for(int i=0;i>x; 18 | map[x]++; 19 | } 20 | int num_of_pairs = 0; 21 | unordered_map::iterator it = map.begin(); 22 | for(;it!=map.end();++it) 23 | { 24 | int n1 = it->first; 25 | int n2 = sum - n1; 26 | if(n1==n2) 27 | if(map[n1]>=2) 28 | num_of_pairs += (map[n1]*(map[n1]-1))/2; 29 | else 30 | num_of_pairs += map[n1]*map[n2]; 31 | } 32 | cout< 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]; 14 | int ans = 1; 15 | for(int i=1;i=0 && hans) 22 | ans=h-l+1; 23 | l--; 24 | h++; 25 | } 26 | l = i-1; 27 | h = i; 28 | while(l>=0 && hans) 31 | ans=h-l+1; 32 | l--; 33 | h++; 34 | } 35 | } 36 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 14 | int las[n][2]; 15 | int ans = 1; 16 | for(int i=0;ilas[i][1]) 22 | las[i][1] = las[j][0] + 1; 23 | if(a[j]>a[i] && las[j][1]+1>las[i][0]) 24 | las[i][0] = las[j][1] + 1; 25 | } 26 | ans = max(ans,max(las[i][0],las[i][1])); 27 | 28 | } 29 | cout< 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]; 14 | int ans = 1; 15 | 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 n,W; 11 | cin>>n>>W; 12 | int val[n],wt[n]; 13 | for(int i=0;i>val[i]; 14 | for(int i=0;i>wt[i]; 15 | 16 | int knapsack[n+1][W+1]; 17 | for(int i=0;i<=n;i++) 18 | { 19 | for(int w = 0;w<=W;w++) 20 | { 21 | if(i==0 || w==0) 22 | knapsack[i][w]=0; 23 | else 24 | { 25 | if(wt[i-1]<=w) 26 | knapsack[i][w] = max(knapsack[i-1][w],knapsack[i-1][w-wt[i-1]]+val[i-1]); 27 | else 28 | knapsack[i][w] = knapsack[i-1][w]; 29 | } 30 | } 31 | } 32 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | string x; 10 | cin>>x; 11 | string n="",ans=""; 12 | for(int i=x.size()-1;i>=0;i--) 13 | { 14 | if(x[i]==' ') 15 | { 16 | int s = 0, e = n.size()-1; 17 | while(s 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 | string x; 13 | cin>>x; 14 | int ans[n][n]={0}; 15 | 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 n; 11 | cin>>n; 12 | string x; 13 | cin>>x; 14 | bool ans[n][n]={false}; 15 | int sol = 1; 16 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main() { 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]; 14 | queue q; 15 | int vis[n]={0}; 16 | q.push(0); 17 | vis[0]=0; 18 | while(!q.empty() && vis[n-1]==0) 19 | { 20 | int top = q.front(); 21 | q.pop(); 22 | for(int i=1;i<=a[top];i++) 23 | { 24 | if((top+i) 2 | using namespace std; 3 | 4 | int main() { 5 | int dp[1001][2][3]={0}; 6 | for(int i=0;i<=1000;i++) 7 | dp[i][0][0]=1; 8 | for(int j=0;j<=1;j++) 9 | for(int k=0;k<=2;k++) 10 | dp[0][j][k]=1; 11 | for(int i=1;i<=1000;i++) 12 | { 13 | for(int j=0;j<=1;j++) 14 | { 15 | for(int k=0;k<=2;k++) 16 | { 17 | dp[i][j][k]=dp[i-1][j][k]; 18 | if(j>0) 19 | dp[i][j][k]+=dp[i-1][j-1][k]; 20 | if(k>0) 21 | dp[i][j][k]+=dp[i-1][j][k-1]; 22 | } 23 | } 24 | } 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | int n; 30 | cin>>n; 31 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,k; 11 | cin>>n>>k; 12 | int a[n]; 13 | for(int i=0;i>a[i]; 15 | deque q; 16 | for(int i=0;i=a[q.back()]) 19 | q.pop_back(); 20 | q.push_back(i); 21 | } 22 | for(int i=k;i=a[q.back()]) 29 | q.pop_back(); 30 | q.push_back(i); 31 | } 32 | cout< 2 | #include 3 | using namespace std; 4 | 5 | unordered_map m; 6 | 7 | long solve(long x,long y) 8 | { 9 | long min1 = x*y; 10 | long min2 = x*y; 11 | 12 | if(x==y) 13 | return 1; 14 | string key = to_string(x) + "|" + to_string(y); 15 | if(m.find(key)!=m.end()) 16 | return m[key]; 17 | for(int i=1;i<=x/2;i++) 18 | min1 = min(min1,solve(i,y)+solve(x-i,y)); 19 | for(int i=1;i<=y/2;i++) 20 | min2 = min(min2,solve(x,i)+solve(x,y-i)); 21 | 22 | m[key] = min(min1,min2); 23 | return m[key]; 24 | } 25 | 26 | int main() { 27 | int t; 28 | cin>>t; 29 | while(t--) 30 | { 31 | long x,y; 32 | cin>>x>>y; 33 | long ans = solve(x,y); 34 | cout<& primes) { 4 | int ugly[n]; 5 | ugly[0]=1; 6 | int p = primes.size(); 7 | int index[p]; 8 | int next[p]; 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 13 | int sum_left[n],sum_right[n]; 14 | sum_left[0]=a[0]; 15 | sum_right[n-1]=a[n-1]; 16 | for(int i=1;i=0;i--) 19 | sum_right[i]=sum_right[i+1]+a[i]; 20 | bool flag = false; 21 | for(int i=0;i0?sum_left[i-1]:0; 24 | int y = i 2 | #include 3 | using namespace std; 4 | 5 | int main() { 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 | int l=0,r=n-1; 16 | int leftMax = INT_MIN,rightMax = INT_MIN; 17 | int val = 0; 18 | while(l<=r) 19 | { 20 | if(a[l]leftMax) 23 | leftMax = a[l]; 24 | else 25 | val += leftMax - a[l]; 26 | l++; 27 | } 28 | else 29 | { 30 | if(a[r]>rightMax) 31 | rightMax = a[r]; 32 | else 33 | val+= rightMax - a[r]; 34 | r--; 35 | } 36 | } 37 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,m; 12 | cin>>n>>m; 13 | string a,b; 14 | cin>>a>>b; 15 | int lcs[n+1][m+1]; 16 | int ans = 0; 17 | int end = 1; 18 | for(int i=0;i<=n;i++) 19 | { 20 | for(int j=0;j<=m;j++) 21 | { 22 | if(i==0||j==0) 23 | lcs[i][j]=0; 24 | else if(a[i-1]==b[j-1]) 25 | { 26 | lcs[i][j]=lcs[i-1][j-1]+1; 27 | if(ans>lcs[i][j]) 28 | { 29 | ans = lcs[i][j]; 30 | end = i; 31 | } 32 | } 33 | else 34 | lcs[i][j]=0; 35 | } 36 | } 37 | cout< 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 | int mat[m][n]; 13 | for(int i=0;i>mat[i][j]; 16 | 17 | int i=0,j=0,r=m,c=n; 18 | while(i=j;x--) 29 | cout<=i; y--) 35 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int len,k; 10 | cin>>len>>k; 11 | string x; 12 | cin>>x; 13 | if(k>=len-1) 14 | { 15 | cout<<"1\n"; 16 | continue; 17 | } 18 | int lps[len][len]; 19 | for(int i=0;i=(len - lps[0][len-1]))<<"\n"; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /algo/dynamic-programming/optimal-cost-to-construct-BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | unordered_map m; 7 | 8 | int solve(int freq[],int l, int r, int level) 9 | { 10 | if(l>r) 11 | return 0; 12 | string key = to_string(l) + "|" + to_string(r) + "|" + to_string(level); 13 | if(m.find(key)!=m.end()) 14 | return m[key]; 15 | int ans = INT_MAX; 16 | for(int i = l; i<=r; i++) 17 | ans = min(ans,level*freq[i] + solve(freq,l,i-1,level+1)+solve(freq,i+1,r,level+1)); 18 | return (m[key] = ans); 19 | 20 | } 21 | 22 | 23 | int main() 24 | { 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | int n; 30 | cin>>n; 31 | int freq[n]; 32 | for(int i=0;i>freq[i]; 34 | int ans = solve(freq,0,n-1,1); 35 | cout< 2 | #include 3 | using namespace std; 4 | 5 | set ans; 6 | void perm(string x,int i,int n) 7 | { 8 | if(i==n-1) 9 | { 10 | if(ans.find(x)==ans.end()) 11 | ans.insert(x); 12 | return; 13 | } 14 | //No Change 15 | perm(x,i+1,n); 16 | //Swaping for permutations 17 | for(int j=i+1;j<=n-1;j++) 18 | { 19 | swap(x[i],x[j]); 20 | perm(x,i+1,n); 21 | swap(x[i],x[j]); 22 | } 23 | } 24 | 25 | int main() { 26 | int t; 27 | cin>>t; 28 | while(t--) 29 | { 30 | ans.clear(); 31 | string x; 32 | cin>>x; 33 | perm(x,0,x.size()); 34 | set::iterator it = ans.begin(); 35 | for(;it!=ans.end();++it) 36 | cout<<*it<<" "; 37 | cout<<"\n"; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /algo/dynamic-programming/longest-bitonic-subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | int lis[n]; 14 | int lds[n]; 15 | for(int i=0;i>a[i]; 18 | lis[i]=1; 19 | lds[i]=1; 20 | } 21 | for(int i=1;ia[j] && lis[i]=0;i--) 30 | { 31 | for(int j=i+1;ja[j] && lds[i] 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int m,n; 10 | cin>>m>>n; 11 | int mat[m][n]; 12 | for(int i=0;i>mat[i][j]; 15 | int dp[m][n]; 16 | dp[m-1][n-1] = mat[m-1][n-1]>0?1:1-mat[m-1][n-1]; 17 | for(int i=n-2;i>=0;i--) 18 | dp[m-1][i]=max(dp[m-1][i+1]-mat[m-1][i],1); 19 | for(int i=m-2;i>=0;i--) 20 | dp[i][n-1]=max(dp[i+1][n-1]-mat[i][n-1],1); 21 | for(int i=m-2;i>=0;i--) 22 | { 23 | for(int j=n-2;j>=0;j--) 24 | { 25 | int min_points_on_exit = min(dp[i+1][j],dp[i][j+1]); 26 | dp[i][j] = max(1,min_points_on_exit-mat[i][j]); 27 | } 28 | } 29 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int arrival[n],dept[n]; 13 | for(int i=0;i>arrival[i]; 15 | for(int i=0;i>dept[i]; 17 | sort(arrival,arrival+n); 18 | sort(dept,dept+n); 19 | int i=1,j=0; 20 | int p = 1 , r = 1; 21 | while(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 | int a[m*n]; 13 | int mat[m][n]; 14 | for(int i=0;i>a[i]; 15 | int r=0,c=0,k=0; 16 | while(r=c;i--) 27 | mat[m-1][i] = a[k++]; 28 | } 29 | if(c=r;j--) 32 | mat[j][c] = a[k++]; 33 | } 34 | } 35 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool isIsomorphic(string x,string y) 5 | { 6 | if(x.size()!=y.size()) 7 | return false; 8 | 9 | int c[26]; 10 | bool vis[26]; 11 | for(int i=0;i<26;i++) 12 | { 13 | c[i]=-1; 14 | vis[i]=false; 15 | } 16 | int n = x.size(); 17 | 18 | for(int i=0;i>t; 40 | while(t--) 41 | { 42 | string x,y; 43 | cin>>x>>y; 44 | if(isIsomorphic(x,y)) 45 | cout<<"YES\n"; 46 | else 47 | cout<<"NO\n"; 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /algo/binary-search/find-frequency-of-elements-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lower_bound(int arr[],int x,int n) 5 | { 6 | int l = 0; 7 | int r = n-1; 8 | while(l=x) 12 | r=mid; 13 | else 14 | l=mid+1; 15 | } 16 | return l; 17 | } 18 | 19 | int upper_bound(int arr[],int x,int n) 20 | { 21 | int l = 0; 22 | int r = n-1; 23 | while(lx) 27 | r=mid; 28 | else 29 | l=mid+1; 30 | } 31 | return l; 32 | } 33 | 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t) 40 | { 41 | int n,x; 42 | cin>>n>>x; 43 | int a[n]; 44 | for(int i=0;i>a[i]; 45 | cout<<(upper_bound(a,x,n) - lower_bound(a,x,n))<<"\n"; 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /algo/greedy-programming/max-length-chain-reorderin-allowed-greedy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Pair 5 | { 6 | public: 7 | int start; 8 | int end; 9 | Pair(int x,int y) 10 | { 11 | this->start = x; 12 | this->end = y; 13 | } 14 | } 15 | 16 | bool comp(const Pair &x,const Pair &y) 17 | { 18 | return x.end < y.end ; 19 | } 20 | 21 | 22 | int main() 23 | { 24 | int t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | int n; 29 | cin>>n; 30 | Pair arr[n]; 31 | for(int i=0;i>x>>y; 35 | arr[i] = Pair(x,y); 36 | } 37 | sort(arr,arr+n,comp); 38 | int start = 0; 39 | int count = 1; 40 | for(int i=1;i arr[start].end) 43 | { 44 | count++; 45 | start = i; 46 | } 47 | } 48 | cout< 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 | int mat[m][n]; 13 | for(int i=0;i>mat[i][j]; 16 | 17 | int ans = mat[0][0]; 18 | for(int left_c = 0; left_c < n; left_c++) 19 | { 20 | int temp[m] = {0}; 21 | for(int right_c = left_c; right_c < n; right_c++) 22 | { 23 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | #define MAX_VAL 100 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int n; 13 | cin>>n; 14 | int a[n]; 15 | for(int i=0;i>a[i]; 16 | int target; 17 | cin>>target; 18 | int ans[n][MAX_VAL+1]; 19 | for(int j=0;j<=MAX_VAL;j++) 20 | ans[0][j]=abs(j-a[0]); 21 | 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 m,n; 11 | cin>>m>>n; 12 | int mat[m][n]; 13 | for(int i=0;i>mat[i][j]; 16 | 17 | int ans = mat[0][0]; 18 | for(int left_c = 0; left_c < n; left_c++) 19 | { 20 | int temp[m] = {0}; 21 | for(int right_c = left_c; right_c < n; right_c++) 22 | { 23 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int types = 10; 13 | int val[] ={1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 14 | int sol[n+1][types+1]={0}; 15 | for(int i=0;i<=n;i++) 16 | { 17 | for(int j=0;j<=types;j++) 18 | { 19 | sol[i][j]= i; 20 | if(i==0) 21 | sol[i][j]=0; 22 | else if(j==0) 23 | sol[i][j] = INT_MAX; 24 | else 25 | { 26 | if(val[j-1]<=i) 27 | sol[i][j] = min(sol[i][j-1],1 + sol[i-val[j-1]][j]); 28 | else 29 | sol[i][j]=sol[i][j-1]; 30 | } 31 | } 32 | } 33 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | long ugly[10001]; 6 | ugly[0]=1; 7 | int i2=0,i3=0,i5=0; 8 | long next_2 = 2,next_3 = 3,next_5 = 5; 9 | for(int i=1;i<10001;i++) 10 | { 11 | long next = min(next_2,min(next_3,next_5)); 12 | if(next == next_2) 13 | { 14 | i2++; 15 | ugly[i]=next_2; 16 | next_2 = ugly[i2]*2; 17 | } 18 | if(next == next_3) 19 | { 20 | i3++; 21 | ugly[i]=next_3; 22 | next_3 = ugly[i3]*3; 23 | } 24 | if(next == next_5) 25 | { 26 | i5++; 27 | ugly[i]=next_5; 28 | next_5 = ugly[i5]*5; 29 | } 30 | } 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | int n; 36 | cin>>n; 37 | cout< 2 | #include 3 | #include 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 | pair a[n]; 15 | for(int i=0;i>x>>y; 19 | a[i] = {x,y}; 20 | } 21 | sort(a,a+n); 22 | int index = 0; 23 | for(int i=1;i curr = a[i],prev = a[index]; 26 | if(curr.first > prev.second) 27 | { 28 | index++; 29 | a[index] = a[i]; 30 | } 31 | else 32 | { 33 | a[index].second = max(a[index].second,a[i].second); 34 | a[index].first = min(a[index].first,a[i].first); 35 | } 36 | } 37 | for(int i=0;i<=index;i++) 38 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | string a[n]; 12 | int minLen = 10001; 13 | for(int i=0;i>a[i]; 16 | minLen = (minLen0) 38 | cout< 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int m,n; 13 | cin>>m>>n; 14 | int a[m][n]; 15 | for(int i=0;i>a[i][j]; 18 | int cost; 19 | cin>>cost; 20 | int sol[m+1][n+1][cost+1]={0}; 21 | for(int i=0;i<=m;i++) 22 | { 23 | for(int j=0;j<=n;j++) 24 | { 25 | if(i==0 && j==0) 26 | sol[i][j][a[i][j]]=1; 27 | else 28 | { 29 | for(int k = 0;k<=cost-a[i][j];k++) 30 | { 31 | if(i>0) 32 | sol[i][j][k+a[i][j]]=sol[i-1][j][k]+1; 33 | if(j>0) 34 | sol[i][j][k+a[i][j]]=sol[i][j-1][k]+1; 35 | } 36 | } 37 | } 38 | } 39 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | string a; 12 | cin>>a; 13 | int n = a.size(); 14 | int wR = 1; 15 | int ans = 1; 16 | int lastseen[256]; 17 | int curr = 1; 18 | memset(lastseen,-1,sizeof(lastseen)); 19 | lastseen[a[0]] = 0; 20 | while(wR 2 | using namespace std; 3 | 4 | long long find_element(long long arr[],int n) 5 | { 6 | int l=0,r=n-1; 7 | while(l<=r) 8 | { 9 | int mid = l + (r-l)/2; 10 | if(mid & 1) 11 | { 12 | if(arr[mid]==arr[mid-1]) 13 | l=mid+1; 14 | else if(arr[mid]==arr[mid+1]) 15 | r=mid-1; 16 | else 17 | return arr[mid]; 18 | } 19 | else 20 | { 21 | if(arr[mid]==arr[mid-1]) 22 | r=mid-1; 23 | else if(arr[mid]==arr[mid+1]) 24 | l=mid+1; 25 | else 26 | return arr[mid]; 27 | } 28 | } 29 | return -1; 30 | } 31 | 32 | int main() { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | long long a[n]; 40 | for(int i=0;i>a[i]; 41 | long long element = find_element(a,n); 42 | cout< 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int t; 12 | cin>>t; 13 | while(t--) 14 | { 15 | int n; 16 | cin>>n; 17 | int a[n]; 18 | for(int i=0;i>a[i]; 19 | int root = 0; 20 | stack st; 21 | bool flag = true; 22 | for(int i=0;i 4 | using namespace std; 5 | 6 | int solve(int a1[],int a2[],int n,int m,int k) 7 | { 8 | int l1=0,r1=n; 9 | int l2=0,r2=m; 10 | while(l1a2[m2]) 17 | l2=m2+1; 18 | else 19 | l1=m1+1; 20 | } 21 | else 22 | if(a1[m1]>a2[m2]) 23 | r1=m1; 24 | else 25 | r2=m2; 26 | } 27 | if(l1==r1) 28 | return a2[k]; 29 | else 30 | return a1[k]; 31 | } 32 | 33 | 34 | int main() 35 | { 36 | int t; 37 | cin>>t; 38 | while(t--) 39 | { 40 | int n,m,k; 41 | cin>>n>>m>>k; 42 | int a[n],b[m]; 43 | for(int i=0;i>a[i]; 44 | for(int i=0;i>b[i]; 45 | int ans = solve(a,b,n,m,k); 46 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int k; 10 | string x; 11 | cin>>x>>k; 12 | int hash[26]={0}; 13 | int n = x.size(); 14 | int l=0,r=-1; 15 | int ans = -1; 16 | int curr = 0; 17 | if(k<=0) 18 | { 19 | cout<<"-1\n"; 20 | continue; 21 | } 22 | while(r 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,k; 11 | cin>>k; 12 | cin>>n; 13 | long a[n]; 14 | for(int i=0;i>a[i]; 16 | long dp[k+1][n+1]; 17 | memset(dp,0,sizeof(dp)); 18 | for(int i=1;i<=k;i++) 19 | { 20 | int prev_diff = INT_MIN; 21 | for(int j=2;j<=n;j++) 22 | { 23 | dp[i][j] = dp[i][j-1]; 24 | prev_diff = max(prev_diff,dp[i-1][j-1] - a[j-2]); 25 | /* 26 | for(int k = 1;k 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | unordered_set m; 12 | int n; 13 | cin>>n; 14 | for(int i=0;i>x; 18 | m.insert(x); 19 | } 20 | string str; 21 | cin>>str; 22 | int len = str.size(); 23 | int dp[len+1]={0}; 24 | for(int i=1;i<=len;i++) 25 | { 26 | if(!dp[i] && (m.find(str.substr(0,i))!=m.end())) 27 | dp[i]=true; 28 | if(dp[i]) 29 | { 30 | for(int j=i+1;j<=len;j++) 31 | { 32 | if(!dp[j] && (m.find(str.substr(i,j-i))!=m.end())) 33 | dp[j]=true; 34 | } 35 | } 36 | } 37 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int partition(int arr[],int s,int e) 6 | { 7 | srand(time(0)); 8 | int random_pivot = s + rand()%(e-s+1); 9 | swap(arr[s],arr[random_pivot]); 10 | int pivot = arr[s]; 11 | int j = s; 12 | for(int i=s+1;i<=e;i++) 13 | { 14 | if(arr[i]>t; 38 | while(t--) 39 | { 40 | int n; 41 | cin>>n; 42 | int a[n]; 43 | for(int i=0;i>a[i]; 44 | quicksort(a,0,n-1); 45 | 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 | int a[m*n]; 13 | int mat[m][n]; 14 | for(int i=0;i>a[i]; 15 | int r=0,c=0,k=0; 16 | int prev = mat[0][0]; 17 | while(r=c;i--) 28 | swap(mat[m-1][i],prev); 29 | } 30 | if(c=r;j--) 33 | swap(mat[j][c],prev); 34 | } 35 | } 36 | mat[0][0] = prev; 37 | 38 | 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 n; 11 | cin>>n; 12 | int a[n],sum = 0; 13 | for(int i=0;i>a[i]; 16 | sum+=a[i]; 17 | } 18 | bool isPossible[n+1][sum+1]={false}; 19 | for(int i=0;i<=n;i++) 20 | isPossible[i][0] = true; 21 | for(int i=1;i<=sum;i++) 22 | isPossible[0][i] = false; 23 | for(int i=1;i<=n;i++) 24 | { 25 | for(int j=1;j<=sum;j++) 26 | { 27 | if(a[i-1]<=j) 28 | isPossible[i][j] = isPossible[i-1][j]||isPossible[i-1][j-a[i-1]]; 29 | else 30 | isPossible[i][j] = isPossible[i-1][j]; 31 | } 32 | } 33 | int min = sum; 34 | for(int i=1;i<=sum;i++) 35 | { 36 | if(isPossible[n][i] && min>abs(sum - 2*i)) 37 | min = abs(sum - 2*i); 38 | } 39 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int m,n,k,start_x,start_y; 12 | cin>>m>>n>>start_x>>start_y>>k; 13 | int d[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; 14 | double dp[m][n][k+1]; 15 | 16 | for(int i=0;i=0 && x=0 && y 2 | using namespace std; 3 | 4 | int binary_search(int arr[],int l,int r,int k) 5 | { 6 | while(l<=r) 7 | { 8 | int mid = l + (r-l)/2; 9 | if(arr[mid]k) 12 | r=mid-1; 13 | else 14 | return mid; 15 | } 16 | return -1; 17 | } 18 | 19 | int pivot(int arr[],int n) 20 | { 21 | int l=0,r=n-1; 22 | while(l>1; 25 | if(arr[mid]>t; 36 | while(t--) 37 | { 38 | int n,x; 39 | cin>>n>>x; 40 | int a[n],p; 41 | for(int i=0;i>a[i]; 42 | if(a[0]< a[n-1]) p = 0; 43 | else 44 | p=pivot(a,n); 45 | if(a[n-1] 2 | using namespace std; 3 | 4 | void merge(int arr[],int s1,int s2,int e) 5 | { 6 | int temp[e-s1+1]; 7 | int i=s1,j=s2,k=0; 8 | while(i>t; 39 | while(t--) 40 | { 41 | int n; 42 | cin>>n; 43 | int a[n]; 44 | for(int i=0;i>a[i]; 45 | mergesort(a,0,n-1); 46 | for(int i=0;i 2 | #include 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 | int freq[n]; 14 | for(int i=0;i>freq[i]; 16 | int dp[n][n]; 17 | int sum[n]; 18 | sum[0]=freq[0]; 19 | for(int i=0;ii?dp[i][k-1]?0; 32 | int right = k 2 | using namespace std; 3 | 4 | bool isPalindrome(string x) 5 | { 6 | int l = 0,r = x.size()-1; 7 | while(l>t; 17 | while(t--) 18 | { 19 | string x; 20 | cin>>x; 21 | int n = x.size(); 22 | int count[26]={0}; 23 | for(int i=0;i2) 29 | { 30 | flag = true; 31 | break; 32 | } 33 | } 34 | if(flag) 35 | { 36 | cout<<"YES\n"; 37 | continue; 38 | } 39 | string new_str=""; 40 | for(int i=0;i1) 43 | new_str+=x[i]; 44 | } 45 | if(!isPalindrome(new_str)) 46 | cout<<"YES\n"; 47 | else 48 | cout<<"NO\n"; 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /algo/dynamic-programming/printing-longest-common-subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 100000 4 | 5 | int lcs[MAX+1][MAX+1]; 6 | 7 | string get_lcs(string a,string b,int n,int m) 8 | { 9 | if(n==0||m==0) 10 | return ""; 11 | else if(a[n-1]==b[m-1]) 12 | return get_lcs(a,b,n-1,m-1)+a[n-1]; 13 | else 14 | { 15 | if(lcs[n-1][m]>lcs[n][m-1]) 16 | return get_lcs(a,b,n-1,m); 17 | else 18 | return get_lcs(a,b,n,m-1); 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | int t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | int n,m; 29 | cin>>n>>m; 30 | string a,b; 31 | cin>>a>>b; 32 | for(int i=0;i<=n;i++) 33 | { 34 | for(int j=0;j<=m;j++) 35 | { 36 | if(i==0||j==0) 37 | lcs[i][j]=0; 38 | else if(a[i-1]==b[j-1]) 39 | lcs[i][j]=lcs[i-1][j-1]+1; 40 | else 41 | lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]); 42 | } 43 | } 44 | cout< 2 | using namespace std; 3 | #define INF 1000000000001 4 | 5 | int main() 6 | { 7 | long t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | long n; 12 | cin>>n; 13 | long dim[n+1]; 14 | // if dim[] = {10,30,5,60} we have matrix as 10x30,30x5,5x60 15 | for(long i=0;i<=n;i++) 16 | cin>>dim[i]; 17 | long dp[n+1][n+1]; 18 | for(long i=1;i<=n;i++) 19 | dp[i][i]=0; 20 | for(long i=1;i<=n-1;i++) 21 | dp[i][i+1]=dim[i-1]*dim[i]*dim[i+1]; 22 | for(long d=3;d<=n;d++) 23 | { 24 | for(long i=1;i<=n-d+1;i++) 25 | { 26 | long minimum = INF; 27 | long j = i+d-1; 28 | for(long k=i;k<=j-1;k++) 29 | { 30 | long cost = dp[i][k]; 31 | cost+=dp[k+1][j]; 32 | cost+=dim[i-1]*dim[k]*dim[j]; 33 | minimum = min(minimum,cost); 34 | } 35 | dp[i][j]=minimum; 36 | } 37 | } 38 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | unordered_set m; 12 | int n; 13 | cin>>n; 14 | for(int i=0;i>x; 18 | m.insert(x); 19 | } 20 | string str; 21 | cin>>str; 22 | int len = str.size(); 23 | int dp[len+1]; 24 | memset(dp,-1,sizeof(dp)); 25 | for(int i=1;i<=len;i++) 26 | { 27 | if((m.find(str.substr(0,i))!=m.end())) 28 | dp[i]=0; 29 | if(dp[i]!=-1) 30 | { 31 | for(int j=i+1;j<=len;j++) 32 | { 33 | if(m.find(str.substr(i,j-i))!=m.end()) 34 | 35 | if(dp[j]!=-1) 36 | dp[j]=min(dp[j],dp[i]+1); 37 | else 38 | dp[j] = dp[i] + 1; 39 | } 40 | } 41 | } 42 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | // O(N^3) solution see binary search folder for O(N log(sum[arr[i]])) time complexity 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int n,k; 13 | cin>>n>>k; 14 | int a[n]; 15 | int sum[n]={0}; 16 | for(int i=0;i>a[i]; 18 | sum[0]=a[0]; 19 | for(int i=1;i 2 | using namespace std; 3 | 4 | class Pair 5 | { 6 | public: 7 | int start; 8 | int end; 9 | Pair(int x,int y) 10 | { 11 | this->start = x; 12 | this->end = y; 13 | } 14 | } 15 | 16 | bool comp(const Pair &x,const Pair &y) 17 | { 18 | return x.start < y.start ; 19 | } 20 | 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int n; 28 | cin>>n; 29 | Pair arr[n]; 30 | int ans[n]={0}; 31 | for(int i=0;i>x>>y; 35 | arr[i] = Pair(x,y); 36 | ans[i]=1; 37 | } 38 | //Sort in increasing order of start if reordering is allowed 39 | sort(arr,arr+n,comp); 40 | 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 n; 11 | cin>>n; 12 | int a[n]; 13 | int sum = 0; 14 | for(int i=0;i>a[i]; 17 | sum+=a[i]; 18 | } 19 | if(sum & 1) 20 | cout<<"NO\n"; 21 | else 22 | { 23 | sum/=2; 24 | bool isPossible[n+1][sum+1]; 25 | for(int i=0;i<=n;i++) 26 | isPossible[i][0]=true; 27 | for(int i=0;i<=sum;i++) 28 | isPossible[0][i]=false; 29 | for(int i=1;i<=n;i++) 30 | { 31 | for(int j=1;j<=sum;j++) 32 | { 33 | if(a[i-1]<=j) 34 | isPossible[i][j]=(isPossible[i-1][j]||isPossible[i-1][j-a[i-1]]); 35 | else 36 | isPossible[i][j]=isPossible[i-1][j]; 37 | } 38 | } 39 | cout<<(isPossible[n][sum]?"YES":"NO")<<"\n"; 40 | } 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /algo/binary-search/median-of-2-sorted-array-diff-size.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int get_median(int a1[],int a2[],int n,int m) 5 | { 6 | int l=0,r=n; 7 | int posX,posY; 8 | while(l<=r) 9 | { 10 | posX = (l+r)/2; 11 | posY = (n+m+1)/2 - posX; 12 | if(posX ==0 || a1[posX - 1]<=a2[posY]) 13 | { 14 | if(posY == 0 || a2[posY - 1] <= a1[posX]) 15 | break; 16 | else 17 | l=posX+1; 18 | } 19 | else 20 | r=posX-1; 21 | } 22 | if((n+m) & 1) 23 | return max((posX > 0?a1[posX-1]:-1e6),(posY > 0?a2[posY-1]:-1e6)); 24 | else 25 | return (max((posX > 0?a1[posX-1]:-1e6),(posY > 0?a2[posY-1]:-1e6))+min((posX < n?a1[posX]:1e6),(posY < m?a2[posY]:1e6)))/2; 26 | } 27 | 28 | 29 | 30 | int main() 31 | { 32 | int t; 33 | cin>>t; 34 | while(t--) 35 | { 36 | int n,m; 37 | cin>>n>>m; 38 | int a[n],b[m]; 39 | for(int i=0;i>a[i]; 40 | for(int i=0;i>b[i]; 41 | int ans = get_median(a,b,n,m); 42 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int partition(int a[],int l,int r) 7 | { 8 | srand(time(0)); 9 | int rndm = l + rand()%(r-l+1); 10 | swap(a[rndm],a[l]); 11 | int piv_val = a[l]; 12 | int i = l; 13 | for(int j=l+1;j<=r;j++) 14 | { 15 | if(a[j] k) 36 | r = pivot - 1; 37 | else 38 | l = pivot + 1; 39 | } 40 | return -1; 41 | } 42 | 43 | int main() 44 | { 45 | int t; 46 | cin>>t; 47 | while(t--) 48 | { 49 | int n,k; 50 | cin>>k>>n; 51 | int a[n]; 52 | for(int i=0;i>a[i]; 53 | cout< 2 | using namespace std; 3 | //Position this line where user code will be pasted. 4 | int main() 5 | { 6 | int T; 7 | cin>>T; 8 | while(T--) 9 | { 10 | int N, E; 11 | cin>>N>>E; 12 | 13 | vector g[N]; 14 | bool vis[N]; 15 | 16 | memset(vis, false, sizeof(vis)); 17 | 18 | for(int i=0;i>u>>v; 22 | g[u].push_back(v); 23 | g[v].push_back(u); 24 | } 25 | dfs(0,g,vis); 26 | 27 | cout< g[], bool vis[]) 37 | { 38 | vis[s]=true; 39 | cout< 2 | using namespace std; 3 | 4 | int mat[1001][1001]; 5 | 6 | unordered_map memory; 7 | 8 | int numberOfPaths(int i,int j,int cost) 9 | { 10 | if(i==0 && j==0) 11 | return cost==mat[i][j]; 12 | string key = to_string(i) + "|" + to_string(j) + "|" + to_string(cost); 13 | if(memory.find(key)==memory.end()) 14 | { 15 | if(i==0) 16 | m[key] = numberOfPaths(i,j-1,cost-mat[i][j]); 17 | else if(j==0) 18 | m[key] = numberOfPaths(i-1,j,cost-mat[i][j]); 19 | else 20 | m[key] = numberOfPaths(i-1,j,cost-mat[i][j]) + numberOfPaths(i,j-1,cost-mat[i][j]); 21 | } 22 | return m[key]; 23 | } 24 | 25 | 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | memory.clear(); 33 | int m,n,cost; 34 | cin>>m>>n; 35 | for(int i=0;i>mat[i][j]; 38 | cin>>cost; 39 | int answer = numberOfPaths(n-1,n-1,cost); 40 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool check_match(char y,char x) 6 | { 7 | if(x=='[' && y==']') 8 | return true; 9 | if(x=='{' && y=='}') 10 | return true; 11 | if(x=='(' && y==')') 12 | return true; 13 | return false; 14 | } 15 | 16 | int main() { 17 | int t; 18 | cin>>t; 19 | while(t--) 20 | { 21 | string x; 22 | cin>>x; 23 | stack st; 24 | bool flag = true; 25 | for(int i=0;x[i];i++) 26 | { 27 | if(x[i]=='('||x[i]=='{'||x[i]=='[') 28 | st.push(x[i]); 29 | else 30 | { 31 | 32 | if(!st.empty() && check_match(x[i],st.top())) 33 | st.pop(); 34 | else 35 | { 36 | flag = false; 37 | break; 38 | } 39 | } 40 | } 41 | if(!st.empty()) 42 | flag = false; 43 | if(flag) 44 | cout<<"balanced\n"; 45 | else 46 | cout<<"not balanced\n"; 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /algo/binary-search/painters-partion-problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int numberOfPainters(int arr[],int n,int sum) 5 | { 6 | int count = 1; 7 | int val = arr[0]; 8 | for(int i=1;isum) 11 | { 12 | count++; 13 | val=arr[i]; 14 | } 15 | else 16 | val+=arr[i]; 17 | } 18 | return count; 19 | } 20 | 21 | 22 | int main() 23 | { 24 | int t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | int n,k; 29 | cin>>n>>k; 30 | int a[n]; 31 | for(int i=0;i>a[i]; 33 | int sum,maximum=a[0]; 34 | sum=a[0]; 35 | for(int i=1;i 3 | using namespace std; 4 | 5 | int vis[1001][1001]; 6 | int a[1001][1001]; 7 | int d[4][2]= {{0,1},{0,-1},{-1,0},{1,0}}; 8 | void dfs(int i,int j,int m,int n) 9 | { 10 | vis[i][j]=1; 11 | for(int k=0;k<=3;k++) 12 | { 13 | int x = i+d[k][0]; 14 | int y = j+d[k][1]; 15 | if(x=0 && y=0 && a[i][j]-d[x][y]==1) 16 | { 17 | if(vis[i][j]==-1) 18 | dfs(x,y,m,n); 19 | vis[i][j]=max(vis[i][j],1+vis[x][y]); 20 | } 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int t; 27 | cin>>t; 28 | while(t--) 29 | { 30 | int m,n; 31 | cin>>m>>n; 32 | for(int i=0;i>a[i][j]; 35 | vis[i][j]=-1; 36 | } 37 | } 38 | int ans = 1; 39 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | class Activity 6 | { 7 | public: 8 | int start; 9 | int end; 10 | int index; 11 | Activity(int s,int e,int i) 12 | { 13 | this->start = s; 14 | this->end = e; 15 | this->index = i; 16 | } 17 | Activity() 18 | { 19 | start = -1; 20 | end = -1; 21 | index = -1; 22 | } 23 | }; 24 | 25 | bool comp(const Activity &x,const Activity &y) 26 | { 27 | return x.end>t; 34 | while(t--) 35 | { 36 | int n; 37 | cin>>n; 38 | Activity m[n]; 39 | for(int i=0;i>x; 43 | m[i]=Activity(x,-1,i+1); 44 | } 45 | for(int i=0;i>m[i].end; 47 | sort(m,m+n,comp); 48 | int start = 0,count = 1; 49 | for(int i=1;i= m[start].end) 52 | { 53 | count++; 54 | start =i; 55 | } 56 | } 57 | cout< 2 | using namespace std; 3 | 4 | void merge(int a[],int l,int mid,int r, long &ans) 5 | { 6 | int temp[r-l+1]; 7 | int i = l,j = mid+1,k=0; 8 | while(i<=mid && j<=r) 9 | { 10 | if(a[i]<=a[j]) 11 | temp[k++] = a[i++]; 12 | else 13 | { 14 | ans += mid - i + 1; 15 | temp[k++] = a[j++]; 16 | } 17 | } 18 | while(i<=mid) 19 | temp[k++] = a[i++]; 20 | while(j<=r) 21 | temp[k++] = a[j++]; 22 | k = 0; 23 | for(int i=l;i<=r;i++) 24 | a[i] = temp[k++]; 25 | } 26 | 27 | void solve(int a[],int l,int r,long &ans) 28 | { 29 | if(l>t; 41 | while(t--) 42 | { 43 | int n; 44 | cin>>n; 45 | int a[n]; 46 | for(int i=0;i>a[i]; 47 | long inv_count = 0; 48 | solve(a,0,n-1,inv_count); 49 | cout<& nums) { 4 | int n = nums.size(); 5 | vector LIS(n,0); 6 | vector count(n,0); 7 | int max=0,ans=0; 8 | for(int i=0;iLIS[i]) 20 | { 21 | count[i] = count[j]; 22 | LIS[i] = LIS[j]+1; 23 | } 24 | } 25 | } 26 | if(max 2 | #include 3 | using namespace std; 4 | 5 | class Meeting 6 | { 7 | public: 8 | int start; 9 | int end; 10 | int index; 11 | Meeting(int s,int e,int i) 12 | { 13 | this->start = s; 14 | this->end = e; 15 | this->index = i; 16 | } 17 | Meeting() 18 | { 19 | start = -1; 20 | end = -1; 21 | index = -1; 22 | } 23 | }; 24 | 25 | bool comp(const Meeting &x,const Meeting &y) 26 | { 27 | return x.end>t; 34 | while(t--) 35 | { 36 | int n; 37 | cin>>n; 38 | Meeting m[n]; 39 | for(int i=0;i>x; 43 | m[i]=Meeting(x,-1,i+1); 44 | } 45 | for(int i=0;i>m[i].end; 47 | sort(m,m+n,comp); 48 | int start = 0; 49 | cout< m[start].end) 53 | { 54 | cout< 2 | using namespace std; 3 | 4 | 5 | set vis; 6 | void DFS(int i,vector adj[],stack &s) 7 | { 8 | vis.insert(i); 9 | for(const auto & x : adj[i]) 10 | { 11 | if(vis.find(x)==vis.end()) 12 | DFS(x,adj,s); 13 | } 14 | s.push(i); 15 | } 16 | int* topoSort(int V, vector adj[]) 17 | { 18 | vis.clear(); 19 | stack s; 20 | for(int i=0;i>T; 39 | while(T--) 40 | { 41 | 42 | int N, E; 43 | cin>>N>>E; 44 | vector adj[N]; 45 | bool vis[N] = {false}; 46 | for(int i=0;i>u>>v; 50 | adj[u].push_back(v); 51 | } 52 | 53 | int* topoSorted = topoSort(N, adj); 54 | cout< 2 | #include 3 | using namespace std; 4 | 5 | /* 6 | //o(N^3) sol 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | int m,n,X; 14 | cin>>m>>n>>X; 15 | long dp[n+1][X+1]; 16 | dp[0][0] = 1; 17 | memset(dp,0,sizeof(dp)); 18 | for(int i=1;i<=X && i<=m;i++) 19 | dp[1][i] = 1; 20 | for(int i=2;i<=n;i++) 21 | { 22 | for(int j=1;j<=X;j++) 23 | { 24 | for(int k = 1;k<=m && k>t; 39 | while(t--) 40 | { 41 | int m,n,X; 42 | cin>>m>>n>>X; 43 | long dp[n+1][X+1]; 44 | memset(dp,0,sizeof(dp)); 45 | dp[0][0] = 1; 46 | for(int i=1;i<=n;i++) 47 | { 48 | for(int j=1;j<=X;j++) 49 | { 50 | dp[i][j] = dp[i-1][j-1] + dp[i][j-1]; 51 | if(j-m-1>=0) 52 | dp[i][j]-=dp[i-1][j-m-1]; 53 | } 54 | } 55 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool isValid(string x) 6 | { 7 | long long rem = 0; 8 | bool flag = false; 9 | if(x[0]=='0') 10 | return false; 11 | for(int i=0;x[i];i++) 12 | { 13 | int num = x[i]-'0'; 14 | if(num) 15 | flag = true; 16 | rem = (rem*2 + num); 17 | } 18 | if(!flag) 19 | return false; 20 | while(rem>1) 21 | { 22 | if(rem%5) 23 | return false; 24 | rem/=5; 25 | } 26 | return true; 27 | } 28 | 29 | int main() { 30 | int t; 31 | cin>>t; 32 | while(t--) 33 | { 34 | string x; 35 | cin>>x; 36 | int n = x.size(); 37 | int dp[n+1]; 38 | dp[0]=0; 39 | memset(dp,-1,sizeof(dp)); 40 | for(int i=1;i<=n;i++) 41 | { 42 | if(isValid(x.substr(0,i))) 43 | dp[i] = 1; 44 | if(dp[i]!=-1) 45 | for(int j=i;j 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | int dp[n][n]; 13 | for(int i=0;i>a[i]; 15 | if(n<=2) 16 | { 17 | cout<=0;i--) 30 | { 31 | int l = i-1,r=i+1; 32 | while(l>=0 && r2*a[i]) 37 | { 38 | dp[l][i]=2; 39 | l--; 40 | } 41 | else 42 | { 43 | dp[l][i]=dp[i][r]+1; 44 | ans = max(ans,dp[l][i]); 45 | l--; 46 | r++; 47 | } 48 | } 49 | } 50 | cout< 2 | using namespace std; 3 | #define MAX 10 4 | 5 | bool isSafe(bool vis[MAX][MAX],int r,int c,int n) 6 | { 7 | for(int i=0;i=0 && i=0 && j>=0;i--,j--) 14 | if(vis[i][j]) 15 | return false; 16 | return true; 17 | } 18 | 19 | void nQueen(bool vis[MAX][MAX],int n,int c) 20 | { 21 | if(c==n) 22 | { 23 | for(int i=0;i>t; 48 | while(t--) 49 | { 50 | int n; 51 | cin>>n; 52 | bool vis[MAX][MAX]; 53 | 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 a,b,c; 11 | cin>>a>>b>>c; 12 | if(a.size()+b.size() != c.size()) 13 | { 14 | cout<<"0\n"; 15 | continue; 16 | } 17 | bool dp[a.size()+1][b.size()+1]; 18 | memset(dp,0,sizeof(dp)); 19 | for(int i=0;i<=a.size();i++) 20 | { 21 | for(int j=0;j<=b.size();j++) 22 | { 23 | if(i==0 && j==0) 24 | dp[i][j] = 1; 25 | else if(i==0) 26 | { 27 | if(b[j-1]==c[j-1]) 28 | dp[i][j] = dp[i][j-1]; 29 | } 30 | else if(j==0) 31 | { 32 | if(a[i-1]==c[i-1]) 33 | dp[i][j] = dp[i-1][j]; 34 | } 35 | else if(a[i-1]==c[i+j-1] && b[j-1]!=c[i+j-1]) 36 | dp[i][j] = dp[i-1][j]; 37 | else if(a[i-1]!=c[i+j-1] && b[j-1]==c[i+j-1]) 38 | dp[i][j] = dp[i][j-1]; 39 | else if(a[i-1]==c[i+j-1] && b[j-1]==c[i+j-1]) 40 | dp[i][j] = dp[i-1][j]||dp[i][j-1]; 41 | } 42 | } 43 | cout< 7 | #include 8 | using namespace std; 9 | 10 | bool isSubseq(string orig,string small) 11 | { 12 | if(small.size()>orig.size()) 13 | return false; 14 | int j = 0; 15 | for(int i=0;i>t; 28 | while(t--) 29 | { 30 | int n; 31 | cin>>n; 32 | vector dict; 33 | for(int i=0;i>x; 37 | dict.push_back(x); 38 | } 39 | string str; 40 | cin>>str; 41 | string ans = ""; 42 | for(string x : dict) 43 | { 44 | if(isSubseq(str,x)) 45 | ans = ans.size()<(x).size()?x:ans; 46 | } 47 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | string x; 10 | cin>>x; 11 | int n = x.size(); 12 | bool isPal[n][n]; 13 | for(int i=0;i 2 | using namespace std; 3 | 4 | void to_heap(int arr[],int n) 5 | { 6 | for(int i=(n/2)-1;i>=0;i--) 7 | heapify_down(arr,n,i); 8 | 9 | } 10 | 11 | void heapify_up(int arr[],int n) 12 | { 13 | int i = n-1; 14 | int parent = (i-1)/2; 15 | while(i!=0 && arr[parent]=n || arr[i]>=arr[child1]) && (child2>=n || childarr[i]>=arr[child2])) 30 | break; 31 | int swapper = (child2arr[child2]?child1:child2):child1; 32 | swap(arr[i],arr[swapper]); 33 | i = swapper; 34 | } 35 | } 36 | 37 | void push(int arr[],int &n,int x) 38 | { 39 | arr[n]=x; 40 | n++; 41 | } 42 | 43 | int pop(int arr[],int &n) 44 | { 45 | int _out=a[0]; 46 | swap(a[0],a[n-1]); 47 | n--; 48 | heapify_down(arr,n); 49 | return _out; 50 | } 51 | 52 | 53 | int main() 54 | { 55 | int n; 56 | cin>>n; //size of heap 57 | int a[n]; // array to heapify 58 | for(int i=0;i>a[i]; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /ds/arrays/inversion-count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | long merge(long arr[],long l,long mid,long r,long sorted[]) 6 | { 7 | long i=l,j=mid+1,k=l; 8 | long count = 0; 9 | while(i<=mid && j<=r) 10 | { 11 | if(arr[i]<=arr[j]) 12 | sorted[k++]=arr[i++]; 13 | else 14 | { 15 | sorted[k++]=arr[j++]; 16 | count+=(mid-i+1); 17 | } 18 | } 19 | while(i<=mid) 20 | sorted[k++]=arr[i++]; 21 | while(j<=r) 22 | sorted[k++]=arr[j++]; 23 | for(long i=l;i<=r;i++) 24 | arr[i]=sorted[i]; 25 | return count; 26 | } 27 | 28 | long mergesort(long arr[],long l,long r,long sorted[]) 29 | { 30 | long count = 0; 31 | if(l>t; 52 | while(t--) 53 | { 54 | long n; 55 | cin>>n; 56 | long a[n]; 57 | for(long i=0;i>a[i]; 58 | long ans = inversion(a,n); 59 | cout< 2 | #include 3 | using namespace std; 4 | #define MAX 100 5 | 6 | vector sol; 7 | int d[4][2]={{1,0},{0,-1},{0,1},{-1,0}}; 8 | string dir="DLRU"; 9 | 10 | bool isSafe(int m[MAX][MAX],int n,int i,int j) 11 | { 12 | return (i>=0 && i=0 && j printPath(int m[MAX][MAX], int n) 38 | { 39 | return sol; 40 | } 41 | 42 | int main() { 43 | int t; 44 | cin>>t; 45 | while(t--){ 46 | int n; 47 | cin>>n; 48 | 49 | int m[100][100]; 50 | for(int i=0;i>m[i][j]; 53 | 54 | sol.clear(); 55 | string x = ""; 56 | solve(m,n,0,0,x); 57 | for(int i=0;i 2 | using namespace std; 3 | //Position this line where user code will be pasted. 4 | int main() 5 | { 6 | int T; 7 | cin>>T; 8 | while(T--) 9 | { 10 | 11 | int N, E; 12 | cin>>N>>E; 13 | vector adj[N]; 14 | bool vis[N] = {false}; 15 | for(int i=0;i>u>>v; 19 | adj[u].push_back(v); 20 | } 21 | 22 | bfs(0, adj, vis, N); 23 | cout< adj[], bool vis[], int N) 34 | { 35 | queue q; 36 | q.push(s); 37 | vis[s]=true; 38 | while(!q.empty()) 39 | { 40 | int top = q.front(); 41 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int* topoSort(int V, vector adj[]) 7 | { 8 | bool vis[V]={false}; 9 | vector indegree(V,0); 10 | for(int i=0;i q; 15 | for(int i=0;i>T; 47 | while(T--) 48 | { 49 | 50 | int N, E; 51 | cin>>N>>E; 52 | vector adj[N]; 53 | bool vis[N] = {false}; 54 | for(int i=0;i>u>>v; 58 | adj[u].push_back(v); 59 | } 60 | 61 | int* topoSorted = topoSort(N, adj); 62 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define INF 1000000 8 | 9 | typedef pair Pair; 10 | 11 | 12 | int main() { 13 | vector dist(10001, INF); 14 | vector> adj(10001); 15 | for(int i=1;i<=10000;i++) 16 | { 17 | if(3*i<=10000) 18 | adj[i].push_back(3*i); 19 | if(i+1<=10000) 20 | adj[i].push_back(i+1); 21 | } 22 | 23 | priority_queue,greater> pq; 24 | pq.push({0,1}); 25 | 26 | dist[1] = 0; 27 | bool vis[10001] = {false}; 28 | while(!pq.empty()) 29 | { 30 | Pair top = pq.top(); 31 | pq.pop(); 32 | if(vis[top.second]) 33 | continue; 34 | vis[top.second] = true; 35 | 36 | for(int i=0;i (dist[top.second] + 1)) 42 | { 43 | dist[node] = dist[top.second] + 1; 44 | pq.push({dist[node],node}); 45 | } 46 | } 47 | } 48 | } 49 | int t; 50 | cin>>t; 51 | while(t--) 52 | { 53 | int n; 54 | cin>>n; 55 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | //Method2 --> using 2 pointers 7 | long solve2(int arr[],int n) 8 | { 9 | long long ans = 0; 10 | sort(arr,arr+n); 11 | for(int i = n-1;i>=0;i--) 12 | { 13 | int j=i-1,k=0; 14 | while(k Using Hash Function 32 | //Not include here : case for 0 + 0 = 0 and 0 + x = x 33 | long solve1(int arr[],int n) 34 | { 35 | int hash[1000001]={0}; 36 | for(int i=0;i>t; 51 | while(t--) 52 | { 53 | int n; 54 | cin>>n; 55 | int a[n]; 56 | for(int i=0;i>a[i]; 58 | long ans = solve2(a,n); 59 | if(ans==0) 60 | cout<<"-1\n"; 61 | else 62 | cout< 8 | using namespace std; 9 | #define MAX 501 10 | int getId(int M[MAX][MAX],int n); 11 | int main() 12 | { 13 | int T; 14 | cin>>T; 15 | int M[MAX][MAX]; 16 | while(T--) 17 | { 18 | int N; 19 | cin>>N; 20 | memset(M,0,sizeof M); 21 | for(int i=0;i>M[i][j]; 26 | } 27 | } 28 | cout< 2 | using namespace std; 3 | //Position this line where user code will be pasted. 4 | int main() 5 | { 6 | int T; 7 | cin>>T; 8 | while(T--) 9 | { 10 | int N, E; 11 | cin>>N>>E; 12 | 13 | vector g[N]; 14 | int arrival[N]; 15 | int dept[N]; 16 | 17 | memset(vis, false, sizeof(vis)); 18 | 19 | for(int i=0;i>u>>v; 23 | g[u].push_back(v); 24 | g[v].push_back(u); 25 | } 26 | for(int i=0;i g[], bool vis[],int &time) 46 | { 47 | arrival[s]=time++; 48 | cout< 3 | using namespace std; 4 | //Position this line where user code will be pasted. 5 | int main() { 6 | 7 | int t; 8 | cin >> t; 9 | 10 | while(t--){ 11 | 12 | int v, e; 13 | cin >> v >> e; 14 | 15 | vector adj[v]; 16 | 17 | for(int i =0;i> u >> v; 20 | adj[u].push_back(v); 21 | } 22 | 23 | cout << isCyclic(v, adj) << endl; 24 | 25 | } 26 | 27 | return 0; 28 | } 29 | } 30 | /*This is a function problem.You only need to complete the function given below*/ 31 | /* Function to check if the given graph contains cycle 32 | * V: number of vertices 33 | * adj[]: representation of graph 34 | */ 35 | 36 | void isCyclicUtil(vector adj[],int vis[],int i,bool &hasCycle) 37 | { 38 | if(hasCycle) 39 | return ; 40 | vis[i] = 1; 41 | for(int j = 0; j adj[]) 56 | { 57 | int vis[V]={0}; 58 | bool ans = false; 59 | for(int i=0;i 2 | using namespace std; 3 | 4 | class QueueStack{ 5 | private: 6 | queue q1; 7 | queue q2; 8 | public: 9 | void push(int); 10 | int pop(); 11 | }; 12 | 13 | int main() 14 | { 15 | int T; 16 | cin>>T; 17 | while(T--) 18 | { 19 | QueueStack *qs = new QueueStack(); 20 | int Q; 21 | cin>>Q; 22 | while(Q--){ 23 | int QueryType=0; 24 | cin>>QueryType; 25 | if(QueryType==1) 26 | { 27 | int a; 28 | cin>>a; 29 | qs->push(a); 30 | } 31 | else if(QueryType==2) 32 | cout<pop()<<" "; 33 | cout<<"\n"; 34 | } 35 | } 36 | 37 | /*This is a function problem.You only need to complete the function given below*/ 38 | /* The structure of the class is 39 | class QueueStack{ 40 | private: 41 | queue q1; 42 | queue q2; 43 | public: 44 | void push(int); 45 | int pop(); 46 | }; 47 | */ 48 | /* The method push to push element into the stack */ 49 | void QueueStack :: push(int x) 50 | { 51 | q1.push(x); 52 | } 53 | /*The method pop which return the element poped out of the stack*/ 54 | int QueueStack :: pop() 55 | { 56 | if(q1.size()==0) 57 | return -1; 58 | while(q1.size()>1) 59 | { 60 | q2.push(q1.front()); 61 | q1.pop(); 62 | } 63 | int x = q1.front(); 64 | q1.pop(); 65 | swap(q1,q2); 66 | return x; 67 | } 68 | -------------------------------------------------------------------------------- /ds/stacks-and-queues/the-celebrity-problem.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * You are in a party of N people,where only one person is known to everyone. Such a person may be present in the party 4 | * if yes, (s)he doesn’t know anyone in the party. Your task is to find the stranger (celebrity) in party. 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | #define MAX 501 10 | int getId(int M[MAX][MAX],int n); 11 | int main() 12 | { 13 | int T; 14 | cin>>T; 15 | int M[MAX][MAX]; 16 | while(T--) 17 | { 18 | int N; 19 | cin>>N; 20 | memset(M,0,sizeof M); 21 | for(int i=0;i>M[i][j]; 26 | } 27 | } 28 | cout< s; 39 | for(int i=0;i1) 42 | { 43 | int x = s.top(); 44 | s.pop(); 45 | int y = s.top(); 46 | s.pop(); 47 | if(m[x][y]) 48 | s.push(y); 49 | else 50 | s.push(x); 51 | } 52 | int ans=s.top(); 53 | s.pop(); 54 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int a[n],leftMin[n],rightMin[n],leftMax[n],rightMax[n]; 12 | for(int i=0;i>a[i]; 14 | int min_sum = 1001, min_sum_till = 1001; 15 | for(int i=0;i=0;i--) 24 | { 25 | min_sum_till = min(min_sum_till+a[i],a[i]); 26 | min_sum = min(min_sum,min_sum_till); 27 | rightMin[i] = min_sum; 28 | } 29 | int max_sum = -1001, max_sum_till = -1001; 30 | for(int i=0;i=0;i--) 39 | { 40 | max_sum_till = max(max_sum_till+a[i],a[i]); 41 | max_sum = max(max_sum,max_sum_till); 42 | rightMax[i] = max_sum; 43 | } 44 | int ans = 0; 45 | for(int i = 0;i 2 | using namespace std; 3 | class StackQueue{ 4 | private: 5 | stack s1; 6 | stack s2; 7 | public: 8 | void push(int B); 9 | int pop(); 10 | }; 11 | 12 | int main() 13 | { 14 | int T; 15 | cin>>T; 16 | while(T--) 17 | { 18 | StackQueue *sq = new StackQueue(); 19 | int Q; 20 | cin>>Q; 21 | while(Q--){ 22 | int QueryType=0; 23 | cin>>QueryType; 24 | if(QueryType==1) 25 | { 26 | int a; 27 | cin>>a; 28 | sq->push(a); 29 | } 30 | else if(QueryType==2) 31 | cout<pop()<<" "; 32 | } 33 | cout<<"\n"; 34 | } 35 | } 36 | 37 | /*This is a function problem.You only need to complete the function given below*/ 38 | /* The structure of the class is 39 | class StackQueue{ 40 | private: 41 | // These are STL stacks ( http://goo.gl/LxlRZQ ) 42 | stack s1; 43 | stack s2; 44 | public: 45 | void push(int); 46 | int pop(); 47 | }; */ 48 | /* The method push to push element into the queue */ 49 | void StackQueue :: push(int x) 50 | { 51 | s1.push(x); 52 | } 53 | /*The method pop which return the element poped out of the queue*/ 54 | int StackQueue :: pop() 55 | { 56 | if(s2.empty()) 57 | { 58 | while(!s1.empty()) 59 | { 60 | s2.push(s1.top()); 61 | s1.pop(); 62 | } 63 | } 64 | if(s2.empty()) 65 | return -1; 66 | int x = s2.top(); 67 | s2.pop(); 68 | return x; 69 | } 70 | -------------------------------------------------------------------------------- /ds/heap/median-of-running-stream.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 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 | priority_queue maxHeap; 14 | priority_queue,greater> minHeap; 15 | while(n--) 16 | { 17 | int x; 18 | cin>>x; 19 | int s1 = minHeap.size(); 20 | int s2 = maxHeap.size(); 21 | if(s2==s1) 22 | { 23 | int effective_median = maxHeap.top()+minHeap.top(); 24 | effective_median/=2; 25 | if(xs1) 31 | { 32 | int effective_median = maxHeap.top(); 33 | if(xeffective_median) 46 | { 47 | maxHeap.push(minHeap.top()); 48 | minHeap.pop(); 49 | minHeap.push(x); 50 | } 51 | else 52 | maxHeap.push(x); 53 | } 54 | s1 = minHeap.size(); 55 | s2 = maxHeap.size(); 56 | if(s1==s2) 57 | cout<<(maxHeap.top()+minHeap.top())/2.0<<"\n"; 58 | else if(s1>s2) 59 | cout< 3 | using namespace std; 4 | //Position this line where user code will be pasted. 5 | int main() 6 | { 7 | int T; 8 | cin>>T; 9 | while(T--) 10 | { 11 | int V, E; 12 | cin>>V>>E; 13 | 14 | // array of vectors to represent graph 15 | vector adj[V]; 16 | 17 | int u, v; 18 | for(int i=0;i>u>>v; 21 | 22 | // adding edge to the graph 23 | adj[u].push_back(v); 24 | adj[v].push_back(u); 25 | } 26 | cout << isCyclic(adj, V)< adj[],int vis[],int i,int parent,bool &hasCycle) 37 | { 38 | if(hasCycle) 39 | return ; 40 | vis[i] = 1; 41 | for(int j = 0; j adj[], int V) 56 | { 57 | int vis[V]={0}; 58 | bool ans = false; 59 | for(int i=0;i 2 | using namespace std; 3 | 4 | int brute_force(string text,string pat) 5 | { 6 | int n = text.size(); 7 | int m = pat.size(); 8 | 9 | for(int i=0;i<=n-m;i++) 10 | { 11 | bool flag = true; 12 | for(int j=0;j>t; 85 | while(t--) 86 | { 87 | string text,pattern; 88 | cin>>text>>pattern; 89 | cout< 2 | using namespace std; 3 | 4 | int sudoku[9][9]; 5 | 6 | bool isSafe(int r,int c,int val) 7 | { 8 | for(int i=0;i<9;i++) 9 | if(sudoku[i][c]==val) 10 | return false; 11 | for(int i=0;i<9;i++) 12 | if(sudoku[r][i]==val) 13 | return false; 14 | int br = r/3; 15 | int bc = c/3; 16 | for(int i=br*3;i<(br+1)*3;i++) 17 | for(int j=bc*3;j<(bc+1)*3;j++) 18 | if(sudoku[i][j]==val) 19 | return false; 20 | return true; 21 | } 22 | 23 | bool solve() 24 | { 25 | for(int i=0;i<9;i++) 26 | { 27 | for(int j=0;j<9;j++) 28 | { 29 | if(sudoku[i][j]==0) 30 | { 31 | for(int num=1;num<=9;num++) 32 | { 33 | if(isSafe(i,j,num)) 34 | { 35 | sudoku[i][j]=num; 36 | if(solve()) 37 | return true; 38 | else 39 | sudoku[i][j]=0; 40 | } 41 | } 42 | return false; 43 | } 44 | } 45 | } 46 | return true; 47 | } 48 | 49 | int main() { 50 | int t; 51 | cin>>t; 52 | while(t--) 53 | { 54 | for(int i=0;i<9;i++) 55 | for(int j=0;j<9;j++) 56 | cin>>sudoku[i][j]; 57 | solve(); 58 | for(int i=0;i<9;i++) 59 | { 60 | for(int j=0;j<9;j++) 61 | cout< 2 | #include 3 | using namespace std; 4 | 5 | string add(string x,string y) 6 | { 7 | reverse(x.begin(),x.end()); 8 | reverse(y.begin(),y.end()); 9 | int c = 0; 10 | string ans = ""; 11 | for(int i = 0;i0) 30 | { 31 | ans+=to_string(c); 32 | } 33 | reverse(ans.begin(),ans.end()); 34 | int nz_i = 0; 35 | for(int i=0;i>t; 51 | while(t--) 52 | { 53 | int n; 54 | cin>>n; 55 | int a[n]; 56 | string n1="",n2=""; 57 | for(int i=0;i>a[i]; 58 | sort(a,a+n); 59 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int n,k; 13 | cin>>n>>k; 14 | int a[n]; 15 | int sum[n]={0}; 16 | for(int i=0;i>a[i]; 18 | sum[0]=a[0]; 19 | for(int i=1;i (maximum - minimum)) 47 | { 48 | dp[i][j] = maximum - minimum; 49 | maxP[i][j] = maximum; 50 | minP[i][j] = minimum; 51 | } 52 | } 53 | } 54 | } 55 | /* 56 | for(int i=1;i<=k;i++) // number of partitions 57 | { 58 | for(int j=1;j<=n;j++) // number of boards to paint 59 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Node 7 | { 8 | public: 9 | char ch; 10 | int freq; 11 | Node *left,*right; 12 | Node(int freq,char ch,Node* left=NULL,Node* right=NULL) 13 | { 14 | this->ch = ch; 15 | this->freq = freq; 16 | this->left = left; 17 | this->right = right; 18 | } 19 | }; 20 | 21 | class comp 22 | { 23 | public: 24 | bool operator()(Node* x,Node* y) 25 | { 26 | return x->freq > y->freq; 27 | } 28 | }; 29 | 30 | void encode(Node* root,string code,string codes[]) 31 | { 32 | if(root->ch != '\0') 33 | { 34 | codes[root->ch - 'a'] = code; 35 | return ; 36 | } 37 | encode(root->left,code+"0",codes); 38 | encode(root->right,code+"1",codes); 39 | } 40 | 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while(t--) 46 | { 47 | string x; 48 | cin>>x; //x --> String to be Huffman Encoded 49 | priority_queue,comp> minHeap; 50 | int count[26]={0}; 51 | for(int i=0;i1) 56 | { 57 | Node* left = minHeap.top(); 58 | minHeap.pop(); 59 | Node* right = minHeap.top(); 60 | minHeap.pop(); 61 | minHeap.push(new Node(left->freq + right->freq,'\0',left,right)); 62 | } 63 | Node* root = minHeap.top(); //root ---> root of Huffman Tree 64 | 65 | string code[26]; 66 | encode(root,"",code); 67 | 68 | for(int i=0;i<26;i++) 69 | cout<<(char)(i+'a')<<" "< 2 | using namespace std; 3 | 4 | int getLPS(string x) 5 | { 6 | int n = x.size(); 7 | if(n==0) 8 | return 0; 9 | n = 2*n + 1; 10 | int lps[n]={0}; 11 | lps[0]=0; 12 | lps[1]=1; 13 | int center = 1; // center 14 | int r = 2; // Centered Right 15 | int i = 0; //Current Right 16 | int iMirror = 0; //current Left 17 | int expand = -1; 18 | int diff = -1; 19 | int maxLPS = 0; 20 | int maxLPSCenter = 0; 21 | int start = -1; 22 | int end = -1; 23 | for(int i=2;i0) 29 | { 30 | if(lps[iMirror]diff) 40 | { 41 | lps[i] = diff; 42 | expand = 1; 43 | } 44 | } 45 | else 46 | { 47 | lps[i]=0; 48 | expand = 1; 49 | } 50 | if(expand) 51 | { 52 | while(i+lps[i]=0 && (((i + lps[i] + 1) % 2 == 0) || (x[(i+lps[i]+1)/2]==x[(i-L[i]-1)/2]) )) 53 | lps[i]++; 54 | } 55 | if(lps[i]>maxLPS) 56 | { 57 | maxLPS = lps[i]; 58 | maxLPSCenter = i; 59 | } 60 | if(i + lps[i]>r) 61 | { 62 | r = i + lps[i]; 63 | center = i; 64 | } 65 | } 66 | } 67 | 68 | int main() 69 | { 70 | int t; 71 | cin>>t; 72 | while(t--) 73 | { 74 | string x; 75 | cin>>x; 76 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n,sum; 10 | cin>>n>>sum; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 14 | unordered_map>> val; 15 | int k=0; 16 | for(int i=0;i> res; 24 | for(const auto &x : val) 25 | { 26 | const auto &it = val.find(sum-x.first); 27 | if(it!=val.end()) 28 | { 29 | for(const auto &z : it->second) 30 | { 31 | for(const auto &v : x.second) 32 | { 33 | set ans; 34 | ans.insert(z.first); 35 | ans.insert(v.first); 36 | ans.insert(z.second); 37 | ans.insert(v.second); 38 | if(ans.size() == 4) 39 | { 40 | vector zzz; 41 | for(const auto &c : ans) 42 | zzz.push_back(a[c]); 43 | sort(zzz.begin(),zzz.end()); 44 | res.insert(zzz); 45 | } 46 | } 47 | } 48 | } 49 | } 50 | for(const auto &answer : res) 51 | { 52 | for(const auto &values : answer) 53 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string x; 11 | cin>>x; 12 | int curr_max = 0; 13 | int last_entry = 0; 14 | string decrypt=""; 15 | int n = x.size(); 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int key; 7 | struct Node *left; 8 | struct Node *right; 9 | Node(int x){ 10 | key = x; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | 16 | void insert(Node *root,int a1,int a2,char lr){ 17 | if(root==NULL) 18 | return; 19 | if(root->key==a1){ 20 | switch(lr){ 21 | case 'L':root->left=new Node(a2); 22 | break; 23 | case 'R':root->right=new Node(a2); 24 | break; 25 | } 26 | } 27 | else{ 28 | insert(root->left,a1,a2,lr); 29 | insert(root->right,a1,a2,lr); 30 | } 31 | } 32 | 33 | bool isFullTree (struct Node* root); 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--){ 40 | int n; 41 | cin>>n; 42 | struct Node* root = NULL; 43 | while(n-->0){ 44 | int a1,a2; 45 | char lr; 46 | cin>>a1>>a2>>lr; 47 | if(root==NULL){ 48 | root=new Node(a1); 49 | switch(lr){ 50 | case 'L':root->left=new Node(a2); 51 | break; 52 | case 'R':root->right=new Node(a2); 53 | break; 54 | } 55 | } 56 | else{ 57 | insert(root,a1,a2,lr); 58 | } 59 | } 60 | if (isFullTree(root)) 61 | cout<<"1"; 62 | else 63 | cout<<"0"; 64 | } 65 | return 0; 66 | } 67 | /*This is a function problem.You only need to complete the function given below*/ 68 | /*Complete the function below 69 | Node is as follows: 70 | struct Node 71 | { 72 | int key; 73 | struct Node *left, *right; 74 | }; 75 | */ 76 | bool isFullTree (struct Node* root) 77 | { 78 | if(root==NULL) 79 | return true; 80 | if(root->left==NULL && root->right!=NULL) 81 | return false; 82 | if(root->left!=NULL && root->right==NULL) 83 | return false; 84 | return isFullTree(root->left) && isFullTree(root->right); 85 | } 86 | -------------------------------------------------------------------------------- /algo/backtracking/word-boggle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define MAX 10 5 | 6 | map dict; 7 | bool vis[MAX][MAX]; 8 | char word[MAX][MAX]; 9 | int d[8][2]={{1,0},{0,-1},{0,1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; 10 | 11 | bool isSafe(int m,int n,int i,int j) 12 | { 13 | return (i>=0 && i=0 && j=longest) 19 | return; 20 | curr+=word[r][c]; 21 | vis[r][c]=true; 22 | if(dict.find(curr)!=dict.end()) 23 | dict[curr]=2; 24 | for(int i=0;i<=7;i++) 25 | { 26 | int x = r + d[i][0]; 27 | int y = c + d[i][1]; 28 | if(isSafe(m,n,x,y)) 29 | solve(x,y,m,n,longest,curr); 30 | } 31 | curr.pop_back(); 32 | vis[r][c]=false; 33 | } 34 | 35 | int main() { 36 | int t; 37 | cin>>t; 38 | while(t--) 39 | { 40 | dict.clear(); 41 | int m,n; 42 | cin>>n; 43 | int maxlen=0; 44 | while(n--) 45 | { 46 | string x; 47 | cin>>x; 48 | dict[x]=1; 49 | maxlen = max(maxlen,(int)x.size()); 50 | } 51 | cin>>m>>n; 52 | for(int i=0;i>word[i][j]; 57 | vis[i][j]=false; 58 | } 59 | } 60 | 61 | string w = ""; 62 | for(int i=0;i::iterator it = dict.begin(); 66 | bool flag = false; 67 | for(;it!=dict.end();++it) 68 | { 69 | if(it->second == 2) 70 | { 71 | flag = true; 72 | cout<first<<" "; 73 | } 74 | } 75 | if(!flag) 76 | cout<<"-1"; 77 | cout<<"\n"; 78 | } 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /algo/dynamic-programming/count-total-possible-combinations-n-digit-numbers-mobile-keypad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isValid(int x,int y) 5 | { 6 | char keypad[4][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}}; 7 | return (x>=0 && x<4 && y>=0 && y<3 && keypad[x][y]!='*' && keypad[x][y]!='#'); 8 | } 9 | 10 | int main() { 11 | int t; 12 | cin>>t; 13 | char keypad[4][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}}; 14 | int d[5][2] = {{0,0},{0,1},{0,-1},{1,0},{-1,0}}; 15 | while(t--) 16 | { 17 | int n; 18 | cin>>n; 19 | if(n==1 || n==0) 20 | { 21 | cout< 2 | using namespace std; 3 | 4 | template 5 | class Trie 6 | { 7 | class Node 8 | { 9 | public: 10 | int endFor; 11 | Node* next[26]; 12 | Node() 13 | { 14 | for(int i=0;i<26;i++) 15 | next[i]=NULL; 16 | endFor = 0; 17 | } 18 | }; 19 | Node* root; 20 | public: 21 | Trie() 22 | { 23 | root = new Node(); 24 | } 25 | void insert(string x) 26 | { 27 | Node* temp = root; 28 | for(int i=0;inext[x[i]-'a']==NULL) 31 | temp->next[x[i]-'a'] = new Node(); 32 | temp = temp->next[x[i]-'a']; 33 | } 34 | temp->endFor++; 35 | } 36 | bool search(string x) 37 | { 38 | Node* temp = root; 39 | for(int i=0;inext[x[i]]-'a')==NULL) 42 | return false; 43 | temp = temp->next[x[i]-'a']; 44 | } 45 | if(temp->endFor > 0) 46 | return true; 47 | else 48 | return false; 49 | } 50 | 51 | bool hasChild(Node* head) 52 | { 53 | for(int i=0;i<26;i++) 54 | if(head->next[i]!=NULL) 55 | return true; 56 | return false; 57 | } 58 | 59 | bool deleter(string x,int i,Node* head) 60 | { 61 | if(i==x.size() && head->endFor > 0) 62 | { 63 | head->endFor = 0; 64 | if(hasChild(head)) 65 | return false; 66 | delete head; 67 | head = NULL; 68 | return true; 69 | } 70 | else 71 | { 72 | if(head != NULL && head->next[x[i]-'a']!=NULL && deleter(x,i+1,head->next[x[i]-'a']) && temp->endFor == 0) 73 | { 74 | if(hasChild(head)) 75 | return false; 76 | delete head; 77 | head = NULL; 78 | return true; 79 | } 80 | } 81 | return false; 82 | } 83 | void delete(string x) 84 | { 85 | deleter(x,0,root); 86 | } 87 | 88 | 89 | }; 90 | 91 | int main() 92 | { 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /ds/trees/bst-nodes-in-given-range.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node { 4 | int data; 5 | Node* right; 6 | Node* left; 7 | 8 | Node(int x){ 9 | data = x; 10 | right = NULL; 11 | left = NULL; 12 | } 13 | }; 14 | Node *insert(Node *root,int data) 15 | { 16 | if(root==NULL) 17 | root=new Node(data); 18 | else if(datadata) 19 | root->left=insert(root->left,data); 20 | else 21 | root->right=insert(root->right,data); 22 | 23 | return root; 24 | } 25 | //Position this line where user code will be pasted. 26 | int main() { 27 | 28 | int testcases; 29 | cin>>testcases; 30 | while(testcases--) 31 | { 32 | Node *root=NULL; 33 | int sizeOfArray; 34 | cin>>sizeOfArray; 35 | int arr[sizeOfArray]; 36 | 37 | for(int i=0;i>arr[i]; 39 | 40 | for(int i=0;i>l>>h; 46 | cout<data == l && root->data==h) 73 | return 1; 74 | else if(root->data>=l && root->data<=h) 75 | return 1 + getCountOfNode(root->left,l,h) + getCountOfNode(root->right,l,h); 76 | else if(root->dataright,l,h); 78 | else 79 | return getCountOfNode(root->left,l,h); 80 | } 81 | -------------------------------------------------------------------------------- /ds/stacks-and-queues/stack-with-min-O(1)-time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class _stack{ 5 | stack s; 6 | int minEle; 7 | public : 8 | int getMin(); 9 | int pop(); 10 | void push(int); 11 | }; 12 | int main() 13 | { 14 | int t; 15 | cin>>t; 16 | while(t--) 17 | { 18 | int q; 19 | cin>>q; 20 | _stack *a = new _stack(); 21 | while(q--){ 22 | int qt; 23 | cin>>qt; 24 | if(qt==1) 25 | { 26 | //push 27 | int att; 28 | cin>>att; 29 | a->push(att); 30 | } 31 | else if(qt==2) 32 | { 33 | //pop 34 | cout<pop()<<" "; 35 | } 36 | else if(qt==3) 37 | { 38 | //getMin 39 | cout<getMin()<<" "; 40 | } 41 | } 42 | cout< s; 51 | int minEle; 52 | public : 53 | int getMin(); 54 | int pop(); 55 | void push(int); 56 | }; 57 | */ 58 | /*returns min element from stack*/ 59 | int _stack :: getMin() 60 | { 61 | if(s.empty()) 62 | return -1; 63 | return minEle; 64 | //Your code here 65 | } 66 | /*returns poped element from stack*/ 67 | int _stack ::pop() 68 | { 69 | if(s.empty()) 70 | return -1; 71 | int x; 72 | if(s.top()=minEle) 91 | s.push(x); 92 | else 93 | { 94 | s.push(2*x - minEle); 95 | minEle = x; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /algo/dynamic-programming/boolean-parenthesis-to-make-expression-true.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | long long t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | long long n; 12 | cin>>n; 13 | string x; 14 | cin>>x; 15 | cout< val; 17 | vector symbol; 18 | for(long long i=0;i 2 | using namespace std; 3 | /* A binary tree node has data, pointer to left child 4 | and a pointer to right child */ 5 | struct Node 6 | { 7 | int data; 8 | struct Node *left; 9 | struct Node *right; 10 | 11 | Node(int x){ 12 | data = x; 13 | left = NULL; 14 | right = NULL; 15 | } 16 | }; 17 | int minDepth(Node *node); 18 | /* Driver program to test size function*/ 19 | int main() 20 | { 21 | int t; 22 | struct Node *child; 23 | scanf("%d", &t); 24 | while (t--) 25 | { 26 | map m; 27 | int n; 28 | scanf("%d",&n); 29 | struct Node *root = NULL; 30 | while (n--) 31 | { 32 | Node *parent; 33 | char lr; 34 | int n1, n2; 35 | scanf("%d %d %c", &n1, &n2, &lr); 36 | if (m.find(n1) == m.end()) 37 | { 38 | parent = new Node(n1); 39 | m[n1] = parent; 40 | if (root == NULL) 41 | root = parent; 42 | } 43 | else 44 | parent = m[n1]; 45 | child = new Node(n2); 46 | if (lr == 'L') 47 | parent->left = child; 48 | else 49 | parent->right = child; 50 | m[n2] = child; 51 | } 52 | cout< q; 69 | if(root != NULL) 70 | q.push(root); 71 | int d=0; 72 | while(!q.empty()) 73 | { 74 | d++; 75 | int l = q.size(); 76 | while(l--) 77 | { 78 | Node* node = q.front(); 79 | q.pop(); 80 | if(node->left == NULL && node->right==NULL) 81 | return d; 82 | if(node->left) 83 | q.push(node->left); 84 | if(node->right) 85 | q.push(node->right); 86 | } 87 | } 88 | return d; 89 | } 90 | -------------------------------------------------------------------------------- /ds/trees/max-sum-path-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* A binary tree node has data, pointer to left child 4 | and a pointer to right child */ 5 | struct Node 6 | { 7 | int data; 8 | struct Node *left; 9 | struct Node *right; 10 | 11 | Node(int x){ 12 | data = x; 13 | left = NULL; 14 | right = NULL; 15 | } 16 | }; 17 | int maxSumPath(Node *node); 18 | /* Driver program to test size function*/ 19 | int main() 20 | { 21 | int t; 22 | struct Node *child; 23 | scanf("%d", &t); 24 | while (t--) 25 | { 26 | map m; 27 | int n; 28 | scanf("%d",&n); 29 | struct Node *root = NULL; 30 | while (n--) 31 | { 32 | Node *parent; 33 | char lr; 34 | int n1, n2; 35 | scanf("%d %d %c", &n1, &n2, &lr); 36 | if (m.find(n1) == m.end()) 37 | { 38 | parent = new Node(n1); 39 | m[n1] = parent; 40 | if (root == NULL) 41 | root = parent; 42 | } 43 | else 44 | parent = m[n1]; 45 | child = new Node(n2); 46 | if (lr == 'L') 47 | parent->left = child; 48 | else 49 | parent->right = child; 50 | m[n2] = child; 51 | } 52 | cout<left,sum); 72 | int r = maxSum(root->right,sum); 73 | 74 | // Case Where I take left tree + current Node + Right Tree path 75 | int max_turning_here = l + r + root->data; 76 | // Case where I travel through current Node upwards 77 | int max_through_here = max(max(l,r) + root->data,root->data); 78 | 79 | sum = max(max_through_here,max_turning_here); 80 | return max_through_here; 81 | } 82 | 83 | int maxSumPath(Node* node) 84 | { 85 | int ans = 0; 86 | maxSum(node,ans); 87 | return ans; 88 | } 89 | -------------------------------------------------------------------------------- /ds/trees/root-to-leaf-paths-sum.cpp: -------------------------------------------------------------------------------- 1 | { 2 | #include 3 | using namespace std; 4 | /* A binary tree node has data, pointer to left child 5 | and a pointer to right child */ 6 | 7 | struct Node 8 | { 9 | int data; 10 | struct Node *left; 11 | struct Node *right; 12 | 13 | Node(int x){ 14 | data = x; 15 | left = NULL; 16 | right = NULL; 17 | } 18 | }; 19 | /* Function to get diameter of a binary tree */ 20 | long long treePathsSum(Node *root); 21 | /* Driver program to test size function*/ 22 | int main() 23 | { 24 | int t; 25 | scanf("%d", &t); 26 | while (t--) 27 | { 28 | map m; 29 | int n; 30 | scanf("%d",&n); 31 | struct Node *root = NULL; 32 | struct Node *child; 33 | while (n--) 34 | { 35 | Node *parent; 36 | char lr; 37 | int n1, n2; 38 | scanf("%d %d %c", &n1, &n2, &lr); 39 | if (m.find(n1) == m.end()) 40 | { 41 | parent = new Node(n1); 42 | m[n1] = parent; 43 | if (root == NULL) 44 | root = parent; 45 | } 46 | else 47 | parent = m[n1]; 48 | child = new Node(n2); 49 | if (lr == 'L') 50 | parent->left = child; 51 | else 52 | parent->right = child; 53 | m[n2] = child; 54 | } 55 | cout << treePathsSum(root) << endl; 56 | } 57 | return 0; 58 | } 59 | 60 | } 61 | /*This is a function problem.You only need to complete the function given below*/ 62 | /* Tree node structure used in the program 63 | struct Node 64 | { 65 | int data; 66 | Node* left, *right; 67 | }; */ 68 | /*You are required to complete below method */ 69 | 70 | void solve(Node* root,long long curr,long long &sum) 71 | { 72 | curr = curr*10 + root->data; 73 | if(root->left!=NULL) 74 | solve(root->left,curr,sum); 75 | else 76 | sum+=curr; 77 | if(root->right!=NULL) 78 | solve(root->right,curr,sum); 79 | else 80 | sum+=curr; 81 | if(root->left==NULL && root->right==NULL) 82 | sum-=curr; 83 | } 84 | 85 | long long treePathsSum(Node *root) 86 | { 87 | long long sum = 0; 88 | solve(root,0,sum); 89 | return sum; 90 | } 91 | -------------------------------------------------------------------------------- /ds/arrays/given-zero-flips-allowed-find-max-consec-zeros.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | //O(N^2) soln --> Brute Force 6 | int solveBrute(int arr[],int n,int m) 7 | { 8 | int ans = 0; 9 | for(int i=0;i Efficient with O(N) space 24 | int solveEfficient(int a[],int n,int m) 25 | { 26 | int left[n]={0},right[n]={0}; 27 | int c=0; 28 | vector z; 29 | for(int i=0;i=0;i--) 42 | { 43 | if(a[i]==0) 44 | { 45 | right[i]=c; 46 | c=0; 47 | } 48 | else 49 | c++; 50 | } 51 | int ans = 0; 52 | for(int i=0;i0?left[z[0]]:-0; 55 | int curr = ans; 56 | for(int i=m;i O(1) space implementation 66 | int solve_efficient(int a[],int n,int m) 67 | { 68 | int wL = 0,wR = 0; 69 | int bestL = 0, bestWindow = 0; 70 | int curr_zeros = 0; 71 | while(wR bestWindow && curr_zeros<=m) 78 | { 79 | bestL = wL; 80 | bestWindow = wR-wL+1; 81 | } 82 | wR++; 83 | } 84 | else 85 | { 86 | if(a[wL]==0) 87 | curr_zeros--; 88 | wL++; 89 | } 90 | } 91 | return bestWindow; 92 | } 93 | 94 | 95 | int main() 96 | { 97 | int t; 98 | cin>>t; 99 | while(t--) 100 | { 101 | int n,m; 102 | cin>>n>>m; // m -> zero flips allowed 103 | int a[n]; 104 | for(int i=0;i>a[i]; 105 | int ans = solve_efficient(a,n,m); 106 | cout< 3 | using namespace std; 4 | // Tree node class 5 | struct Node 6 | { 7 | int data; //data of the node 8 | Node *left, *right; //left and right references 9 | // Constructor of tree node 10 | Node(int key) 11 | { 12 | data = key; 13 | left = right = NULL; 14 | } 15 | }; 16 | // Method that prints the bottom view. 17 | void bottomView(Node *root); 18 | /* Driver program to test size function*/ 19 | int main() 20 | { 21 | int t; 22 | struct Node *child; 23 | scanf("%d", &t); 24 | while (t--) 25 | { 26 | map m; 27 | int n; 28 | scanf("%d",&n); 29 | struct Node *root = NULL; 30 | while (n--) 31 | { 32 | Node *parent; 33 | char lr; 34 | int n1, n2; 35 | scanf("%d %d %c", &n1, &n2, &lr); 36 | if (m.find(n1) == m.end()) 37 | { 38 | parent = new Node(n1); 39 | m[n1] = parent; 40 | if (root == NULL) 41 | root = parent; 42 | } 43 | else 44 | parent = m[n1]; 45 | child = new Node(n2); 46 | if (lr == 'L') 47 | parent->left = child; 48 | else 49 | parent->right = child; 50 | m[n2] = child; 51 | } 52 | bottomView(root); 53 | cout << endl; 54 | } 55 | return 0; 56 | } 57 | /*This is a function problem.You only need to complete the function given below*/ 58 | /* Tree node class 59 | struct Node 60 | { 61 | int data; //data of the node 62 | Node *left, *right; //left and right references 63 | // Constructor of tree node 64 | Node(int key) 65 | { 66 | data = key; 67 | left = right = NULL; 68 | } 69 | }; */ 70 | // Method that prints the bottom view. 71 | void bottomView(Node *root) 72 | { 73 | queue> q; 74 | q.push({root,0}); 75 | map m; 76 | while(!q.empty()) 77 | { 78 | Node* top = q.front().first; 79 | int d = q.front().second; 80 | m[d]= top; 81 | q.pop(); 82 | if(top->left) 83 | q.push({top->left,d-1}); 84 | if(top->right) 85 | q.push({top->right,d+1}); 86 | } 87 | for(auto it=m.begin();it!=m.end();++it) 88 | cout<second->data<<" "; 89 | 90 | } 91 | -------------------------------------------------------------------------------- /ds/trees/check-if-bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* A binary tree node has data, pointer to left child 4 | and a pointer to right child */ 5 | struct Node { 6 | int data; 7 | Node* right; 8 | Node* left; 9 | 10 | Node(int x){ 11 | data = x; 12 | right = NULL; 13 | left = NULL; 14 | } 15 | }; 16 | /* Returns true if the given tree is a binary search tree 17 | (efficient version). */ 18 | bool isBST(struct Node* node); 19 | int isBSTUtil(struct Node* node, int min, int max); 20 | /* Driver program to test size function*/ 21 | int main() 22 | { 23 | int t; 24 | struct Node *child; 25 | scanf("%d 26 | ", &t); 27 | while (t--) 28 | { 29 | map m; 30 | int n; 31 | scanf("%d 32 | ",&n); 33 | struct Node *root = NULL; 34 | while (n--) 35 | { 36 | Node *parent; 37 | char lr; 38 | int n1, n2; 39 | scanf("%d %d %c", &n1, &n2, &lr); 40 | // cout << n1 << " " << n2 << " " << (char)lr << endl; 41 | if (m.find(n1) == m.end()) 42 | { 43 | parent = new Node(n1); 44 | m[n1] = parent; 45 | if (root == NULL) 46 | root = parent; 47 | } 48 | else 49 | parent = m[n1]; 50 | child = new Node(n2); 51 | if (lr == 'L') 52 | parent->left = child; 53 | else 54 | parent->right = child; 55 | m[n2] = child; 56 | } 57 | cout << isBST(root) << endl; 58 | } 59 | return 0; 60 | } 61 | 62 | /*This is a function problem.You only need to complete the function given below*/ 63 | /* A binary tree node has data, pointer to left child 64 | and a pointer to right child 65 | struct Node { 66 | int data; 67 | Node* right; 68 | Node* left; 69 | 70 | Node(int x){ 71 | data = x; 72 | right = NULL; 73 | left = NULL; 74 | } 75 | }; */ 76 | 77 | //Prev is reference so that we can get prev w.r.t parent 78 | int checkBST(Node* root,Node* &prev) 79 | { 80 | if(root!=NULL) 81 | { 82 | if(!checkBST(root->left,prev)) 83 | return false; 84 | if(prev!=NULL && root->data < prev->data) 85 | return false; 86 | prev = root; 87 | if(!checkBST(root->right,prev)) 88 | return false; 89 | } 90 | return true; 91 | } 92 | 93 | bool isBST(Node* root) { 94 | Node* prev = NULL; 95 | return checkBST(root,prev); 96 | } 97 | -------------------------------------------------------------------------------- /ds/graph/given-sorted-dictionary-find-precedence-characters.cpp: -------------------------------------------------------------------------------- 1 | //Initial Template for C++ 2 | /* script for test case http://code.geeksforgeeks.org/PetjYq */ 3 | #include 4 | using namespace std; 5 | string printOrder(string [], int , int ); 6 | string order; 7 | bool f(string a,string b) 8 | { 9 | int p1=0;int p2=0; 10 | for(int i=0;i>t; 28 | while(t--) 29 | { 30 | int n,k; 31 | cin>>n>>k; 32 | string s[n]; 33 | for(int i=0;i>s[i]; 34 | 35 | string ss = printOrder(s,n,k); 36 | order=""; 37 | for(int i=0;i adj[k]; 63 | for(int i=1;i inorder(k,0); 76 | for(int i=0;i q; 81 | for(int i=0;i 4 | using namespace std; 5 | // A Linked List node 6 | struct Node 7 | { 8 | int data; 9 | Node* next; 10 | 11 | Node(int x){ 12 | data = x; 13 | next = NULL; 14 | } 15 | 16 | }; 17 | Node* mergeKLists(Node* arr[], int N); 18 | /* Function to print nodes in a given linked list */ 19 | void printList(Node* node) 20 | { 21 | while (node != NULL) 22 | { 23 | printf("%d ", node->data); 24 | node = node->next; 25 | } 26 | cout<>t; 33 | while(t--) 34 | { 35 | int N; 36 | cin>>N; 37 | struct Node *arr[N]; 38 | for(int j=0;j>n; 42 | int x; 43 | cin>>x; 44 | arr[j]=new Node(x); 45 | Node *curr = arr[j]; 46 | n--; 47 | for(int i=0;i>x; 50 | Node *temp = new Node(x); 51 | curr->next =temp; 52 | curr=temp; 53 | } 54 | } 55 | Node *res = mergeKLists(arr,N); 56 | printList(res); 57 | } 58 | return 0; 59 | } 60 | 61 | } 62 | /*This is a function problem.You only need to complete the function given below*/ 63 | /*Linked list Node structure 64 | struct Node 65 | { 66 | int data; 67 | Node* next; 68 | 69 | Node(int x){ 70 | data = x; 71 | next = NULL; 72 | } 73 | 74 | }; 75 | */ 76 | /* arr[] is an array of pointers to heads of linked lists 77 | and N is size of arr[] */ 78 | 79 | class myComp 80 | { 81 | public: 82 | bool operator()(const Node* a,const Node* b) 83 | { 84 | return (a->data > b->data); 85 | } 86 | }; 87 | 88 | Node * mergeKLists(Node *arr[], int N) 89 | { 90 | priority_queue,myComp> pq; 91 | for(int i=0;idata); 96 | if(temp->next != NULL) 97 | pq.push(temp->next); 98 | temp = head; 99 | while(!pq.empty()) 100 | { 101 | Node* tmp = pq.top(); 102 | pq.pop(); 103 | temp->next = new Node(tmp->data); 104 | temp = temp->next; 105 | if(tmp->next != NULL) 106 | pq.push(tmp->next); 107 | } 108 | return head; 109 | } 110 | -------------------------------------------------------------------------------- /ds/linked-list/sort-list-of-0-1-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* Link list Node */ 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | Node(int x) { 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | struct Node *start = NULL; 14 | struct Node* sortList(struct Node *head); 15 | // Function to sort a linked list of 0s, 1s and 2s 16 | void printList(struct Node *Node) { 17 | while (Node != NULL) { 18 | printf("%d ", Node->data); 19 | Node = Node->next; 20 | } 21 | printf(""); 22 | } 23 | /* Drier program to test above function*/ 24 | void insert(int n1) { 25 | int n, value, i; 26 | // scanf("%d",&n); 27 | n = n1; 28 | struct Node *temp; 29 | for (i = 0; i < n; i++) { 30 | scanf("%d", &value); 31 | if (i == 0) { 32 | start = new Node(value); 33 | temp = start; 34 | continue; 35 | } else { 36 | temp->next = new Node(value); 37 | temp = temp->next; 38 | temp->next = NULL; 39 | } 40 | } 41 | } 42 | int main() { 43 | int n; 44 | int t; 45 | scanf("%d", &t); 46 | while (t--) { 47 | scanf("%d", &n); 48 | insert(n); 49 | struct Node *newHead = sortList(start); 50 | printList(newHead); 51 | } 52 | return 0; 53 | } 54 | 55 | /*This is a function problem.You only need to complete the function given below*/ 56 | /* 57 | Sort the list of 0's,1's and 2's 58 | The input list will have at least one element 59 | Node is defined as 60 | struct Node { 61 | int data; 62 | struct Node *next; 63 | Node(int x) { 64 | data = x; 65 | next = NULL; 66 | } 67 | }; 68 | */ 69 | // This function is to segregate the elememtns in the linked list 70 | // This will do the required arrangement by changing the links 71 | Node* sortList(Node *head) { 72 | Node* Zero = new Node(-1); 73 | Node* One = new Node(-1); 74 | Node* Two = new Node(-1); 75 | Node* OneStart = One; 76 | Node* ZeroStart = Zero; 77 | Node* TwoStart = Two; 78 | Node *temp = head; 79 | while(temp!=NULL) 80 | { 81 | if(temp->data == 0) 82 | { 83 | Zero->next = temp; 84 | Zero = Zero->next; 85 | temp = temp->next; 86 | } 87 | else if(temp->data == 1) 88 | { 89 | One->next = temp; 90 | One = One->next; 91 | temp = temp->next; 92 | } 93 | else if(temp->data == 2) 94 | { 95 | Two->next = temp; 96 | Two = Two->next; 97 | temp = temp->next; 98 | } 99 | } 100 | Zero->next = OneStart->next ? OneStart->next : TwoStart->next; 101 | One->next = TwoStart->next; 102 | Two->next = NULL; 103 | 104 | head = ZeroStart->next; 105 | 106 | delete ZeroStart,OneStart,TwoStart; 107 | return head; 108 | } 109 | -------------------------------------------------------------------------------- /ds/linked-list/single-linked-list-all.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template 5 | class LinkedList 6 | { 7 | class Node 8 | { 9 | T data; 10 | Node* next; 11 | public: 12 | Node(T data) 13 | { 14 | this.data = data; 15 | this.next = NULL; 16 | } 17 | }; 18 | Node* head; 19 | public : 20 | void insert_at_beginning(T val) 21 | { 22 | Node* temp = new Node(val); 23 | temp->next = head; 24 | head = temp; 25 | } 26 | void insert_at_middle(T val,int pos) 27 | { 28 | if(pos==1) 29 | insert_at_beginning(val); 30 | else 31 | { 32 | Node* temp = head; 33 | pos--; 34 | while(--pos && temp!=NULL) 35 | temp = temp->next; 36 | Node* add = new Node(val); 37 | add->next = temp->next; 38 | temp->next = add; 39 | } 40 | } 41 | 42 | void insert_at_end(T val) 43 | { 44 | if(head==NULL) 45 | head = new Node(val); 46 | else if(head->next==NULL) 47 | head->next = new Node(val); 48 | else 49 | { 50 | Node* temp = head; 51 | while(temp->next!=NULL) 52 | temp=temp->next; 53 | temp->next = new Node(val); 54 | } 55 | } 56 | 57 | void delete_head() 58 | { 59 | Node* temp = head; 60 | head = head->next; 61 | delete temp; 62 | } 63 | 64 | void delete_pos(int pos) 65 | { 66 | if(pos==1) 67 | delete_head(); 68 | else 69 | { 70 | pos--; 71 | Node* temp = head; 72 | while(--pos && temp!=NULL) 73 | temp = temp->next; 74 | Node* sub = temp->next; 75 | if(sub!=NULL) 76 | temp->next = sub->next; 77 | else 78 | temp->next = NULL; 79 | delete sub; 80 | } 81 | } 82 | 83 | void delete_val(T val) 84 | { 85 | if(head->data == val) 86 | delete_head(); 87 | else 88 | { 89 | Node* temp = head; 90 | while(temp->next!=NULL) 91 | { 92 | if(temp->next->data==val) 93 | { 94 | Node* del = temp->next; 95 | temp->next = temp->next->next; 96 | delete del; 97 | break; 98 | } 99 | } 100 | } 101 | } 102 | 103 | void delete_intermediate_when_node_given(Node* node) 104 | { 105 | Node *temp = node->next; 106 | node->data = temp->data; 107 | node->next = temp->next; 108 | delete temp; 109 | } 110 | 111 | bool checkLoop() 112 | { 113 | Node* slow = head; 114 | Node* fast = head; 115 | while(fast!=NULL && fast->next!=NULL) 116 | { 117 | slow = slow->next; 118 | fast = fast->next->next; 119 | if(slow == fast) 120 | return true; 121 | } 122 | return false; 123 | } 124 | 125 | Node* findLoopHead() 126 | { 127 | Node* slow = head; 128 | Node* fast = head; 129 | while(fast!=NULL && fast->next!=NULL) 130 | { 131 | slow = slow->next; 132 | fast = fast->next->next; 133 | if(slow == fast) 134 | break; 135 | } 136 | if(fast==NULL || fast->next==NULL) 137 | return NULL; 138 | else 139 | { 140 | fast = head; 141 | while(fast!=slow) 142 | { 143 | slow=slow->next; 144 | fast=fast->next; 145 | } 146 | return slow; 147 | } 148 | } 149 | 150 | void removeTheLoop() 151 | { 152 | Node* slow = head; 153 | Node* fast = head; 154 | while(fast!=NULL && fast->next!=NULL) 155 | { 156 | slow = slow->next; 157 | fast = fast->next->next; 158 | if(slow == fast) 159 | break; 160 | } 161 | if(fast==NULL || fast->next==NULL) 162 | return ; 163 | else 164 | { 165 | fast = head; 166 | while(fast->next!=slow->next) 167 | { 168 | slow=slow->next; 169 | fast=fast->next; 170 | } 171 | slow->next = NULL; 172 | } 173 | } 174 | 175 | Node* getMiddle() 176 | { 177 | Node* slow = head; 178 | Node* fast = head; 179 | while(fast!=NULL && fast->next!=NULL) 180 | { 181 | slow = slow->next; 182 | fast = fast->next->next; 183 | if(slow==fast) 184 | return NULL; 185 | } 186 | return slow; 187 | } 188 | void RotateByKCounterClock(int k) 189 | { 190 | Node* temp = head; 191 | if(k==0) 192 | return ; 193 | while(--k) 194 | { 195 | if(temp->next==NULL) 196 | temp=head; 197 | else 198 | temp=temp->next; 199 | } 200 | Node* end = temp; 201 | while(end->next!=NULL) 202 | end=end->next; 203 | end->next=head; 204 | head=temp->next; 205 | temp->next = NULL; 206 | } 207 | 208 | //ToDO: To complete 209 | /* 210 | void RotateByKClock(int k) 211 | { 212 | Node* temp = head; 213 | 214 | } 215 | */ 216 | 217 | void reverse() 218 | { 219 | Node* temp=head; 220 | Node* prev=NULL; 221 | while(temp!=NULL) 222 | { 223 | Node* tmp = temp->next; 224 | temp->next = prev; 225 | prev = temp; 226 | temp = tmp; 227 | } 228 | head = prev; 229 | } 230 | 231 | void deleteLinkedList() 232 | { 233 | Node* temp = head; 234 | while(temp!=NULL) 235 | { 236 | Node* prev = temp; 237 | temp=temp->next; 238 | prev->next = NULL; 239 | delete prev; 240 | } 241 | head = NULL; 242 | } 243 | 244 | }; 245 | 246 | 247 | int main() 248 | { 249 | return 0; 250 | } 251 | --------------------------------------------------------------------------------