├── .github └── workflows │ └── c-cpp.yml ├── .gitignore ├── .vscode └── settings.json ├── GeeksForGeeks ├── Arrays │ ├── 3 Way Partitioning.cpp │ ├── Count distinct elem in every window.cpp │ ├── Minimum Platform.cpp │ ├── Move all zeroes to end.cpp │ ├── Move negative elements to end.cpp │ ├── Sorting by Absolute Diff.cpp │ ├── Wave Array.cpp │ ├── Zero sum subarray.cpp │ ├── counting-inversions.cpp │ ├── majority-elemment.cpp │ ├── search-in-rotated-array.cpp │ └── spiral-martix.cpp ├── DP │ ├── Interleaved strings.cpp │ ├── Reach the nth Point.cpp │ ├── edit distance.cpp │ ├── longest common subsequence.cpp │ ├── longest common substring.cpp │ ├── longest pallindrome subsequence.cpp │ ├── path in matrix.cpp │ ├── possible paths.cpp │ └── water_overflow.cpp ├── Linked List │ ├── find-length-of-loop.cpp │ ├── finding nth node in linked list.cpp │ ├── get middle.cpp │ ├── loop detection.cpp │ ├── min heap using PQ.cpp │ ├── number of nodes.cpp │ ├── reverse linked list.cpp │ └── sum equls x.cpp ├── OOPS │ ├── Book_store.cpp │ ├── book store.cpp │ └── count num of dynamic objects.cpp ├── Priority queue │ ├── merge k sorted arrays.cpp │ └── modular exponentiation.cpp ├── Project Euler │ ├── even fibonacci.cpp │ ├── good set.cpp │ └── multiples of 3 and 5.cpp ├── Recursion │ ├── Acode.cpp │ ├── delete-mid-element-stack.cpp │ ├── obj │ ├── reverse-stack.cpp │ ├── sort-array.cpp │ ├── sort-stack.cpp │ └── tower-of-hanoi.cpp ├── Searching │ ├── Allocate min number of pages.cpp │ ├── Count the zeroes.cpp │ ├── Count zeroes in sorted matrix.cpp │ ├── Painter partition problem.cpp │ └── Peak element.cpp ├── Stack-Queue │ └── maximum of all subarrays of size k.cpp ├── Trees │ ├── Array to BST.cpp │ ├── Binary Tree to BST.cpp │ ├── Check BST.cpp │ ├── Clone a Binary tree │ ├── Closest Neighbour BST.cpp │ ├── Connect Nodes at same level.cpp │ ├── Count BST Nodes in range.cpp │ ├── Delete Node in BST.cpp │ ├── Inorder Successor in BST.cpp │ ├── Inorder Traversal and BST.cpp │ ├── Insert in BST.cpp │ ├── Isomorphic Trees.cpp │ ├── Kth Largest element in BST.cpp │ ├── Kth SMallest element in BST.cpp │ ├── Min Element in BST.cpp │ ├── Nodes at K level.cpp │ ├── Number of Nodes in level.cpp │ ├── Predecessor and Successor.cpp │ ├── Print BST elements in given Range.cpp │ ├── Search a Node in BST.cpp │ ├── ancestors of a node.cpp │ ├── bottom view of binary tree.cpp │ ├── get level of Node.cpp │ ├── h dist.cpp │ ├── horizontal distance.cpp │ ├── huffman coding.cpp │ ├── print path from root to all leaves.cpp │ ├── special tree.cpp │ ├── subtree sum.cpp │ ├── sum of diagonals.cpp │ └── top view of Binary Tree.cpp ├── activity selection problem.cpp ├── geek collects the ball.cpp ├── job sequencing.cpp ├── platforms.cpp ├── rat in maze.cpp ├── scheduling │ ├── round_robin.cpp │ └── shortest_remaining_time_first.cpp └── strings │ ├── count char.cpp │ ├── hacktoberfest.cpp │ └── permutate_strings.cpp ├── Interview Bit ├── Arrays │ ├── Add One to number.cpp │ ├── Find Duplicate in Array.cpp │ ├── Find missing num.cpp │ ├── MAXPPROD.cpp │ ├── Max Absolute Difference.cpp │ ├── Max Distance.cpp │ ├── Min Steps in Infinite Grid.cpp │ ├── Noble integer.cpp │ ├── Pascal triangle.cpp │ ├── Repeated and missing.cpp │ ├── max non-negative subarray.cpp │ ├── merge overlapping intervals.cpp │ ├── pretty print.cpp │ ├── rotate matrix.cpp │ └── wave array.cpp ├── Binary Search │ ├── Allocate Books.cpp │ ├── Matrix Search.cpp │ ├── Merge Two Sorted Lists II.cpp │ ├── Painters Partition problem.cpp │ ├── Rotated sorted array.cpp │ ├── Search for range.cpp │ └── Square root of integer.cpp ├── Heap │ └── Kth smallest lement.cpp ├── Math │ ├── Excel Column Number.cpp │ ├── FizzBuzz.cpp │ ├── GCD.cpp │ ├── Grid Unique Paths.cpp │ ├── Largest Coprime Divisor.cpp │ ├── Palindrome Integer.cpp │ ├── Prime sum.cpp │ ├── Reverse Integer.cpp │ ├── Sum of Pairwise Hamming distance.cpp │ └── Trailing Zeroes in Factorial.cpp ├── Stacks&Queues │ ├── Min Stack.cpp │ └── Nearest Smaller Element.cpp ├── Strings │ ├── Longest common prefix.cp │ └── Longest common prefix.cpp └── Two Pointers │ ├── 3 sum.cpp │ ├── Container with most water.cpp │ ├── Max contigous series of 1s.cpp │ ├── Merge Two Sorted Lists II.cpp │ ├── Minimize Absolute Diff.cpp │ ├── Remove duplicates 2.cpp │ └── Remove duplicates.cpp ├── Leetcode ├── Arrays │ ├── 3sum-closest.cpp │ ├── 3sum.cpp │ ├── Rotate_image.cpp │ ├── container_with_most_water.cpp │ ├── contains-duplicate-iii.cpp │ ├── contains-duplicate.cpp │ ├── find-all-duplicates.cpp │ ├── find_num_disappeared.cpp │ ├── find_the_duplicate_number.cpp │ ├── first-missing-positive.cpp │ ├── insert-interval.cpp │ ├── intersection-of-array.cpp │ ├── intersection-of-two-arraysII.cpp │ ├── interval-list-intersection.cpp │ ├── largest-rectangle-in-histogram.cpp │ ├── longest-mountain-in-array.cpp │ ├── maximal-rectangle.cpp │ ├── maximal-square.cpp │ ├── maximum_subarray_sum.cpp │ ├── meeting-rooms-ii.cpp │ ├── merge-sorted-arrays.cpp │ ├── merge_intervals.cpp │ ├── min-number-of-arrows-to-burst-balloons.cpp │ ├── minimum-size-subarray-sum.cpp │ ├── product_of_array_except_self.cpp │ ├── range-sum-query-immutable.cpp │ ├── remove-covered-intervals.cpp │ ├── remove-duplicates-from-sorted-array.cpp │ ├── remove-element.cpp │ ├── roman-to-integer.cpp │ ├── scheduler.cpp │ ├── search-in-rotated-sorted-array.cpp │ ├── set-matrix-zeroes.cpp │ ├── shortest_unsorted_contigous_subarray.cpp │ ├── spiral-matrix-ii.cpp │ ├── spiral-matrix.cpp │ ├── subarray_product_less_than_k.cpp │ ├── subarray_sum_equals_k.cpp │ ├── subsets.cpp │ ├── third-maximum-number.cpp │ ├── trapping_rain_water.cpp │ ├── valid-sudoku.cpp │ └── valid_triangle_number.cpp ├── Binary Searching │ ├── binary-search.cpp │ ├── find-in-mountain-array.cpp │ ├── find-k-closest-elements.cpp │ ├── find-minimum-in-rotated-sorted-array.cpp │ ├── find-peak-element.cpp │ ├── find-smallest-element-greater-than-target.cpp │ ├── is-perfect-square.cpp │ ├── k-th-smallest-element-in-sorted-matrix.cpp │ ├── kth-smallest-element-in-bst.cpp │ ├── median-of-two-sorted-arrays.cpp │ ├── search-a-2d-matrix-II.cpp │ ├── search-a-2d-matrix.cpp │ ├── search-insewrt-position.cpp │ └── square-root-number.cpp ├── Bit Manipulation │ ├── counting-bits.cpp │ ├── divide-two-integers.cpp │ ├── number-of-1-bits.cpp │ ├── power-x-n.cpp │ ├── reverse-bits.cpp │ ├── reverse-number.cpp │ ├── single-number-II.cpp │ ├── single-number-III.cpp │ ├── single-number.cpp │ └── sum-of-two-integers.cpp ├── DP │ ├── Climbing Stairs.cpp │ ├── Longest string chain.cpp │ ├── Maximal Square.cpp │ ├── Minimum Falling Path Sum.cpp │ ├── Minimum Path Sum.cpp │ ├── Perfect Squares.cpp │ ├── Unique Paths.cpp │ ├── coin-change.cpp │ ├── house-robber-ii.cpp │ ├── house-robber.cpp │ ├── jump-game-ii.cpp │ ├── jump-game.cpp │ ├── longest-palindrome-substring.cpp │ └── partition-equal-subset-sum.cpp ├── Design │ ├── LRU-cache.cpp │ ├── insert-delete-get-random.cpp │ └── shuffle-array.cpp ├── Graphs │ ├── 01-matrix.cpp │ ├── Alien Dictionary.cpp │ ├── Cheapest flight within K stops.cpp │ ├── Clone Graph.cpp │ ├── Course Schedule.cpp │ ├── Employee Importance.cpp │ ├── Escape a large Maze.cpp │ ├── Is Graph Bipartite.cpp │ ├── Island Perimeter.cpp │ ├── Longest Increasing Path in a Matrix.cpp │ ├── dfs.cpp │ ├── flood-fill.cpp │ ├── max-area-of-islands.cpp │ ├── number-of-islands.cpp │ ├── perimeter-of-island.cpp │ ├── rotten-oranges.cpp │ └── time-to-inform-all-employees.cpp ├── Heap │ └── find-median-from-data-stream.cpp ├── Linked List │ ├── delete-a-node-in-linked-list.cpp │ ├── intersection-of-two-linkedi-list.cpp │ ├── linked-list-cycle-ii.cpp │ ├── linkedi-list-cycle.cpp │ ├── merge-sorted-list.cpp │ ├── odd-even-linked-list.cpp │ ├── palindrome-linkedi-list.cpp │ ├── remove-linkedi-list-elements.cpp │ ├── reverse-linked-list-ii.cpp │ ├── reverse-linkedi-list.cpp │ └── rotate-list.cpp ├── Math │ ├── add-digit.cpp │ ├── factorial-trailing-zero.cpp │ ├── happy-number.cpp │ ├── my-pow.cpp │ ├── plus-one.cpp │ └── ugly-number.cpp ├── Priority Queue │ ├── find-k-pairs-with-smallest-sum.cpp │ ├── k-closest-points.cpp │ ├── k-th-largest-element-in-a-stream.cpp │ ├── k-th-largest-element.cpp │ ├── merge-k-sorted-lists.cpp │ ├── top-k-frequent-elements.cpp │ └── top-k-frequent-words.cpp ├── Recursion │ └── generate-paranthesis.cpp ├── Stack │ ├── Basic Calculator 2.cpp │ ├── Basic Calculator.cpp │ ├── Decode String.cpp │ ├── Minimum Add to make Parenthesis Valid.cpp │ ├── daily-temperatures.cpp │ ├── min-stack.cpp │ └── valid-paranthesis.cpp ├── Trees │ ├── 993-Cousins-in-Binary-Tree.cpp │ ├── Insert-into-BST.cpp │ ├── Unique Binary Search Tees II.cpp │ ├── all-elements-in-two-binary-search-tree.cpp │ ├── balanced-binary-tree.cpp │ ├── binary-level-order-traversal.cpp │ ├── binary-tree-camera.cpp │ ├── binary-tree-max-path-sum.cpp │ ├── binary-tree-postorder.cpp │ ├── binary-tree-zigzag-level-order-traversal.cpp │ ├── boundary-of-binary-tree.cpp │ ├── closest-BST-value.cpp │ ├── construct-binary-tree-from-inorder-and-preorder.cpp │ ├── construct-binary-tree-from-inorder-postorder.cpp │ ├── convert-BST-greater-tree.cpp │ ├── convert-sorted-array-to-binart-searhc-tree.cpp │ ├── depth-of-binary-tree.cpp │ ├── diameter-of-binary-tree.cpp │ ├── distribute-coins-in-binary-tree.cpp │ ├── inorder-iterative.cpp │ ├── insert-into-binary-search-tree.cpp │ ├── invert-tree.cpp │ ├── jump-game-iii.cpp │ ├── largest-BST-in-Binary-Tree.cpp │ ├── level-order-recursive.cpp │ ├── level-order.cpp │ ├── lowest-common-ancestor-of-bst.cpp │ ├── lowest-common-ancestor-tree.cpp │ ├── maximum-binary-tree.cpp │ ├── maximum-depth-N-ary-tree.cpp │ ├── merge-binary-tree.cpp │ ├── min-absolute-diff-in-bst.cpp │ ├── min-diff-between-BST-nodes.cpp │ ├── min-height.cpp │ ├── n-ary-postorder.cpp │ ├── n-ary-preorder.cpp │ ├── path-in-zigzag-labelled-binary-tree.cpp │ ├── path-sum-iii.cpp │ ├── path-sum.cpp │ ├── populate-next-right-pointer.cpp │ ├── populating-next-right-pointer-ii.cpp │ ├── postorder-iterative.cpp │ ├── preorder-iterative.cpp │ ├── print-binary-tree.cpp │ ├── range-sum-bst.cpp │ ├── same-tree.cpp │ ├── search-in-binary-search-tree.cpp │ ├── second-minimum-node-in-binary-tree.cpp │ ├── sum-of-distances-in-tree.cpp │ ├── sum-of-left-leaves.cpp │ ├── sum-of-root-to-binary-numbers.cpp │ ├── sum-root-to-leaf-paths.cpp │ ├── symmetric.tree.cpp │ └── vertical-order-traversal-of-binry-tree.cpp ├── Trie │ ├── Add and Search Word DataStructure design.cpp │ ├── Implement Magic Dictionary.cpp │ ├── Implement Trie.cpp │ ├── Stream of Characters.cpp │ ├── add-and-search-word.cpp │ ├── implement-trie-prefix-tree.cpp │ └── longest-common-prefix-followup.cpp ├── backtracking │ ├── combination-sum.cpp │ ├── combinations-ii.cpp │ ├── combinations.cpp │ ├── largest-time-for-given-digits.cpp │ ├── letter-combinations-of-phone-number.cpp │ ├── permutations-ii.cpp │ ├── permutations.cpp │ ├── subsets-ii.cpp │ ├── subsets.cpp │ └── word-search.cpp ├── queue │ └── queue-recontruction-by-height.cpp └── strings │ ├── 318-Maximum-Product-of-Word-Lengths.cpp │ ├── check-for-anagrams.cpp │ ├── group-anagrams.cpp │ ├── longest-common-prefix.cpp │ ├── palindromic-substring.cpp │ ├── pallindrome-partitioning.cpp │ ├── remove-all-adjacent-duplicates.in-string-ii.cpp │ ├── repeated-dna-subsequence.cpp │ ├── reverse-string-II.cpp │ ├── reverse-string.cpp │ ├── reverse-words-in-a-string-III.cpp │ └── word-pattern.cpp ├── Object Oriented System Design └── library_management_system.cpp ├── README.md ├── codeforces └── #673(Div2) │ ├── copy-paste.cpp │ └── tw-arrays.cpp ├── stripe-design.js └── url_test.js /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: configure 17 | run: ./configure 18 | - name: make 19 | run: make 20 | - name: make check 21 | run: make check 22 | - name: make distcheck 23 | run: make distcheck 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.out 4 | *.com 5 | *.class 6 | *.dll 7 | *.exe 8 | *.o 9 | *.so 10 | 11 | # Packages # 12 | ############ 13 | # it's better to unpack these files and commit the raw source 14 | # git has its own built in compression methods 15 | *.7z 16 | *.dmg 17 | *.gz 18 | *.iso 19 | *.jar 20 | *.rar 21 | *.tar 22 | *.zip 23 | 24 | # Logs and databases # 25 | ###################### 26 | *.log 27 | *.sql 28 | *.sqlite 29 | 30 | # OS generated files # 31 | ###################### 32 | .DS_Store 33 | .DS_Store? 34 | ._* 35 | .Spotlight-V100 36 | .Trashes 37 | ehthumbs.db 38 | <<<<<<< HEAD 39 | Thumbs.db 40 | ======= 41 | Thumbs.db 42 | >>>>>>> 1ca97795a8c45202e9f6b4107bf9da5ab87becea 43 | 44 | Stripe 45 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "deque": "cpp", 4 | "string": "cpp", 5 | "vector": "cpp" 6 | } 7 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Arrays/Move all zeroes to end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | vector arr(n); 14 | 15 | int i; 16 | for(i=0;i>arr[i]; 18 | 19 | int count=0; 20 | for(i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int arr[n]; 13 | int i; 14 | 15 | for(i=0;i>arr[i]; 17 | 18 | for(i=1;i=0&&arr[j]<0) 27 | { 28 | arr[j+1]=arr[j]; 29 | j--; 30 | } 31 | 32 | arr[j+1]=key; 33 | } 34 | 35 | for(i=0;i 2 | #include 3 | using namespace std; 4 | int k; 5 | bool compare(int a, int b ) 6 | { 7 | return abs(a-k)>t; 14 | while(t--) 15 | { 16 | int n; 17 | cin>>n>>k; 18 | 19 | vector arr(n); 20 | int i; 21 | for(i=0;i>arr[i]; 23 | 24 | stable_sort(arr.begin(),arr.end(), compare); 25 | 26 | for(i=0;i 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | vector arr(n); 14 | int i; 15 | for(i=0;i>arr[i]; 17 | 18 | for(i=0;i 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int arr[n]; 13 | int i; 14 | for(i=0;i>arr[i]; 17 | } 18 | 19 | unordered_map ump; 20 | int count=0; 21 | int sum=0; 22 | ump[0]++; 23 | for(i=0;i 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | //code 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | int wt; 14 | cin>>wt; 15 | 16 | int coins[]={1,2}; 17 | 18 | vector dp(wt+1,0); 19 | dp[0]=0; 20 | dp[1]=1; 21 | dp[2]=2; 22 | 23 | for(int i=3;i<=wt;i++) 24 | { 25 | dp[i]=dp[i-1]+dp[i-2]; 26 | } 27 | 28 | cout< 2 | #include 3 | using namespace std; 4 | #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) 5 | 6 | int editCost(string s1, string s2, int m, int n) 7 | { 8 | int dp[m+1][n+1]; 9 | 10 | int i,j; 11 | for(i=0;i<=m;i++) 12 | { 13 | for(j=0;j<=n;j++) 14 | { 15 | if(i==0||j==0) 16 | dp[i][j]=max(i,j); 17 | 18 | else if(s1[i-1]==s2[j-1]) 19 | dp[i][j]=dp[i-1][j-1]; 20 | 21 | else 22 | 23 | dp[i][j]=min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+1; 24 | } 25 | } 26 | 27 | return dp[m][n]; 28 | } 29 | 30 | 31 | int main() 32 | { 33 | IOS; 34 | //code 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int m,n; 40 | cin>>m>>n; 41 | string s1,s2; 42 | cin>>s1>>s2; 43 | 44 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | 8 | int m,n; 9 | cin>>m>>n; 10 | string s1,s2; 11 | cout<<"Enter string1: "; 12 | cin>>s1; 13 | cout<<"Enter string2 : "; 14 | cin>>s2; 15 | 16 | 17 | int dp[m+1][n+1]; 18 | 19 | int i,j; 20 | for(i=0;i<=m;i++) 21 | { 22 | for(j=0;j<=n;j++) 23 | { 24 | if(i==0||j==0) 25 | dp[i][j]=0; 26 | 27 | else if (s1[i-1]==s2[j-1]) 28 | dp[i][j]=dp[i-1][j-1]+1; 29 | 30 | else 31 | { 32 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 33 | } 34 | } 35 | } 36 | cout<<"length of common subsequence: "<0&&j>0) 43 | { 44 | if(s1[i]==s2[j]) 45 | { 46 | cout<dp[i][j-1]) 53 | i--; 54 | else 55 | j--; 56 | 57 | } 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /GeeksForGeeks/DP/longest common substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int m,n; 7 | cin>>m>>n; 8 | string s1,s2; 9 | cin>>s1>>s2; 10 | 11 | int dp[m+1][n+1]; 12 | 13 | int i,j; 14 | int res=0; 15 | for(i=0;i<=m;i++) 16 | { 17 | for(j=0;j<=n;j++) 18 | { 19 | if(i==0||j==0) 20 | dp[i][j]=0; 21 | 22 | else if(s1[i-1]==s2[j-1]) 23 | { 24 | dp[i][j]=1+dp[i-1][j-1]; 25 | res=max(res,dp[i][j]); 26 | 27 | } 28 | else 29 | dp[i][j]=0; 30 | } 31 | } 32 | cout< 2 | using namespace std; 3 | 4 | 5 | int getlp(string s,int i,int j,map &m) 6 | { 7 | cout<second; 11 | 12 | 13 | if(i==j) 14 | { 15 | m[s.substr(i,j+1)]=1; 16 | return 1; 17 | } 18 | 19 | 20 | if(s[i]==s[j]) 21 | { 22 | 23 | if(i+1==j) 24 | { 25 | m[s.substr(i,j+1)]=2; 26 | 27 | return 2; 28 | } 29 | 30 | else 31 | { 32 | 33 | return 2+getlp(s,i+1,j-1,m); 34 | } 35 | 36 | } 37 | else 38 | return max(getlp(s,i+1,j,m),getlp(s,i,j-1,m)); 39 | } 40 | 41 | 42 | int main() 43 | { 44 | string s; 45 | cin>>s; 46 | map m; 47 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int dp[n][n]; 12 | int i,j; 13 | for(i=0;i>dp[i][j]; 17 | } 18 | 19 | 20 | for(i=1;i 6 | #include 7 | using namespace std; 8 | 9 | long long int pascal(int n,int r) 10 | { 11 | long long int arr[n+1][r+1]; 12 | 13 | 14 | 15 | int i,j; 16 | 17 | for(i=0;i<=n;i++) 18 | { 19 | for(j=1;j<=r;j++) 20 | { 21 | 22 | arr[i][j]=0; 23 | } 24 | } 25 | 26 | for(i=0;i<=n;i++) 27 | arr[i][0]=1; 28 | 29 | 30 | 31 | for(i=1;i<=n;i++) 32 | { 33 | 34 | for(j=1;j>t; 58 | 59 | while(t--) 60 | { 61 | int m,n; 62 | cin>>m>>n; 63 | 64 | cout<next!=NULL) 16 | { 17 | hare=hare->next->next; 18 | tortoise=tortoise->next; 19 | 20 | if(hare==tortoise) 21 | break; 22 | 23 | } 24 | 25 | if(hare==NULL || hare->next==NULL) 26 | return NULL; 27 | 28 | tortoise=head; 29 | while(tortoise!=hare) 30 | { 31 | hare=hare->next; 32 | tortoise=tortoise->next; 33 | } 34 | 35 | return hare; 36 | 37 | } 38 | }; -------------------------------------------------------------------------------- /GeeksForGeeks/Linked List/finding nth node in linked list.cpp: -------------------------------------------------------------------------------- 1 | //point to the nth node from the end in a link list 2 | 3 | #include 4 | using namespace std; 5 | 6 | struct node{ 7 | int data; 8 | node* next; 9 | }; 10 | 11 | void findNthNode(node** head,int n) 12 | { 13 | node* first=*head; 14 | node* second=*head; 15 | int i=1; 16 | while(i<=n) 17 | { 18 | second=second->next; 19 | i++; 20 | } 21 | while(second!=NULL) 22 | { 23 | first=first->next; 24 | second=second->next; 25 | } 26 | 27 | cout<<"The nth element is: "<data<data=data; 36 | temp->next=*head; 37 | *head=temp; 38 | 39 | 40 | } 41 | 42 | int main() 43 | { 44 | node* head = NULL; 45 | newNode(&head,20); 46 | newNode(&head,30); 47 | newNode(&head,40); 48 | newNode(&head,50); 49 | node* pointer=head; 50 | 51 | findNthNode(&head,4); 52 | while(pointer!=NULL) 53 | { 54 | cout<data<<"->"; 55 | pointer=pointer->next; 56 | 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /GeeksForGeeks/Linked List/get middle.cpp: -------------------------------------------------------------------------------- 1 | /*This is a function problem.You only need to complete the function given below*/ 2 | /* Link list Node 3 | struct Node { 4 | int data; 5 | Node* next; 6 | }; */ 7 | /* Should return data of middle node. If linked list is empty, then -1*/ 8 | #include 9 | using namespace std; 10 | 11 | struct Node{ 12 | int data; 13 | Node* next; 14 | }; 15 | 16 | void push(Node** head,int data) 17 | { 18 | Node* temp=new Node(); 19 | temp->data=data; 20 | temp->next=*head; 21 | *head=temp; 22 | } 23 | int getMiddle(Node *head) 24 | { 25 | cout<<"Inside middle func"; 26 | if(head==NULL) 27 | return -1; 28 | 29 | Node* first=head; 30 | Node* second=head; 31 | while(second!=NULL&&second->next!=NULL) 32 | { 33 | cout<<"data:"<data<next; 35 | second=second->next->next; 36 | 37 | 38 | } 39 | 40 | return first->data; 41 | 42 | // Your code here 43 | } 44 | 45 | int main() 46 | { 47 | Node* head; 48 | push(&head,10); 49 | push(&head,20); 50 | push(&head,30); 51 | push(&head,40); 52 | push(&head,50); 53 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node* next; 7 | }; 8 | 9 | void push(node** head,int data) 10 | { 11 | node* temp=new node(); 12 | temp->data=data; 13 | temp->next=*head; 14 | *head=temp; 15 | 16 | } 17 | 18 | bool detect(node** head) 19 | { 20 | node* slow=*head; 21 | node* fast=*head; 22 | 23 | while(slow!=NULL&&fast!=NULL&&fast->next!=NULL) 24 | { 25 | slow=slow->next; 26 | fast=fast->next->next; 27 | 28 | if(slow==fast) 29 | return true; 30 | } 31 | return false; 32 | 33 | 34 | } 35 | int main() 36 | { 37 | node *head; 38 | push(&head,10); 39 | push(&head,20); 40 | push(&head,30); 41 | 42 | // head->next->next=head; 43 | if(detect(&head)) 44 | { 45 | cout<<"loop detected"; 46 | } 47 | else 48 | cout<<"no loop"; 49 | 50 | return 0; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /GeeksForGeeks/Linked List/number of nodes.cpp: -------------------------------------------------------------------------------- 1 | //count the number of nodes in a LL recursive and iterati ve 2 | #include 3 | using namespace std; 4 | 5 | struct node{ 6 | int data; 7 | node* next; 8 | }; 9 | 10 | void newNode(node** head,int data) 11 | { 12 | node* temp=new node(); 13 | temp->data=data; 14 | temp->next=NULL; 15 | 16 | if(*head==NULL) 17 | { 18 | *head=temp; 19 | } 20 | else 21 | { 22 | node* pointer=*head; 23 | while(pointer->next!=NULL) 24 | { 25 | pointer=pointer->next; 26 | } 27 | pointer->next=temp; 28 | } 29 | 30 | 31 | } 32 | int main() 33 | { 34 | node *head=NULL; 35 | newNode(&head,10); 36 | newNode(&head,20); 37 | newNode(&head,30); 38 | 39 | node* pointer=head; 40 | while(pointer !=NULL) 41 | { 42 | cout<data<<"->"; 43 | pointer=pointer->next; 44 | } 45 | return 0; 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /GeeksForGeeks/OOPS/count num of dynamic objects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A{ 5 | public: 6 | static int x; 7 | 8 | A() 9 | { 10 | cout<<"Constructor"< 2 | using namespace std; 3 | //get a^b%c 4 | long long int getModularExpo(int a,int b,int c) 5 | { 6 | a=a%c; 7 | long long int res=1; 8 | 9 | while(b>0) 10 | { 11 | if(b&1)//if b is odd 12 | res=(res*a)%c; 13 | 14 | a=(a*a)%c; 15 | b=b/2; // b=b/2 16 | } 17 | 18 | return res; 19 | 20 | 21 | } 22 | 23 | int main() 24 | { 25 | int a,b,c; 26 | cin>>a>>b>>c; 27 | cout< 2 | using namespace std; 3 | 4 | long long int fib(int a,int b,int lim) 5 | { 6 | long long int sum=0; 7 | int c=a+b; 8 | 9 | while(c>lim; 28 | cout< 2 | using namespace std; 3 | # define ll long long 4 | # define div 1000000007 5 | 6 | long long val[2000][2000]; 7 | void nCr(int n) 8 | { 9 | int i,j; 10 | for(i=0;i>n; 39 | nCr(n); 40 | int i; 41 | int size=max(n/2,n-n/2); 42 | ll fact[size]; 43 | fact[0]=1; 44 | for(i=1;i<=size;i++) 45 | { 46 | fact[i]=i*fact[i-1]%div; 47 | } 48 | 49 | for(i=0;i<=size;i++) 50 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a,b,max; 7 | cin>>a>>b>>max; 8 | max--; 9 | int n1=max/a; 10 | int n2=max/b; 11 | int n3=max/(a*b); 12 | 13 | long long int sum=0; 14 | sum+=(n1*(n1+1)/2)*a+(n2*(n2+1)/2)*b-(n3*(n3+1)/2)*(a*b); 15 | 16 | cout< A, 2-->B, .... 26--> Z */ 3 | #include 4 | using namespace std; 5 | 6 | void findStrings(string a, string out) 7 | { 8 | cout<"<1) 29 | { 30 | int second_digit=a[0]-'0'; 31 | int num=10*digit+second_digit; 32 | 33 | if(num<=26) 34 | { 35 | ch=num+('A'-1); 36 | out+=ch; 37 | findStrings(a.substr(2), out); 38 | 39 | } 40 | } 41 | 42 | } 43 | int main() 44 | { 45 | string st; 46 | cout<<"Enter the sequence: "<>st; 48 | 49 | string out=""; 50 | 51 | findStrings(st, out); 52 | 53 | 54 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Recursion/delete-mid-element-stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | stack s; 7 | int k; 8 | s.push(1); 9 | s.push(2); 10 | 11 | s.push(3); 12 | 13 | s.push(4); 14 | k=s.size()/2+1; 15 | solve(s,k); 16 | 17 | 18 | } 19 | 20 | void solve(stack &s, int k) 21 | { 22 | //base condition: when k==1 we delete the top element of stacl 23 | if (k == 1) 24 | { 25 | s.pop(); 26 | return; 27 | } 28 | 29 | //induction step 30 | //we store the top element, call the solve func for smaller stack. 31 | //push the temp element again on top 32 | 33 | int temp=s.top(); 34 | s.pop(); 35 | solve(s, k-1); 36 | s.push(temp); 37 | return; 38 | } 39 | -------------------------------------------------------------------------------- /GeeksForGeeks/Recursion/obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prajwal714/DSA-Coding-Practice/fc8ccaa3feedf62102736feb39eb2cc79b5c0a5b/GeeksForGeeks/Recursion/obj -------------------------------------------------------------------------------- /GeeksForGeeks/Recursion/reverse-stack.cpp: -------------------------------------------------------------------------------- 1 | //reverse a stack in O(1) space 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | } 9 | 10 | //reverses the elements in stack 11 | void reverse(stack &s) 12 | { 13 | //base condition 14 | //when stack size is 1, it is already reverse we return it as it is 15 | if(s.size()==1) 16 | return; 17 | 18 | //induction step, if not we pop the top element and store it in temp; 19 | //call the reverse func for remaining stack and then insert our temp variable 20 | int temp=s.top(); 21 | s.pop(); 22 | reverse(s); 23 | insert(s, temp); 24 | 25 | return; 26 | } 27 | 28 | //inserts an element to the bottom position in stack 29 | void insert(stack &s, int key) 30 | { 31 | //base condition: when stack is empty we can normally insert it 32 | if(s.empty()) 33 | { 34 | s.push(key); 35 | return; 36 | } 37 | 38 | //induction step, if stack is not empty we remove one element from top, call the insert function for stack 39 | //then push the temp variable again 40 | 41 | int temp=s.top(); 42 | s.pop(); 43 | insert(s, key); 44 | s.push(temp); 45 | return; 46 | 47 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Recursion/sort-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | 9 | } 10 | 11 | void sort(vector &arr) 12 | { 13 | //base condition, when single element in array it is already sorted 14 | if(arr.size()==1) 15 | return; 16 | 17 | //induction step 18 | //otherwise, we pop the last element, call the sort for smaller input and insert the temp variable again 19 | int temp=arr[arr.size()-1]; 20 | arr.pop_back(); 21 | 22 | sort(arr); 23 | 24 | insert(arr, temp); 25 | return; 26 | 27 | 28 | } 29 | 30 | void insert(vector &arr, int key){ 31 | //base condition, when arr is empty of arr.last element < temp 32 | //we simeply insert the temp and return 33 | if(arr.size()==0||arr[arr.size()-1]<=key) 34 | { 35 | arr.push_back(key); 36 | return; 37 | 38 | } 39 | 40 | //induction step, else we store the last element, reduce the array size and call the insert function again 41 | // then push back the temp variable again 42 | 43 | int temp=arr[arr.size()-1]; 44 | arr.pop_back(); 45 | insert(arr, key); 46 | arr.push_back(temp); 47 | 48 | return; 49 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Recursion/sort-stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | 9 | } 10 | 11 | void sort(stack &s) 12 | { 13 | //base condition, when single element in say it is already sorted 14 | if(s.size()==1) 15 | return; 16 | 17 | //induction step 18 | //otherwise, we pop the last element, call the sort for smaller input and insert the temp variable again 19 | int temp=s.top(); 20 | s.pop(); 21 | 22 | sort(s); 23 | 24 | insert(s, temp); 25 | return; 26 | 27 | 28 | } 29 | 30 | void insert(stack &s, int key){ 31 | //base condition, when s is empty of s.last element < temp 32 | //we simeply insert the temp and return 33 | if(s.size()==0||s.top()<=key) 34 | { 35 | s.push(key); 36 | return; 37 | 38 | } 39 | 40 | //induction step, else we store the last element, reduce the say size and call the insert function again 41 | // then push back the temp variable again 42 | 43 | int temp=s.top(); 44 | s.pop(); 45 | insert(s, key); 46 | s.push(temp); 47 | 48 | return; 49 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Recursion/tower-of-hanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve(int,int,int,int); 4 | int main() 5 | { 6 | int n=5; 7 | solve(1,3,2,n); 8 | } 9 | 10 | void solve(int s, int d, int h, int n) 11 | { 12 | //base condition, when their is single plate in s, we movie it simply to dest 13 | if(n==1) 14 | { 15 | cout<<"Moving Plate "< 2 | using namespace std; 3 | int main() 4 | { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int arr[n]; 13 | 14 | int i; 15 | for(i=0;i>arr[i]; 17 | 18 | int ele=1; 19 | int start=0, end=n-1; 20 | int lastOcc=-1; 21 | while(start<=end) 22 | { 23 | int mid=start+(end-start)/2; 24 | 25 | if(arr[mid]==ele) 26 | { 27 | lastOcc=mid; 28 | start=mid+1; 29 | } 30 | else if(ele 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,k; 12 | cin>>n>>k; 13 | 14 | deque dq(k); 15 | vector arr(n); 16 | int i; 17 | 18 | for(i=0;i>arr[i]; 20 | 21 | for(i=0;imp){ 4 | 5 | if(tree==NULL){ 6 | return NULL; 7 | } 8 | Node * newtree=new Node(tree->data); 9 | mp[tree]=newtree; 10 | newtree->left=copyrightleft(tree->left,mp); 11 | newtree->right=copyrightleft(tree->right,mp); 12 | return newtree; 13 | } 14 | 15 | void copyrandom(Node *tree,Node *nayatree,unordered_mapmp) 16 | { 17 | 18 | 19 | if(nayatree==NULL){ 20 | return; 21 | } 22 | nayatree->random=mp[tree->random]; 23 | copyrandom(tree->left,nayatree->left,mp); 24 | copyrandom(tree->right,nayatree->right,mp); 25 | } 26 | 27 | Node* cloneTree(Node* tree) 28 | { 29 | //Your code here 30 | if (tree==NULL) 31 | { 32 | return NULL ; 33 | } 34 | unordered_mapmp; 35 | Node * nayatree=copyrightleft(tree,mp); 36 | //dgwiqgiyfgyi 37 | copyrandom(tree,nayatree,mp); 38 | return nayatree; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /GeeksForGeeks/Trees/Inorder Traversal and BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int arr[n]; 13 | 14 | int i; 15 | for(i=0;i>arr[i]; 17 | int flag=1; 18 | for(i=0;i=arr[i+1]) 22 | { 23 | flag=0; 24 | break; 25 | } 26 | } 27 | cout< 2 | using namespace std; 3 | 4 | class node{ 5 | 6 | public: 7 | int data; 8 | node* left,*right; 9 | }; 10 | 11 | node* newNode(int data) 12 | { 13 | node *temp=new node(); 14 | temp->data=data; 15 | 16 | temp->right=NULL; 17 | temp->left=NULL; 18 | return temp; 19 | } 20 | 21 | void diagonalSumUtil(node* root,int vd,map &ds) 22 | { 23 | if(root==NULL) 24 | return; 25 | 26 | ds[vd]+=root->data; 27 | 28 | diagonalSumUtil(root->left,vd+1,ds); 29 | diagonalSumUtil(root->right,vd,ds); 30 | 31 | return; 32 | } 33 | 34 | void diagonalSum(node* root) 35 | { 36 | map ds; 37 | map ::iterator it; 38 | 39 | diagonalSumUtil(root,0,ds); 40 | cout<<"Sum of diagonals: "<first<<" equals : "<second<left=newNode(1); 54 | root->right=newNode(2); 55 | root->left->left=newNode(3); 56 | root->left->right=newNode(4); 57 | root->right->left=newNode(5); 58 | root->right->right=newNode(6); 59 | 60 | diagonalSum(root); 61 | 62 | } 63 | -------------------------------------------------------------------------------- /GeeksForGeeks/job sequencing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct job{ 6 | int id,dl,p; 7 | }; 8 | 9 | bool compare(job a,job b) 10 | { 11 | return a.p>b.p; 12 | } 13 | int main() 14 | { 15 | int t; 16 | cin>>t; 17 | while(t--) 18 | { 19 | int n; 20 | cin>>n; 21 | vector j(n); 22 | 23 | int i; 24 | for(i=0;i>j[i].id; 27 | cin>>j[i].dl; 28 | cin>>j[i].p; 29 | } 30 | sort(j.begin(),j.end(),compare); 31 | queue q; 32 | int result[n]; 33 | bool slot[n]={false}; 34 | 35 | for(i=0;i=0;k--) 38 | { 39 | if(slot[k]==false) 40 | { 41 | slot[k]=true; 42 | q.push(j[i]); 43 | break; 44 | } 45 | } 46 | } 47 | int profit=0; 48 | int count=0; 49 | while(!q.empty()) 50 | { 51 | job temp=q.front(); 52 | q.pop(); 53 | profit+=temp.p; 54 | count++; 55 | } 56 | cout< 2 | #include 3 | using namespace std; 4 | bool compare(pair a,pair b) 5 | { 6 | return a.second>b.second; 7 | } 8 | 9 | int main() 10 | { 11 | int t; 12 | cin>>t; 13 | while(t--) 14 | { 15 | string s; 16 | cin>>s; 17 | int k; 18 | cin>>k; 19 | vector> v; 20 | int i; 21 | 22 | unordered_map m; 23 | for(i=0;ifirst,i->second)); 33 | } 34 | 35 | sort(v.begin(),v.end(),compare); 36 | long long int res=0; 37 | for(i=0;ik) 41 | { 42 | v[i].second-=k; 43 | k=0; 44 | } 45 | else 46 | { 47 | k-=v[i].second; 48 | v[i].second=0; 49 | 50 | } 51 | res+=pow(v[i].second,2); 52 | } 53 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | cout<<"this is my first pr to hacktoberfest 2021"; 6 | } -------------------------------------------------------------------------------- /GeeksForGeeks/strings/permutate_strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector result; 6 | void permutateHelper(string s,string chosen) 7 | { 8 | if(s=="") 9 | { 10 | result.push_back(chosen); 11 | 12 | 13 | } 14 | else 15 | { 16 | for(int i=0;i>t; 46 | 47 | while(t--) 48 | { 49 | string st; 50 | cin>>st; 51 | 52 | permutate(st); 53 | 54 | 55 | cout< Solution::plusOne(vector &arr) { 2 | int i; 3 | int n=arr.size(); 4 | int carry=1; 5 | vector res(n+1,0); 6 | for(i=n;i>=1;i--) 7 | { 8 | int num=arr[i-1]; 9 | res[i]=(num+carry)%10; 10 | carry=(num+carry)/10; 11 | 12 | } 13 | if(carry!=0) 14 | { 15 | 16 | 17 | res[0]=carry; 18 | 19 | 20 | 21 | } 22 | int inx; 23 | for(i=0;i<=n;i++) 24 | { 25 | if(res[i]!=0) 26 | { 27 | inx=i; 28 | break; 29 | } 30 | } 31 | vector br(n+1-inx); 32 | for(i=0;i<=n-inx;i++) 33 | { 34 | br[i]=res[i+inx]; 35 | } 36 | return br; 37 | } 38 | -------------------------------------------------------------------------------- /Interview Bit/Arrays/Find Duplicate in Array.cpp: -------------------------------------------------------------------------------- 1 | int Solution::repeatedNumber(const vector &A) { 2 | 3 | vector B=A; 4 | 5 | for(int i=0;i &A) { 2 | int n=A.size(); 3 | vector v(n+2,false); 4 | int k=0; 5 | for(int i=0;i0&&A[i]<=n) 8 | { 9 | 10 | v[A[i]]=true; 11 | } 12 | 13 | } 14 | for(int i=1;i &arr) { 2 | 3 | int i,j; 4 | int val; 5 | int max1=INT_MIN,max2=INT_MIN,max3=INT_MIN,max4=INT_MIN; 6 | int min1=INT_MAX,min2=INT_MAX,min3=INT_MAX,min4=INT_MAX; 7 | for(i=0;i &A) { 2 | int n = A.size(); 3 | if(n==1) 4 | return 0; 5 | vector> ans; 6 | 7 | //vector pair is made so that we dont lose original index after sorting of array 8 | //make vector pair to store key and value 9 | for(int i=0;i=0;i--){ 20 | mx = max(mx, ans[i].second); 21 | max_index[i] = mx; 22 | } 23 | 24 | //iterate vector pair and find max index from i to n 25 | int mil_gaya = INT_MIN; 26 | for(int i=0;i &A, vector &B) { 2 | 3 | int step=0; 4 | int i,j; 5 | 6 | int startX=A[0]; 7 | int startY=B[0]; 8 | for(i=0;i &A) { 2 | sort(A.begin(), A.end()); 3 | int n=A.size(); 4 | for(int i=0;i > Solution::solve(int A) { 2 | int row=A; 3 | 4 | 5 | 6 | vector> arr(row); 7 | 8 | if(row>0) 9 | arr[0].push_back(1); 10 | 11 | int i; 12 | for(i=1;i repeatedNumber(const vector &V) { 2 | long long sum = 0; 3 | long long squareSum = 0; 4 | long long temp; 5 | for (int i = 0; i < V.size(); i++) { 6 | temp = V[i]; 7 | sum += temp; 8 | sum -= (i + 1); 9 | squareSum += (temp * temp); 10 | squareSum -= ((long long)(i + 1) * (long long)(i + 1)); 11 | } 12 | // sum = A - B 13 | // squareSum = A^2 - B^2 = (A - B)(A + B) 14 | // squareSum / sum = A + B 15 | squareSum /= sum; 16 | 17 | // Now we have A + B and A - B. Lets figure out A and B now. 18 | int A = (int) ((sum + squareSum) / 2); 19 | int B = squareSum - A; 20 | 21 | vector ret; 22 | ret.push_back(A); 23 | ret.push_back(B); 24 | return ret; 25 | } 26 | -------------------------------------------------------------------------------- /Interview Bit/Arrays/pretty print.cpp: -------------------------------------------------------------------------------- 1 | vector > Solution::prettyPrint(int num) { 2 | int n=2*num-1; 3 | 4 | vector > A(n); 5 | 6 | 7 | int val=num; 8 | int top=0, bottom=n-1,right=n-1,left=0; 9 | 10 | int i,j; 11 | 12 | for(i=0;i=left;i--) 30 | A[bottom][i]=val; 31 | bottom--; 32 | 33 | for(i=bottom;i>=top;i--) 34 | A[i][left]=val; 35 | left++; 36 | 37 | val--; 38 | 39 | 40 | } 41 | 42 | return A; 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Interview Bit/Arrays/rotate matrix.cpp: -------------------------------------------------------------------------------- 1 | void Solution::rotate(vector > &arr) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int i,j; 7 | int n=arr[0].size(); 8 | 9 | for(i=0;i Solution::wave(vector &A) { 2 | sort(A.begin(), A.end()); 3 | int n=A.size(); 4 | vector res; 5 | int size=n; 6 | if(n%2==1) 7 | size=n-1; 8 | 9 | for(int i=0;i A, int maxStud, int mid) 2 | { 3 | int i; 4 | int students=1; 5 | long long int sum=0; 6 | for(i=0;imid) 10 | { 11 | students++; 12 | sum=A[i]; 13 | } 14 | 15 | if(students>maxStud) 16 | return false; 17 | } 18 | 19 | return true; 20 | } 21 | 22 | int Solution::books(vector &A, int B) { 23 | 24 | if(B>A.size()) 25 | return -1; 26 | 27 | int start=INT_MIN; 28 | long long int end=0, res; 29 | int i; 30 | for(i=0;i > &A, int B) { 2 | 3 | int m=A.size(); 4 | int n=A[0].size(); 5 | int i=0,j=n-1; 6 | 7 | while(i>=0 && i=0 && j C, int max_painters, int mid) 2 | { 3 | int painter=1; 4 | long long int sum=0; 5 | 6 | for(int i=0;imid) 10 | { 11 | sum=C[i]; 12 | painter++; 13 | } 14 | 15 | if(painter>max_painters) 16 | return false; 17 | } 18 | return true; 19 | } 20 | 21 | int Solution::paint(int A, int B, vector &C) { 22 | 23 | int n=C.size(); 24 | if(A>n) 25 | A=n; 26 | 27 | int start=-1; 28 | long long int end=0; 29 | 30 | int i; 31 | for(i=0;i Solution::searchRange(const vector &A, int B) { 2 | 3 | int firstOcc=-1; 4 | int lastOcc=-1; 5 | 6 | int start=0; 7 | int end=A.size()-1; 8 | 9 | while(start<=end) 10 | { 11 | int mid=start+(end-start)/2; 12 | 13 | if(A[mid]==B) 14 | { 15 | firstOcc=mid; 16 | end=mid-1; 17 | } 18 | else if(B sol; 46 | sol.push_back(firstOcc); 47 | sol.push_back(lastOcc); 48 | 49 | return sol; 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Interview Bit/Binary Search/Square root of integer.cpp: -------------------------------------------------------------------------------- 1 | int Solution::sqrt(int A) { 2 | 3 | int start=1, end=A; 4 | int res; 5 | 6 | if(A==0) 7 | return 0; 8 | 9 | while(start<=end) 10 | { 11 | int mid=start+(end-start)/2; 12 | 13 | if(mid==A/mid) 14 | { 15 | return mid; 16 | 17 | } 18 | 19 | else if(mid &A, int B) { 2 | 3 | priority_queue pq; 4 | 5 | int i; 6 | for(i=0;i=0;i--) 7 | { 8 | res+=pow(26,k)*(A[i]-'A'+1); 9 | k++; 10 | } 11 | 12 | return res; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Interview Bit/Math/FizzBuzz.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::fizzBuzz(int A) { 2 | 3 | vector res; 4 | for(int i=1;i<=A;i++) 5 | { 6 | if(i%3==0&&i%5==0) 7 | res.push_back("FizzBuzz"); 8 | else if(i%3==0) 9 | res.push_back("Fizz"); 10 | else if(i%5==0) 11 | res.push_back("Buzz"); 12 | else 13 | res.push_back(to_string(i)); 14 | } 15 | 16 | return res; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Interview Bit/Math/GCD.cpp: -------------------------------------------------------------------------------- 1 | int Solution::gcd(int A, int B) { 2 | if(B==0) 3 | return A; 4 | if(A%B==0) 5 | return B; 6 | 7 | return gcd(B, A%B); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /Interview Bit/Math/Grid Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | int Solution::uniquePaths(int A, int B) { 2 | int dp[A][B]; 3 | 4 | int i,j; 5 | for(i=0;i0){ 4 | int x=a/i; 5 | if(i*x==a && __gcd(x,b)==1) 6 | return x; 7 | i++; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Interview Bit/Math/Palindrome Integer.cpp: -------------------------------------------------------------------------------- 1 | int Solution::isPalindrome(int A) { 2 | 3 | string s = to_string(A); 4 | 5 | int l=0,r=s.size()-1; 6 | 7 | while(l Solution::primesum(int A) { 9 | vector v; 10 | for(int i=2; i<=A/2; i++) 11 | if(isprime(i) && isprime(A-i)) 12 | v.push_back(i),v.push_back(A-i),i=A; 13 | return v; 14 | } 15 | -------------------------------------------------------------------------------- /Interview Bit/Math/Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | int Solution::reverse(int A) { 2 | 3 | if(A==0) 4 | return 0; 5 | 6 | int rev=0; 7 | bool neg=false; 8 | int num=A; 9 | 10 | if(num<0) 11 | { 12 | neg=true; 13 | num=-num; 14 | } 15 | 16 | 17 | 18 | 19 | while(num!=0) 20 | { 21 | int currDigit=num%10; 22 | if(rev>(INT_MAX-currDigit)/10) 23 | { 24 | return 0; 25 | } 26 | else 27 | rev=rev*10+currDigit; 28 | 29 | 30 | 31 | 32 | num=num/10; 33 | 34 | } 35 | 36 | if(neg) 37 | { 38 | rev=rev*-1; 39 | if(rev>0) 40 | return 0; 41 | else 42 | return rev; 43 | } 44 | else 45 | return rev; 46 | 47 | 48 | 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Interview Bit/Math/Sum of Pairwise Hamming distance.cpp: -------------------------------------------------------------------------------- 1 | int Solution::hammingDistance(const vector &A) { 2 | 3 | vector bin_array(32,0); 4 | const int mod=1000000007; 5 | int i; 6 | for(i=0;i0) 11 | { 12 | bin_array[index]+=(num&1); 13 | num=num>>1; 14 | index++; 15 | } 16 | } 17 | int ans=0; 18 | for(i=0;i<32;i++) 19 | { 20 | ans=(ans+((bin_array[i])*(A.size()-bin_array[i])*2))%mod; 21 | } 22 | 23 | return ans; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Interview Bit/Math/Trailing Zeroes in Factorial.cpp: -------------------------------------------------------------------------------- 1 | int Solution::trailingZeroes(int A) { 2 | int num5=0; 3 | int num2=0; 4 | 5 | int k=1; 6 | 7 | int B=A; 8 | while((B/pow(5,k))) 9 | { 10 | num5+=(B/pow(5,k)); 11 | k++; 12 | } 13 | 14 | B=A; 15 | k=1; 16 | while((B/pow(2,k))) 17 | { 18 | num2+=(B/pow(2,k)); 19 | k++; 20 | } 21 | 22 | return min(num5,num2); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Interview Bit/Stacks&Queues/Min Stack.cpp: -------------------------------------------------------------------------------- 1 | stack s; 2 | stack ss; 3 | MinStack::MinStack() { 4 | 5 | while(!s.empty()) 6 | s.pop(); 7 | 8 | while(!ss.empty()) 9 | ss.pop(); 10 | 11 | } 12 | 13 | void MinStack::push(int x) { 14 | s.push(x); 15 | if(ss.empty()||ss.top()>x) 16 | ss.push(x); 17 | } 18 | 19 | void MinStack::pop() { 20 | 21 | if(s.empty()) 22 | return; 23 | 24 | if(s.top()==ss.top()) 25 | { 26 | s.pop(); 27 | ss.pop(); 28 | } 29 | else 30 | s.pop(); 31 | } 32 | 33 | int MinStack::top() { 34 | 35 | if(s.empty()) 36 | return -1; 37 | 38 | return s.top(); 39 | } 40 | 41 | int MinStack::getMin() { 42 | 43 | if(ss.empty()) 44 | return -1; 45 | 46 | return ss.top(); 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /Interview Bit/Stacks&Queues/Nearest Smaller Element.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::prevSmaller(vector &A) { 2 | 3 | vector sol; 4 | 5 | stack s; 6 | 7 | int i, n=A.size(); 8 | 9 | for(i=0;i=A[i]) 22 | { 23 | s.pop(); 24 | } 25 | 26 | if(s.empty()) 27 | sol.push_back(-1); 28 | else 29 | sol.push_back(s.top()); 30 | } 31 | 32 | s.push(A[i]); 33 | } 34 | 35 | return sol; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Interview Bit/Strings/Longest common prefix.cp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prajwal714/DSA-Coding-Practice/fc8ccaa3feedf62102736feb39eb2cc79b5c0a5b/Interview Bit/Strings/Longest common prefix.cp -------------------------------------------------------------------------------- /Interview Bit/Strings/Longest common prefix.cpp: -------------------------------------------------------------------------------- 1 | string Solution::longestCommonPrefix(vector &A) { 2 | 3 | string current=A[0]; 4 | int min=INT_MAX; 5 | 6 | int i,j; 7 | 8 | for(j=1;j &A, int B) { 2 | 3 | int i,curr,start,end; 4 | int sum=0, res=INT_MAX, ans; 5 | sort(A.begin(), A.end()); 6 | for(i=A.size()-1;i>=2;i--) 7 | { 8 | curr=A[i]; 9 | start=0; end=i-1; 10 | while(startB) 23 | end--; 24 | else 25 | return sum; 26 | } 27 | } 28 | return ans; 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Interview Bit/Two Pointers/Container with most water.cpp: -------------------------------------------------------------------------------- 1 | int Solution::maxArea(vector &A) { 2 | long long int res=0; 3 | long long int sum=0; 4 | 5 | int start=0,end=A.size()-1; 6 | 7 | while(startres) 12 | res=sum; 13 | if(A[start]=A[end]) 18 | { 19 | end--; 20 | } 21 | 22 | } 23 | 24 | return res; 25 | } 26 | 27 | ========================================================================= 28 | 29 | 30 | -------------------------------------------------------------------------------- /Interview Bit/Two Pointers/Max contigous series of 1s.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::maxone(vector &A, int B) { 2 | 3 | int wl = 0; 4 | int wr = 0; 5 | int zeroCount = 0; 6 | int bestLeft; 7 | int bestRight; 8 | int maxValue = INT_MIN; 9 | while(wr < A.size()) 10 | { 11 | if(zeroCount<=B) 12 | { 13 | if(A[wr] == 0) 14 | { 15 | zeroCount++; 16 | } 17 | wr++; 18 | } 19 | else if(zeroCount>B) 20 | { 21 | if(A[wl]==0) 22 | { 23 | zeroCount--; 24 | } 25 | wl++; 26 | } 27 | if(((wr - wl) > maxValue) && (zeroCount<=B)) 28 | { 29 | maxValue = wr - wl; 30 | bestLeft = wl; 31 | bestRight = wr; 32 | } 33 | } 34 | vector v; 35 | for(int i=bestLeft;i &A, vector &B) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int i = A.size() - 1, j = B.size() - 1, index = A.size() + B.size() - 1; 7 | A.resize(A.size() + B.size()); 8 | while(i >= 0 and j >= 0) 9 | A[index--] = (A[i] > B[j]) ? A[i--] : B[j--]; 10 | while(j >= 0) 11 | A[index--] = B[j--]; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Interview Bit/Two Pointers/Minimize Absolute Diff.cpp: -------------------------------------------------------------------------------- 1 | int Solution::solve(vector &A, vector &B, vector &C) { 2 | 3 | int x=0,y=0,z=0; 4 | int a,b,c; 5 | int res=INT_MAX; 6 | while(x &A) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int count=0; 7 | int n=A.size(); 8 | 9 | int index=0; 10 | for(int i=0;i=2) 19 | { 20 | A[count++]=A[i]; 21 | A[count++]=A[i]; 22 | } 23 | else 24 | A[count++]=A[i]; 25 | } 26 | 27 | return count; 28 | 29 | 30 | } 31 | 32 | ======================================================================================== 33 | 34 | class Solution { 35 | public: 36 | int removeDuplicates(int A[], int n) { 37 | int count = 0; 38 | for (int i = 0; i < n; i++) { 39 | if (i < n - 2 && A[i] == A[i+1] && A[i] == A[i+2]) continue; 40 | else { 41 | A[count] = A[i]; 42 | count++; 43 | } 44 | } 45 | return count; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /Interview Bit/Two Pointers/Remove duplicates.cpp: -------------------------------------------------------------------------------- 1 | int Solution::removeDuplicates(vector &A) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int count=0; 7 | A.push_back(-1); 8 | int n=A.size(); 9 | int i; 10 | int index=0; 11 | 12 | for(i=0;i& nums, int target) { 4 | 5 | int i,j,k; 6 | int n=nums.size(); 7 | int res, minDiff=INT_MAX; 8 | sort(nums.begin(), nums.end()); 9 | for(k=n-1;k>=2;k--) 10 | { 11 | i=0;j=k-1; 12 | while(i> threeSum(vector& nums) { 6 | 7 | int i,j,k; 8 | int n=nums.size(); 9 | vector> res; 10 | sort(nums.begin(), nums.end()); 11 | for(k=n-1;k>=2;k--) 12 | { 13 | if(k{nums[i],nums[j],nums[k]}); 24 | 25 | while(i>& arr) { 4 | int n=arr[0].size(); 5 | 6 | int level=n/2; 7 | 8 | int i,j; 9 | for(i=0;i& height) { 4 | 5 | int n=height.size(); 6 | int start=0, end=n-1; 7 | int res=INT_MIN; 8 | while(start<=end){ 9 | 10 | int water=min(height[start],height[end])*(end-start); 11 | 12 | res=max(res,water); 13 | 14 | if(height[start]& nums, int k, int t) { 4 | 5 | 6 | int n=nums.size(); 7 | multiset s; 8 | if(n<=0 || k<=0) 9 | return false; 10 | 11 | int i; 12 | for(i=0;ik) 16 | { 17 | s.erase(nums[i-k-1]); 18 | } 19 | 20 | auto pos=s.lower_bound(num-t); 21 | 22 | if(pos!=s.end() && (long)*pos-num<=t) 23 | return true; 24 | 25 | s.insert(nums[i]); 26 | 27 | } 28 | 29 | return false; 30 | 31 | 32 | } 33 | }; 34 | 35 | //complexity: nlogk logk complexity for set operations, n times for each element in array -------------------------------------------------------------------------------- /Leetcode/Arrays/contains-duplicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyDuplicate(vector& nums, int k) { 4 | 5 | 6 | unordered_map mp; 7 | 8 | int n=nums.size(); 9 | 10 | if(n<=0 || k<=0) 11 | return false; 12 | 13 | 14 | for(int i=0;ik) 17 | { 18 | mp[nums[i-k-1]]--; 19 | } 20 | 21 | mp[nums[i]]++; 22 | 23 | if(mp[nums[i]]>1) 24 | return true; 25 | } 26 | 27 | return false; 28 | } 29 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/find-all-duplicates.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& nums) { 4 | int n=nums.size(); 5 | vector res; 6 | 7 | for(int i=0;i0) 13 | res.push_back(pos+1); 14 | } 15 | 16 | 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/find_num_disappeared.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prajwal714/DSA-Coding-Practice/fc8ccaa3feedf62102736feb39eb2cc79b5c0a5b/Leetcode/Arrays/find_num_disappeared.cpp -------------------------------------------------------------------------------- /Leetcode/Arrays/find_the_duplicate_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | 5 | int tortoise=nums[0]; 6 | int hare=nums[0]; 7 | 8 | do 9 | { 10 | tortoise=nums[tortoise]; 11 | hare=nums[nums[hare]]; 12 | } 13 | while(hare!=tortoise); 14 | 15 | tortoise=nums[0]; 16 | while(tortoise!=hare) 17 | { 18 | tortoise=nums[tortoise]; 19 | hare=nums[hare]; 20 | } 21 | 22 | return hare; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/Arrays/first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | 2 | //we know the answer lies between 1 and n+1 inclusive, 3 | // complexity O(n) and O(1) 4 | 5 | /* so we simply swap the numbers with their correct correct position numbers, such that i+1=nums[i] for each i 6 | in case nums[i]<0 || nums[i]>n we simply ignore these values 7 | 8 | the smallest i where i+1 != nums[i] is our answer 9 | */ 10 | 11 | class Solution { 12 | public: 13 | int firstMissingPositive(vector& nums) { 14 | 15 | int n=nums.size(); 16 | if(n==0) 17 | return 1; 18 | for(int i=0;in) 21 | continue; 22 | 23 | int pos=nums[i]-1; 24 | 25 | while(nums[i]>=1 && nums[i]<=n && nums[i]!=nums[pos]) 26 | { 27 | swap(nums[i], nums[pos]); 28 | pos=nums[i]-1; 29 | } 30 | } 31 | 32 | for(int i=0;i> insert(vector>& intervals, vector& newInterval) { 4 | 5 | vector> res; 6 | int index=0; 7 | 8 | //firstly insert the intervals before the new Interval that are non overlapping 9 | while(index intersection(vector& nums1, vector& nums2) { 7 | 8 | if(nums1.size()>nums2.size()) 9 | return intersection(nums2,nums1); 10 | 11 | unordered_map mp; 12 | vector res; 13 | for(int n:nums1) 14 | { 15 | mp[n]++; 16 | } 17 | 18 | for(int n:nums2) 19 | { 20 | if(mp[n]>0) 21 | { 22 | res.push_back(n); 23 | mp[n]=0; 24 | } 25 | } 26 | 27 | return res; 28 | 29 | 30 | 31 | } 32 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/intersection-of-two-arraysII.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | //we use sorting and two pointer tech 5 | //time: O(mlogm+nlogn) 6 | //space: O(1) 7 | 8 | class Solution { 9 | public: 10 | vector intersect(vector& nums1, vector& nums2) { 11 | 12 | sort(nums1.begin(), nums1.end()); 13 | sort(nums2.begin(), nums2.end()); 14 | int n1 = (int)nums1.size(), n2 = (int)nums2.size(); 15 | int i1 = 0, i2 = 0; 16 | vector res; 17 | while(i1 < n1 && i2 < n2){ 18 | if(nums1[i1] == nums2[i2]) { 19 | res.push_back(nums1[i1]); 20 | i1++; 21 | i2++; 22 | } 23 | else if(nums1[i1] > nums2[i2]){ 24 | i2++; 25 | } 26 | else{ 27 | i1++; 28 | } 29 | } 30 | return res; 31 | 32 | 33 | } 34 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/interval-list-intersection.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> intervalIntersection(vector>& A, vector>& B) { 4 | 5 | 6 | vector> res; 7 | 8 | int i=0,j=0; 9 | 10 | while(i{low,high}); 18 | 19 | 20 | 21 | if(A[i][1]& heights) { 4 | 5 | stack s; 6 | int n=heights.size(); 7 | int i; 8 | int maxArea=0; 9 | for(i=0;iheights[i]) 18 | { 19 | int height=heights[s.top()]; 20 | s.pop(); 21 | int width=s.empty()?i:(i-s.top()-1); 22 | 23 | maxArea=max(maxArea, height*width); 24 | } 25 | s.push(i); 26 | } 27 | } 28 | 29 | while(!s.empty()) 30 | { 31 | int height=heights[s.top()]; 32 | s.pop(); 33 | int width=s.empty()?i:i-s.top()-1; 34 | 35 | maxArea=max(maxArea, height*width); 36 | } 37 | 38 | return maxArea; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Leetcode/Arrays/longest-mountain-in-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestMountain(vector& arr) { 4 | 5 | 6 | int n=arr.size(); 7 | int ans=0, base=0; 8 | 9 | if(n<1) 10 | return 0; 11 | 12 | while(basearr[end+1]) 21 | { 22 | while(end+1arr[end+1]) 23 | end++; 24 | 25 | ans=max(ans, end-base+1); 26 | } 27 | } 28 | 29 | 30 | base=max(end, base+1); 31 | 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/maximal-square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximalSquare(vector>& matrix) { 4 | 5 | int m=matrix.size(); 6 | if(m==0) 7 | return 0; 8 | int n=matrix[0].size(); 9 | 10 | vector> dp(m+1, vector(n+1,0)); 11 | int side=0; 12 | int i,j; 13 | for(i=1;i<=m;i++) 14 | { 15 | for(j=1;j<=n;j++) 16 | { 17 | if(matrix[i-1][j-1]=='1') 18 | { 19 | dp[i][j]=min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+1; 20 | side=max(side, dp[i][j]); 21 | } 22 | } 23 | } 24 | 25 | return side*side; 26 | 27 | 28 | 29 | 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Leetcode/Arrays/maximum_subarray_sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int n=nums.size(); 5 | int i; 6 | 7 | for(i=1;i0) 10 | { 11 | nums[i]+=nums[i-1]; 12 | } 13 | } 14 | 15 | int res=INT_MIN; 16 | for(i=0;i 2 | using namespace std; 3 | 4 | 5 | static bool compare(vector a, vector b) 6 | { 7 | if(a[0]==b[0]) 8 | return a[1]> intervals) 14 | { 15 | int n=intervals.size(); 16 | 17 | sort(intervals.begin(), intervals.end(), compare); 18 | priority_queue, greater> pq; 19 | 20 | int count=0; 21 | pq.push(intervals[0][1]); 22 | count++; 23 | 24 | for(int i=1;i> intervals={ {0,5}, {5,10}, {15,20}}; 42 | 43 | cout<& nums1, int m, vector& nums2, int n) { 4 | 5 | int i = m - 1, j = n - 1, tar = m + n - 1; 6 | 7 | while(j>=0) 8 | { 9 | if(i>=0 && nums1[i]>nums2[j]) 10 | { 11 | nums1[tar--]=nums1[i--]; 12 | } 13 | else 14 | nums1[tar--]=nums2[j--]; 15 | } 16 | } 17 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/min-number-of-arrows-to-burst-balloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | static bool compare(vector a, vector b) 5 | { 6 | return a[0]>& points) { 10 | 11 | if(points.size()==0) 12 | return 0; 13 | 14 | sort(points.begin(), points.end(), compare); 15 | 16 | vector> res; 17 | 18 | res.push_back(points[0]); 19 | 20 | for(int i=1;i=res.back()[0] && points[i][0]<=res.back()[1]) 23 | { 24 | res.back()[0]=max(res.back()[0], points[i][0]); 25 | res.back()[1]=min(res.back()[1], points[i][1]); 26 | } 27 | else 28 | res.push_back(points[i]); 29 | } 30 | 31 | return res.size(); 32 | 33 | } 34 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/minimum-size-subarray-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int s, vector& nums) { 4 | 5 | if(nums.size()==0) 6 | return 0; 7 | 8 | int left=0,right=0; 9 | int sum=nums[0]; 10 | int res=INT_MAX; 11 | while(left<=right) 12 | { 13 | while(sum=s) 20 | { 21 | res=min(res, right-left+1); 22 | } 23 | 24 | sum-=nums[left]; 25 | left++; 26 | } 27 | if(res==INT_MAX) 28 | return 0; 29 | 30 | return res; 31 | } 32 | }; 33 | 34 | //complexity: O(n) -------------------------------------------------------------------------------- /Leetcode/Arrays/product_of_array_except_self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | int n=nums.size(); 5 | 6 | vector lp(n); 7 | vector rp(n); 8 | int i; 9 | lp[0]=1; 10 | rp[n-1]=1; 11 | 12 | int prod=1; 13 | for(i=1;i=0;i--) 20 | { 21 | prod*=nums[i+1]; 22 | rp[i]=(prod); 23 | } 24 | 25 | vector res; 26 | 27 | for(i=0;i nums; 4 | NumArray(vector& arr) { 5 | 6 | nums.assign(arr.begin(), arr.end()); 7 | 8 | for(int i=1;isumRange(i,j); 27 | */ -------------------------------------------------------------------------------- /Leetcode/Arrays/remove-covered-intervals.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Given a list of intervals, remove all intervals that are covered by another interval in the list. 3 | 4 | // Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d. 5 | 6 | // After doing so, return the number of remaining intervals. 7 | class Solution { 8 | public: 9 | 10 | static bool compare(vector a, vector b) 11 | { 12 | return a[0]>& intervals) { 16 | 17 | sort(intervals.begin(), intervals.end(), compare); 18 | 19 | vector> res; 20 | res.push_back(intervals[0]); 21 | 22 | for(int i=1;i=res.back()[0] && intervals[i][1]<=res.back()[1] || res.back()[0]>=intervals[i][0] && res.back()[1]<=intervals[i][1]) 30 | { 31 | res.back()[0]=min(res.back()[0], intervals[i][0]); 32 | res.back()[1]=max(res.back()[1], intervals[i][1]); 33 | 34 | } 35 | else 36 | res.push_back(intervals[i]); 37 | } 38 | 39 | return res.size(); 40 | 41 | } 42 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int i=0; 5 | int j=0; 6 | int n=nums.size(); 7 | if(n==0) 8 | return 0; 9 | 10 | //this can be generalized for atmost k duplicate elements, just replace 2 with k 11 | for(j=0;jnums[i-2]) 14 | { 15 | nums[i++]=nums[j]; 16 | } 17 | } 18 | 19 | return i; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/Arrays/remove-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | 5 | int n=nums.size(); 6 | int right=n; 7 | for(int left=0;left mp={ 4 | {'I',1}, 5 | {'V',5}, 6 | {'X',10,}, 7 | {'L',50,}, 8 | {'C',100}, 9 | {'D',500}, 10 | {'M',1000} 11 | 12 | }; 13 | 14 | int romanToInt(string s) { 15 | 16 | 17 | if(s.length()==0) 18 | return 0; 19 | 20 | int res=0; 21 | res+=mp[s.back()]; 22 | 23 | for(int i=s.length()-2;i>=0;i--) 24 | { 25 | if(mp[s[i]]& tasks, int n) { 4 | unordered_map mp; 5 | 6 | int i,size=tasks.size(); 7 | int count=0, countOfMaxTasks=0; 8 | for(i=0;i& nums) { 4 | 5 | int n=nums.size(); 6 | 7 | int minElem=INT_MAX, maxElem=INT_MIN; 8 | int i; 9 | bool flag=false; 10 | for(i=1;i=0;i--) 24 | { 25 | if(nums[i]>nums[i+1]) 26 | flag=true; 27 | 28 | if(flag) 29 | { 30 | maxElem=max(maxElem, nums[i]); 31 | } 32 | } 33 | 34 | int l,r; 35 | for(l=0;l=0;r--) 42 | { 43 | if(maxElem>nums[r]) 44 | break; 45 | } 46 | 47 | if((r-l)<=0) 48 | return 0; 49 | else 50 | return r-l+1; 51 | 52 | 53 | 54 | } 55 | }; 56 | -------------------------------------------------------------------------------- /Leetcode/Arrays/spiral-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | // fill values from 1 to n^2 in a spiral order 2 | 3 | class Solution { 4 | public: 5 | vector> generateMatrix(int n) { 6 | 7 | vector> matrix(n, vector(n,0)); 8 | 9 | int up=0,down=n-1, left=0,right=n-1; 10 | int k=1; 11 | while(up<=down && left<=right) 12 | { 13 | int i; 14 | for(i=left;i<=right;i++) 15 | matrix[up][i]=k++; 16 | 17 | up++; 18 | 19 | for(i=up;i<=down;i++) 20 | matrix[i][right]=k++; 21 | 22 | right--; 23 | 24 | for(i=right;i>=left;i--) 25 | matrix[down][i]=k++; 26 | 27 | down--; 28 | 29 | for(i=down;i>=up;i--) 30 | matrix[i][left]=k++; 31 | 32 | left++; 33 | } 34 | 35 | return matrix; 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/spiral-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector spiralOrder(vector>& matrix) { 4 | vector res; 5 | 6 | int m=matrix.size(); 7 | if(m==0) 8 | return res; 9 | int n=matrix[0].size(); 10 | if(n==0) 11 | return res; 12 | 13 | int up=0, down=m-1, right=n-1, left=0; 14 | while(up<=down && left<=right) 15 | { 16 | int i; 17 | if(up=left;i--) 32 | res.push_back(matrix[down][i]); 33 | 34 | 35 | } 36 | down--; 37 | 38 | if(left<=right) 39 | { 40 | for(i=down;i>=up;i--) 41 | res.push_back(matrix[i][left]); 42 | 43 | 44 | } 45 | left++; 46 | 47 | 48 | 49 | } 50 | 51 | return res; 52 | } 53 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/subarray_product_less_than_k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarrayProductLessThanK(vector& nums, int k) { 4 | if(k<=1) 5 | return 0; 6 | int n=nums.size(); 7 | int prod=1,left=0, res=0; 8 | 9 | for(int right=0;right=k) 14 | { 15 | prod=prod/nums[left++]; 16 | } 17 | 18 | res+=right-left+1; 19 | } 20 | 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Leetcode/Arrays/subarray_sum_equals_k.cpp: -------------------------------------------------------------------------------- 1 | //O(n^2) solution with space complexity O(1) 2 | 3 | class Solution { 4 | public: 5 | int subarraySum(vector& nums, int k) { 6 | 7 | int n=nums.size(); 8 | int count=0; 9 | for(int start=0;start& nums, int k) { 31 | 32 | int n=nums.size(); 33 | int count=0; 34 | 35 | unordered_map mp; 36 | int sum=0; 37 | mp[0]=1; 38 | for(int i=0;i> subsets(vector& nums) { 5 | 6 | int n=nums.size(); 7 | int i; 8 | int size=1<> res(size); 10 | 11 | 12 | for(i=0;i& nums) { 4 | int n=nums.size(); 5 | if(n<1) 6 | return -1; 7 | 8 | 9 | int firstMax=NULL, secondMax=NULL, thirdMax=NULL; 10 | 11 | for(int i=0;ifirstMax) 17 | { 18 | thirdMax=secondMax; 19 | secondMax=firstMax; 20 | firstMax=nums[i]; 21 | } 22 | 23 | else if(secondMax==NULL || nums[i]>secondMax) 24 | { 25 | thirdMax=secondMax; 26 | secondMax=nums[i]; 27 | } 28 | 29 | else if(thirdMax==NULL || nums[i]> thirdMax) 30 | thirdMax=nums[i]; 31 | } 32 | 33 | if(thirdMax==NULL) 34 | return firstMax; 35 | else 36 | { 37 | return thirdMax; 38 | } 39 | 40 | 41 | } 42 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/valid-sudoku.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValidSudoku(vector>& board) { 4 | 5 | int row[9][9]={0}, col[9][9]={0}, block[9][9]={0}; 6 | 7 | for(int i=0;i<9;i++) 8 | { 9 | for(int j=0;j<9;j++) 10 | { 11 | if(board[i][j]=='.') 12 | continue; 13 | int num=board[i][j]-'0'-1; 14 | int row_num=i, col_num=j, block_num=(i/3)*3+j/3; 15 | if(row[row_num][num] || col[col_num][num] || block[block_num][num]) 16 | return false; 17 | 18 | row[row_num][num]=col[col_num][num]=block[block_num][num]=1; 19 | } 20 | } 21 | 22 | return true; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leetcode/Arrays/valid_triangle_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int triangleNumber(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | 6 | int n=nums.size(); 7 | int res=0; 8 | for(int third=n-1;third>=2;third--) 9 | { 10 | 11 | int first=0,second=third-1; 12 | while(firstnums[third]) 15 | { 16 | //if this condition is satisfied, all elements left of first will also be valid for traingle, total possible = second-first 17 | res+=second-first; 18 | second--; 19 | 20 | } 21 | else 22 | first++; 23 | 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Binary Searching/binary-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | 5 | int n=nums.size(); 6 | int low=0, high=n-1; 7 | 8 | while(low<=high) 9 | { 10 | int mid=(low+high)/2; 11 | 12 | if( nums[mid]==target) 13 | return mid; 14 | else if(nums[mid]& nums) { 4 | 5 | int low=0, high=nums.size()-1; 6 | if(nums.size()==0) 7 | return -1; 8 | 9 | while(low& nums) { 4 | 5 | int n=nums.size(); 6 | int low=0,high=n-1; 7 | 8 | while(low& letters, char target) { 4 | 5 | int n=letters.size(); 6 | if(target>=letters[n-1] || targettarget) 16 | { 17 | pos=mid; 18 | right=mid-1; 19 | } 20 | else 21 | left=mid+1; 22 | } 23 | 24 | return letters[pos]; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leetcode/Binary Searching/is-perfect-square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int num) { 4 | 5 | if(num==1) 6 | return true; 7 | 8 | 9 | 10 | int low=1,high=num/2; 11 | 12 | while(low<=high) 13 | { 14 | int mid=low+(high-low)/2; 15 | 16 | if(mid==num/mid && num%mid==0) 17 | return true; 18 | else if(mid>& matrix, int k) { 4 | 5 | int n=matrix.size(); 6 | if(n==0) 7 | return 0; 8 | if(matrix[0].size()==0) 9 | return 0; 10 | 11 | int low=matrix[0][0]; 12 | int high=matrix[n-1][n-1]; 13 | 14 | while(low s; 17 | 18 | while(true) 19 | { 20 | while(root!=NULL) 21 | { 22 | s.push(root); 23 | root=root->left; 24 | } 25 | 26 | root=s.top(); 27 | s.pop(); 28 | k--; 29 | if(k==0) 30 | return root->val; 31 | 32 | root=root->right; 33 | 34 | 35 | } 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Binary Searching/search-a-2d-matrix-II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | 5 | int m=matrix.size(); 6 | if(m==0) 7 | return false; 8 | int n=matrix[0].size(); 9 | int i=0,j=n-1; 10 | 11 | while(i>=0 && i<=m-1 && j>=0 && j<=n-1) 12 | { 13 | int mid=matrix[i][j]; 14 | 15 | if(target==mid) 16 | return true; 17 | else if(target>& matrix, int target) { 4 | 5 | int m=matrix.size(); 6 | if(m==0) 7 | return false; 8 | int n=matrix[0].size(); 9 | 10 | 11 | 12 | int col=n-1; 13 | int row=0; 14 | 15 | while(row=0) 16 | { 17 | if(target==matrix[row][col]) 18 | return true; 19 | else if(target& nums, int target) { 4 | 5 | int left=0,right=nums.size()-1; 6 | 7 | while(left<=right) 8 | { 9 | int mid=left+(right-left)/2; 10 | 11 | if(nums[mid]==target) 12 | return mid; 13 | else if(nums[mid] countBits(int num) { 4 | 5 | vector res={0,1}; 6 | 7 | if(num==0) 8 | return {0}; 9 | 10 | if(num==1) 11 | return {0,1}; 12 | 13 | for(int i=2;i<=num;i++) 14 | { 15 | if(i%2==0) 16 | { 17 | res.push_back(res[i/2]); 18 | } 19 | else 20 | res.push_back(1+res[i/2]); 21 | } 22 | 23 | return res; 24 | 25 | 26 | } 27 | }; -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/divide-two-integers.cpp: -------------------------------------------------------------------------------- 1 | int divide(int dividend, int divisor) { 2 | if (dividend == INT_MIN && divisor == -1) { 3 | return INT_MAX; 4 | } 5 | long dvd = labs(dividend), dvs = labs(divisor), ans = 0; 6 | int sign = dividend > 0 ^ divisor > 0 ? -1 : 1; 7 | while (dvd >= dvs) { 8 | long temp = dvs, m = 1; 9 | while (temp << 1 <= dvd) { 10 | temp <<= 1; 11 | m <<= 1; 12 | } 13 | dvd -= temp; 14 | ans += m; 15 | } 16 | return sign * ans; 17 | } -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/number-of-1-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int count=0; 5 | 6 | while(n) 7 | { 8 | n=n&(n-1); //returns the last set bit 9 | count++; 10 | } 11 | 12 | return count; 13 | } 14 | }; 15 | 16 | //10011-> 1001 -> 1 hence count=3; -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/power-x-n.cpp: -------------------------------------------------------------------------------- 1 | //9=1001 2 | //x^9=x^(2^3)*x^(2^0); 3 | 4 | class Solution { 5 | public: 6 | double myPow(double x, int n) { 7 | double ans=1; 8 | long absN=abs((long)n); 9 | 10 | while(absN>0) 11 | { 12 | if((absN&1)==1) 13 | ans=ans*x; 14 | 15 | absN=absN>>1; 16 | x=x*x; 17 | } 18 | 19 | return (n<0)? 1/ans:ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/reverse-bits.cpp: -------------------------------------------------------------------------------- 1 | //we create an unsigned mask of 32 bits, we check the last bit of n 2 | 3 | class Solution { 4 | public: 5 | uint32_t reverseBits(uint32_t n) { 6 | uint32_t mask=1<<31; 7 | int res=0; 8 | 9 | for(int i=0;i<32;i++) 10 | { 11 | if(n&1) //check the last bit of n 12 | { 13 | res=res|mask; //if lat bit is 1, we OR it with our Mask to create a reverse 14 | } 15 | 16 | mask=mask>>1; //right shift both mask and nnumber 17 | n=n>>1; 18 | } 19 | 20 | return res; 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/reverse-number.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prajwal714/DSA-Coding-Practice/fc8ccaa3feedf62102736feb39eb2cc79b5c0a5b/Leetcode/Bit Manipulation/reverse-number.cpp -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/single-number-II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | 5 | int res=0; 6 | for(int i=0;i<32;i++) 7 | { 8 | int bit=0; 9 | for(int val:nums) 10 | { 11 | if(val&(1< singleNumber(vector& nums) { 4 | 5 | int x=0; 6 | 7 | for(int val:nums) 8 | { 9 | x^=val; 10 | } 11 | //x return the first XOR second value; 12 | //each set bit in x either belongs to first or second, therefore we find the first set bit in x 13 | int first=0,second=0; 14 | int bit; 15 | for(int i=0;i<32;i++) 16 | { 17 | if(x&(1< {first,second}; 38 | } 39 | 40 | }; -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/single-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | 5 | int missing=0; 6 | for(int i=0;i>& A) 13 | { 14 | vector> dp(A.size(), vector(A[0].size(), INT_MAX)); 15 | 16 | for(int j=0; j leastPerfectSquare(n+1, INT_MAX); 14 | leastPerfectSquare[0] = 0; 15 | 16 | for(int i=1; i<=n; i++) 17 | { 18 | for(int j=1; j*j<=i; j++) 19 | { 20 | leastPerfectSquare[i] = min(leastPerfectSquare[i], leastPerfectSquare[i-j*j]+1); 21 | } 22 | } 23 | return leastPerfectSquare[n]; 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode/DP/house-robber-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums) { 4 | if(nums.size()==0) 5 | return 0; 6 | if(nums.size()==1) 7 | return nums[0]; 8 | 9 | vector A(nums.begin(), nums.end()-1); 10 | vector B(nums.begin()+1, nums.end()); 11 | 12 | return max(robSum(A), robSum(B)); 13 | } 14 | 15 | int robSum(vector &nums) 16 | { 17 | if(nums.size()==0) 18 | return 0; 19 | if(nums.size()==1) 20 | return nums[0]; 21 | int prevprev=0; 22 | int prev=0; 23 | int res; 24 | for(int i=0;i& nums) { 4 | 5 | //we have two choices, we can rob the current bank and the bank before the previous 6 | // or rob the previous bank and previous previsous banks 7 | int n=nums.size(); 8 | if(n==0) 9 | return 0; 10 | vector dp(n+1,0); 11 | 12 | dp[0]=0; 13 | dp[1]=nums[0]; 14 | 15 | for(int i=1;i& nums) { 32 | 33 | //we have two choices, we can rob the current bank and the bank before the previous 34 | // or rob the previous bank and previous previsous banks 35 | int n=nums.size(); 36 | if(n==0) 37 | return 0; 38 | 39 | 40 | int prev=0; 41 | int prevprev=0; 42 | int curr; 43 | for(int i=0;i duplicate; 4 | vector original; 5 | 6 | Solution(vector& nums) { 7 | original.assign(nums.begin(), nums.end()); 8 | duplicate.assign(nums.begin(), nums.end()); 9 | } 10 | 11 | /** Resets the array to its original configuration and return it. */ 12 | vector reset() { 13 | 14 | duplicate.assign(original.begin(), original.end()); 15 | return original; 16 | } 17 | 18 | /** Returns a random shuffling of the array. */ 19 | vector shuffle() { 20 | 21 | for(int i=0;i param_1 = obj->reset(); 36 | * vector param_2 = obj->shuffle(); 37 | */ -------------------------------------------------------------------------------- /Leetcode/Graphs/Employee Importance.cpp: -------------------------------------------------------------------------------- 1 | /* Find the total importace of employee with id */ 2 | 3 | /* 4 | // Definition for Employee. 5 | class Employee { 6 | public: 7 | int id; 8 | int importance; 9 | vector subordinates; 10 | }; 11 | */ 12 | 13 | class Solution { 14 | public: 15 | int getImportance(vector employees, int id) 16 | { 17 | unordered_map m; 18 | for(auto emp: employees) 19 | m[emp->id] = emp; 20 | 21 | stack todo; 22 | todo.push(m[id]); 23 | 24 | int importance = 0; 25 | 26 | while(!todo.empty()) 27 | { 28 | auto emp = todo.top(); 29 | todo.pop();; 30 | 31 | importance += emp->importance; 32 | 33 | for(int e: emp->subordinates) 34 | todo.push(m[e]); 35 | } 36 | return importance; 37 | } 38 | }; -------------------------------------------------------------------------------- /Leetcode/Graphs/dfs.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | 9 | cin>>t; 10 | while(t--) 11 | { 12 | int n,e; 13 | cin>>n; 14 | 15 | vector g[n]; 16 | 17 | for(int i=0;i>u>>v; 21 | g[u].push_back(v); 22 | g[v].push_back(u); 23 | } 24 | vector vis(n); 25 | 26 | dfs(0, g, vis); 27 | 28 | } 29 | } 30 | 31 | void dfs(int start, vector g[], vector &vis) 32 | { 33 | vis[start]=true; 34 | cout<> floodFill(vector>& image, int sr, int sc, int newColor) { 4 | 5 | int m=image.size(), n=image[0].size(); 6 | 7 | queue> q; 8 | q.push({sr,sc}); 9 | int color=image[sr][sc]; 10 | image[sr][sc]=newColor; 11 | vector> dir={{0,1}, {1,0}, {0,-1}, {-1,0}}; 12 | vector> visited(m, vector(n, false)); 13 | 14 | visited[sr][sc]=true; 15 | 16 | while(!q.empty()) 17 | { 18 | pair temp=q.front(); 19 | q.pop(); 20 | 21 | for(auto d: dir) 22 | { 23 | int x=temp.first+d[0]; 24 | int y=temp.second+d[1]; 25 | 26 | if(x>=0 && x=0 && y>& grid) { 4 | 5 | int ans=INT_MIN; 6 | 7 | int m=grid.size(), n=grid[0].size(); 8 | 9 | for(int i=0;i> &grid, int x, int y) 26 | { 27 | if(x<0 || y<0 || x>=grid.size() || y>=grid[0].size() || grid[x][y]==0 || grid[x][y]==-1) 28 | return 0; 29 | 30 | grid[x][y]=-1; 31 | 32 | return 1+ dfs(grid,x+1,y)+dfs(grid, x, y+1)+ dfs(grid, x-1, y) + dfs(grid, x,y-1); 33 | 34 | 35 | 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Graphs/number-of-islands.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIslands(vector>& grid) { 4 | 5 | int res=0; 6 | 7 | for(int i=0;i> &grid, int x, int y) 24 | { 25 | if(x<0 || x>=grid.size()|| y<0 || y>=grid[0].size() || grid[x][y]=='0') 26 | return; 27 | 28 | grid[x][y]='0'; 29 | 30 | dfs(grid, x+1,y); 31 | dfs(grid, x,y+1); 32 | dfs(grid,x,y-1); 33 | dfs(grid, x-1,y); 34 | } 35 | }; -------------------------------------------------------------------------------- /Leetcode/Graphs/perimeter-of-island.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int islandPerimeter(vector>& grid) { 4 | 5 | int m=grid.size(); 6 | int n=grid[0].size(); 7 | 8 | int res=0; 9 | 10 | for(int i=0;i& manager, vector& informTime) { 5 | 6 | if(n==0) 7 | return 0; 8 | 9 | vector> graph(n); 10 | int i; 11 | 12 | for(i=0;i &informTime, vector> &graph) 30 | { 31 | 32 | int time=0; 33 | 34 | for(int child: graph[manager]) 35 | { 36 | time=max(time, dfs(child, informTime, graph)); 37 | } 38 | 39 | return time+informTime[manager]; 40 | } 41 | 42 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/delete-a-node-in-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | 13 | //swap the value with current node to next node, 14 | //then delete the next node; 15 | 16 | int temp=node->val; 17 | node->val=node->next->val; 18 | node->next->val=temp; 19 | 20 | ListNode* tempNode=node->next; 21 | node->next=node->next->next; 22 | 23 | delete(tempNode); 24 | 25 | return; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/intersection-of-two-linkedi-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | 10 | 11 | //in case their is no intersection between both list, after a certain time first list will be 12 | // traversed nA+nB times and second will be traversed nB+nA times, ie both will be null 13 | //at same time, and thus terminate. returning null;' 14 | class Solution { 15 | public: 16 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 17 | 18 | ListNode* p1=headA, *p2=headB; 19 | 20 | if(p1==NULL || p2==NULL) 21 | return NULL; 22 | 23 | while(p1!=NULL && p2!=NULL && p1!=p2) 24 | { 25 | p1=p1->next; 26 | p2=p2->next; 27 | 28 | if(p1==p2) 29 | return p1; 30 | 31 | if(p1==NULL) 32 | p1=headB; 33 | if(p2==NULL) 34 | p2=headA; 35 | } 36 | 37 | return p1; 38 | } 39 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/linked-list-cycle-ii.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *detectCycle(ListNode *head) { 12 | 13 | ListNode* hare=head, *tortoise=head; 14 | 15 | while(hare!=NULL && hare->next!=NULL) 16 | { 17 | hare=hare->next->next; 18 | tortoise=tortoise->next; 19 | 20 | if(hare==tortoise) 21 | break; 22 | 23 | } 24 | 25 | if(hare==NULL || hare->next==NULL) 26 | return NULL; 27 | 28 | tortoise=head; 29 | while(tortoise!=hare) 30 | { 31 | hare=hare->next; 32 | tortoise=tortoise->next; 33 | } 34 | 35 | return hare; 36 | 37 | } 38 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/linkedi-list-cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | 13 | if(head==NULL) 14 | return false; 15 | 16 | ListNode* slow=head, *fast=head; 17 | 18 | while(fast!=NULL) 19 | { 20 | slow=slow->next; 21 | 22 | if(fast->next) 23 | fast=fast->next->next; 24 | else 25 | break; 26 | 27 | 28 | if(slow==fast) 29 | return true; 30 | } 31 | 32 | return false; 33 | } 34 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/merge-sorted-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 14 | 15 | if(l1==NULL) 16 | return l2; 17 | 18 | if(l2==NULL) 19 | return l1; 20 | 21 | ListNode* pseudo=new ListNode(0); 22 | 23 | ListNode* curr=pseudo; 24 | 25 | while(l1!=NULL && l2!=NULL) 26 | { 27 | if(l1->valval) 28 | { 29 | curr->next=l1; 30 | l1=l1->next; 31 | } 32 | else 33 | { 34 | curr->next=l2; 35 | l2=l2->next; 36 | } 37 | 38 | curr=curr->next; 39 | 40 | } 41 | 42 | if(l1==NULL) 43 | curr->next=l2; 44 | else 45 | curr->next=l1; 46 | 47 | return pseudo->next; 48 | } 49 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/odd-even-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* oddEvenList(ListNode* head) { 14 | 15 | if(head==NULL) 16 | return NULL; 17 | 18 | ListNode* odd=head, *even=head->next; 19 | ListNode* evenHead=head->next; 20 | while(even!=NULL && even->next!=NULL) 21 | { 22 | odd->next=odd->next->next; 23 | odd=odd->next; 24 | 25 | even->next=even->next->next; 26 | even=even->next; 27 | 28 | } 29 | 30 | odd->next=evenHead; 31 | 32 | return head; 33 | 34 | 35 | 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/palindrome-linkedi-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | bool isPalindrome(ListNode* head) { 14 | 15 | if(head==NULL) 16 | return true; 17 | 18 | ListNode* fast=head, *slow=head; 19 | 20 | while(fast!=NULL && fast->next!=NULL) 21 | { 22 | fast=fast->next->next; 23 | slow=slow->next; 24 | 25 | 26 | } 27 | 28 | if(fast) 29 | slow=slow->next; 30 | 31 | 32 | ListNode* curr=slow, *prev=NULL; 33 | while(curr!=NULL) 34 | { 35 | ListNode* temp=curr->next; 36 | curr->next=prev; 37 | prev=curr; 38 | curr=temp; 39 | } 40 | 41 | while(prev) 42 | { 43 | if(head->val !=prev->val) 44 | return false; 45 | 46 | head=head->next; 47 | prev=prev->next; 48 | } 49 | 50 | return prev==NULL; 51 | 52 | 53 | } 54 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/remove-linkedi-list-elements.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* removeElements(ListNode* head, int val) { 14 | 15 | 16 | ListNode* pHead=new ListNode(0); 17 | pHead->next=head; 18 | 19 | ListNode* curr=pHead; 20 | while(curr) 21 | { 22 | if(curr->next&&curr->next->val==val) 23 | { 24 | ListNode* tmp=curr->next; 25 | curr->next=curr->next->next; 26 | delete(tmp); 27 | } 28 | else 29 | curr=curr->next; 30 | } 31 | 32 | return pHead->next; 33 | 34 | 35 | 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/reverse-linked-list-ii.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* reverseBetween(ListNode* head, int m, int n) { 14 | 15 | ListNode* prv=NULL, *curr=head; 16 | 17 | if(head==NULL) 18 | return head; 19 | 20 | while(m>1) 21 | { 22 | prv=curr; 23 | curr=curr->next; 24 | 25 | m--; 26 | n--; 27 | } 28 | 29 | ListNode* tail=curr, *conn=prv; 30 | ListNode* temp=NULL; 31 | while(n>0) 32 | { 33 | temp=curr->next; 34 | curr->next=prv; 35 | 36 | prv=curr; 37 | curr=temp; 38 | n--; 39 | } 40 | if(conn==NULL) 41 | { 42 | head=prv; 43 | } 44 | else 45 | conn->next=prv; 46 | 47 | tail->next=curr; 48 | 49 | return head; 50 | } 51 | 52 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/reverse-linkedi-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* reverseList(ListNode* head) { 14 | 15 | ListNode* prv, *curr=head; 16 | prv=NULL; 17 | 18 | 19 | while(curr!=NULL) 20 | { 21 | ListNode* temp=curr->next; 22 | curr->next=prv; 23 | prv=curr; 24 | curr=temp; 25 | 26 | 27 | } 28 | 29 | return prv; 30 | } 31 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/rotate-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* rotateRight(ListNode* head, int k) { 14 | 15 | if(head==NULL) 16 | return head; 17 | 18 | int size=1; 19 | 20 | ListNode* tail=head; 21 | 22 | 23 | while(tail->next!=NULL) 24 | { 25 | size++; 26 | tail=tail->next; 27 | } 28 | k=k%size; 29 | if(k==0) 30 | return head; 31 | 32 | 33 | tail->next=head; 34 | 35 | 36 | 37 | 38 | for(int i=0;inext; 41 | } 42 | 43 | ListNode* newHead=tail->next; 44 | tail->next=NULL; 45 | 46 | return newHead; 47 | 48 | 49 | 50 | } 51 | }; -------------------------------------------------------------------------------- /Leetcode/Math/add-digit.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | 5 | while(num>9) 6 | { 7 | int n=num; 8 | int sum=0; 9 | while(n!=0) 10 | { 11 | int rem=n%10; 12 | sum+=rem; 13 | n/=10; 14 | } 15 | num=sum; 16 | } 17 | 18 | return num; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leetcode/Math/factorial-trailing-zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trailingZeroes(int n) { 4 | 5 | int num=n; 6 | 7 | int k=1; 8 | int count=0; 9 | while(num/pow(5,k)) 10 | { 11 | count+=(num/pow(5,k)); 12 | k++; 13 | } 14 | 15 | return count; 16 | 17 | 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode/Math/happy-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOfSq(int n) 4 | { 5 | int sum=0; 6 | while(n!=0) 7 | { 8 | int rem=n%10; 9 | sum+=rem*rem; 10 | n/=10; 11 | } 12 | return sum; 13 | } 14 | 15 | bool isHappy(int n) { 16 | 17 | int slow=n,fast=n; 18 | do{ 19 | slow=sumOfSq(slow); 20 | fast=sumOfSq(fast); 21 | fast=sumOfSq(fast); 22 | 23 | 24 | } 25 | while(slow!=fast); 26 | 27 | return(slow==1); 28 | 29 | } 30 | }; -------------------------------------------------------------------------------- /Leetcode/Math/my-pow.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | 5 | if(x==1) 6 | return 1; 7 | 8 | if(n==0) 9 | return 1; 10 | if(n<0) 11 | { 12 | if(n==INT_MIN) //this is to handle the case t avoid overflow of INT_MIN on -ve to +ve 13 | n+=2; 14 | 15 | n=-n; 16 | x=1/x; 17 | return myPow(x,n); 18 | } 19 | 20 | if(n%2==0) 21 | return myPow(x*x, n/2); 22 | else 23 | return x*myPow(x*x, n/2); 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode/Math/plus-one.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& digits) { 4 | 5 | vector res(digits.begin(), digits.end()); 6 | 7 | reverse(res.begin(), res.end()); 8 | 9 | int carry=1; 10 | 11 | for(int i=0;i1) 18 | return false; 19 | else 20 | return true; 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode/Priority Queue/find-k-pairs-with-smallest-sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prajwal714/DSA-Coding-Practice/fc8ccaa3feedf62102736feb39eb2cc79b5c0a5b/Leetcode/Priority Queue/find-k-pairs-with-smallest-sum.cpp -------------------------------------------------------------------------------- /Leetcode/Priority Queue/k-closest-points.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> kClosest(vector>& points, int K) { 4 | 5 | 6 | priority_queue> pq; 7 | int n=points.size(); 8 | if(n<1) 9 | { 10 | vector>({}); 11 | } 12 | 13 | for(int i=0;ik) 22 | pq.pop(); 23 | } 24 | vector> res; 25 | while(pq.size()!=0) 26 | { 27 | res.push_back(points[pq.top().second]); 28 | pq.pop(); 29 | } 30 | 31 | return res; 32 | 33 | 34 | 35 | 36 | } 37 | 38 | 39 | }; -------------------------------------------------------------------------------- /Leetcode/Priority Queue/k-th-largest-element-in-a-stream.cpp: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | public: 3 | priority_queue, greater> pq; 4 | int k; 5 | KthLargest(int k, vector& nums) { 6 | 7 | this->k=k; 8 | for(int n:nums) 9 | { 10 | pq.push(n); 11 | if(pq.size()>k) 12 | pq.pop(); 13 | } 14 | } 15 | 16 | int add(int val) { 17 | pq.push(val); 18 | if(pq.size()>k) 19 | pq.pop(); 20 | 21 | return pq.top(); 22 | } 23 | }; 24 | 25 | /** 26 | * Your KthLargest object will be instantiated and called as such: 27 | * KthLargest* obj = new KthLargest(k, nums); 28 | * int param_1 = obj->add(val); 29 | */ -------------------------------------------------------------------------------- /Leetcode/Priority Queue/k-th-largest-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& nums, int k) { 4 | 5 | if(nums.size()<1 || k>nums.size()) 6 | return -1; 7 | 8 | priority_queue, greater> pq; 9 | int n=nums.size(); 10 | 11 | for(int i=0;ik) 16 | pq.pop(); 17 | } 18 | 19 | return pq.top(); 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /Leetcode/Priority Queue/merge-k-sorted-lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | struct compare{ 12 | bool operator()(ListNode* l, ListNode* r) 13 | { 14 | return l->val>r->val; 15 | } 16 | }; 17 | 18 | 19 | 20 | class Solution { 21 | public: 22 | ListNode* mergeKLists(vector& lists) { 23 | 24 | priority_queue, compare>pq; 25 | 26 | for(auto l:lists) 27 | { 28 | if(l!=NULL) 29 | pq.push(l); 30 | } 31 | 32 | if(pq.empty()) 33 | return NULL; 34 | 35 | ListNode* res=pq.top(); 36 | pq.pop(); 37 | 38 | if(res->next) 39 | pq.push(res->next); 40 | 41 | ListNode* tail=res; 42 | 43 | while(!pq.empty()) 44 | { 45 | tail->next=pq.top(); 46 | pq.pop(); 47 | 48 | tail=tail->next; 49 | if(tail->next) 50 | pq.push(tail->next); 51 | } 52 | 53 | return res; 54 | } 55 | }; -------------------------------------------------------------------------------- /Leetcode/Priority Queue/top-k-frequent-elements.cpp: -------------------------------------------------------------------------------- 1 | //time complexity: O(n) and space complexity O(n+k) 2 | 3 | class Solution { 4 | public: 5 | vector topKFrequent(vector& nums, int k) { 6 | 7 | if(nums.size()<1) 8 | return vector(); 9 | 10 | unordered_map mp; 11 | for(int n:nums) 12 | mp[n]++; 13 | 14 | priority_queue> pq; 15 | vector res; 16 | for(auto it=mp.begin();it!=mp.end();it++) 17 | { 18 | pq.push({it->second, it->first}); 19 | 20 | if(pq.size()>mp.size()-k) 21 | { 22 | res.push_back(pq.top().second); 23 | pq.pop(); 24 | } 25 | } 26 | 27 | return res; 28 | 29 | } 30 | }; -------------------------------------------------------------------------------- /Leetcode/Priority Queue/top-k-frequent-words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | struct compare{ 4 | bool operator()(pair a, pair b) 5 | { 6 | if(a.first==b.first) 7 | { 8 | return (a.secondb.first; 12 | } 13 | }; 14 | 15 | 16 | vector topKFrequent(vector& words, int k) { 17 | 18 | unordered_map mp; 19 | priority_queue, vector>, compare> pq; 20 | for(string s:words) 21 | { 22 | mp[s]++; 23 | } 24 | 25 | for(auto it=mp.begin();it!=mp.end();it++) 26 | { 27 | pq.push({it->second, it->first}); 28 | 29 | if(pq.size()>k) 30 | pq.pop(); 31 | } 32 | 33 | vector res; 34 | 35 | while(!pq.empty()) 36 | { 37 | res.push_back(pq.top().second); 38 | pq.pop(); 39 | } 40 | reverse(res.begin(), res.end()); 41 | return res; 42 | 43 | 44 | } 45 | }; -------------------------------------------------------------------------------- /Leetcode/Recursion/generate-paranthesis.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | vector generateParenthesis(int n) { 5 | vector res; 6 | 7 | solve(n,n,"",res); 8 | 9 | return res; 10 | } 11 | 12 | void solve(int open, int close, string op, vector &res) 13 | { 14 | if(open==0 && close==0) 15 | { 16 | res.push_back(op); 17 | return; 18 | } 19 | 20 | if(open!=0) 21 | { 22 | string op1=op; 23 | op1.push_back('('); 24 | 25 | solve(open-1, close, op1, res); 26 | 27 | } 28 | //we can add a closing ( only when close>open, otherwise it will become a invalid string 29 | if(close>open) 30 | { 31 | string op2=op; 32 | op2.push_back(')'); 33 | 34 | solve(open,close-1,op2, res); 35 | 36 | } 37 | 38 | return; 39 | } 40 | }; -------------------------------------------------------------------------------- /Leetcode/Stack/Minimum Add to make Parenthesis Valid.cpp: -------------------------------------------------------------------------------- 1 | /* A string is valid, when it's counter of parenthesis is balanced = 0. And we represent balance of a string as (open_branckets-closing_brackets) and also for every prefix, the opening_brackets 2 | should be non negative. 3 | 4 | Here 'open' represents number of extra opening brackets, that requires more closing brackets for getting balanced. 5 | Similarly 'close' represents number of extra closing brackets, that requires addition of more opening brackets. 6 | 7 | Ex: "()))((" O/P = 4 8 | 9 | */ 10 | 11 | class Solution { 12 | public: 13 | int minAddToMakeValid(string S) { 14 | int open=0, close=0; 15 | for(int i=0;i s; 5 | stack temp; 6 | MinStack() { 7 | 8 | } 9 | 10 | void push(int x) { 11 | s.push(x); 12 | 13 | if(temp.empty()) 14 | temp.push(x); 15 | else if(x<=temp.top()) 16 | temp.push(x); 17 | } 18 | 19 | void pop() { 20 | if(s.empty()) 21 | return; 22 | 23 | if(s.top()==temp.top()) 24 | temp.pop(); 25 | 26 | s.pop(); 27 | } 28 | 29 | int top() { 30 | return s.top(); 31 | } 32 | 33 | int getMin() { 34 | return temp.top(); 35 | } 36 | }; 37 | 38 | /** 39 | * Your MinStack object will be instantiated and called as such: 40 | * MinStack* obj = new MinStack(); 41 | * obj->push(x); 42 | * obj->pop(); 43 | * int param_3 = obj->top(); 44 | * int param_4 = obj->getMin(); 45 | */ 46 | 47 | //===========================================O(1) space complexity solution 48 | 49 | -------------------------------------------------------------------------------- /Leetcode/Stack/valid-paranthesis.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string st) { 4 | 5 | 6 | stack s; 7 | 8 | for(int i=0;ileft==NULL && valval) 20 | { 21 | TreeNode* temp=new TreeNode(val); 22 | root->left=temp; 23 | return root; 24 | 25 | } 26 | else if(root->right==NULL && val>root->val) 27 | { 28 | TreeNode* temp=new TreeNode(val); 29 | root->right=temp; 30 | return root; 31 | } 32 | 33 | if(valval) 34 | insertIntoBST(root->left, val); 35 | else 36 | insertIntoBST(root->right, val); 37 | 38 | return root; 39 | 40 | } 41 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/balanced-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isBalanced(TreeNode* root) { 15 | 16 | if(root==NULL) 17 | return true; 18 | 19 | bool res=true; 20 | height(root, res); 21 | return res; 22 | } 23 | 24 | int height(TreeNode* root, bool &res) 25 | { 26 | if(root==NULL) 27 | return 0; 28 | 29 | int left=height(root->left, res); 30 | int right=height(root->right, res); 31 | 32 | int ht=max(left,right)+1; 33 | int diff=abs(left-right); 34 | if(diff>1) 35 | res=false; 36 | 37 | return ht; 38 | } 39 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/binary-tree-camera.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int res; 15 | int minCameraCover(TreeNode* root) { 16 | res=0; 17 | 18 | if( dfs(root)<1) 19 | res+=1; 20 | 21 | return res; 22 | 23 | } 24 | 25 | int dfs(TreeNode* root) 26 | { 27 | if(root==NULL) 28 | return 2; 29 | 30 | int left=dfs(root->left); 31 | int right=dfs(root->right); 32 | 33 | if(left==0||right==0) 34 | { 35 | res++; 36 | return 1; //parent of leaf node 37 | } 38 | 39 | if(right==1||left==1) 40 | { 41 | return 2; //grad parent of leaf node 42 | } 43 | else 44 | return 0; //leaf ode 45 | } 46 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/binary-tree-max-path-sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int res; 15 | int maxPathSum(TreeNode* root) { 16 | if(root==NULL) 17 | return 0; 18 | 19 | res=INT_MIN; 20 | maxSum(root); 21 | return res; 22 | 23 | } 24 | 25 | int maxSum(TreeNode* root) 26 | { 27 | if(root==NULL) 28 | return 0; 29 | 30 | int left=maxSum(root->left); 31 | if(left<0) 32 | left=0; 33 | int right=maxSum(root->right); 34 | if(right<0) 35 | right=0; 36 | 37 | int ans=max(max(left,right)+root->val, root->val); 38 | 39 | res=max(res, left+right+root->val); 40 | 41 | return ans; 42 | } 43 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/binary-tree-postorder.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector postorderTraversal(TreeNode* root) { 15 | 16 | stack s; 17 | 18 | if(root==NULL) 19 | return vector {}; 20 | 21 | 22 | s.push(root); 23 | vector res; 24 | while(!s.empty()) 25 | { 26 | TreeNode* temp=s.top(); 27 | s.pop(); 28 | 29 | if(temp->left) 30 | s.push(temp->left); 31 | 32 | if(temp->right) 33 | s.push(temp->right); 34 | 35 | res.push_back(temp->val); 36 | 37 | } 38 | 39 | reverse(res.begin(), res.end()); 40 | 41 | return res; 42 | } 43 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/closest-BST-value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | } 8 | 9 | int goal; 10 | int min=INT_MAX; 11 | 12 | void closestValue(TreeNode* root, int target){ 13 | 14 | if(root==NULL) 15 | return; 16 | 17 | if(abs(target-root->valval); 20 | goal=root->val; 21 | } 22 | 23 | if(targetval) 24 | closestValue(root->left, target); 25 | else 26 | { 27 | closestValue(root->right, target); 28 | } 29 | 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Leetcode/Trees/convert-BST-greater-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | 14 | private: 15 | int sum=0; 16 | public: 17 | TreeNode* convertBST(TreeNode* root) { 18 | if(root==NULL) 19 | return NULL ; 20 | 21 | return helper(root); 22 | } 23 | TreeNode* helper(TreeNode* root) 24 | { 25 | if(root==NULL) 26 | return NULL; 27 | 28 | if(root->right) 29 | helper(root->right); 30 | 31 | 32 | root->val=(sum+=root->val); 33 | 34 | if(root->left) 35 | helper(root->left); 36 | 37 | return root; 38 | 39 | } 40 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/convert-sorted-array-to-binart-searhc-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* sortedArrayToBST(vector& nums) { 15 | 16 | if(nums.size()==0) 17 | return NULL; 18 | 19 | if(nums.size()==1) 20 | return new TreeNode(nums[0]); 21 | 22 | int mid=nums.size()/2; 23 | TreeNode* root=new TreeNode(nums[mid]); 24 | 25 | vector left(nums.begin(), nums.begin()+mid); 26 | vector right(nums.begin()+mid+1, nums.end()); 27 | 28 | root->left=sortedArrayToBST(left); 29 | root->right=sortedArrayToBST(right); 30 | 31 | return root; 32 | 33 | } 34 | 35 | 36 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/depth-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int maxDepth(TreeNode* root) { 15 | 16 | if(root==NULL) 17 | return 0; 18 | 19 | int left=maxDepth(root->left); 20 | int right=maxDepth(root->right); 21 | 22 | return max(left,right)+1; 23 | 24 | } 25 | }; 26 | 27 | //thisd is similar to postorder traversal, we calcultae the height of left anf right subtrees, and return the max +1 28 | -------------------------------------------------------------------------------- /Leetcode/Trees/diameter-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | int res; 14 | public: 15 | int diameterOfBinaryTree(TreeNode* root) { 16 | 17 | res=1; 18 | depth(root); 19 | 20 | return res-1; 21 | 22 | } 23 | 24 | int depth(TreeNode* root) 25 | { 26 | if(root==NULL) 27 | return 0; 28 | 29 | int left=depth(root->left); 30 | int right=depth(root->right); 31 | 32 | res=max(res, left+right+1); 33 | 34 | return max(left,right)+1; 35 | 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/distribute-coins-in-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int ans; 15 | int distributeCoins(TreeNode* root) { 16 | ans=0; 17 | dfs(root); 18 | return ans; 19 | } 20 | 21 | int dfs(TreeNode* node) 22 | { 23 | if(node==NULL) 24 | return 0; 25 | 26 | int l=dfs(node->left); 27 | int r=dfs(node->right); 28 | 29 | ans+=abs(l)+abs(r); 30 | return node->val+l+r-1; 31 | } 32 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/inorder-iterative.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | stack s; 7 | vector t1; 8 | while (root1 != NULL || !s.empty()) 9 | { 10 | while (root1 != NULL) 11 | { 12 | s.push(root1); 13 | 14 | root1 = root1->left; 15 | } 16 | 17 | root1 = s.top(); 18 | s.pop(); 19 | 20 | t1.push_back(root1->val); 21 | 22 | root1 = root1->right; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Leetcode/Trees/insert-into-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* insertIntoBST(TreeNode* root, int val) { 15 | 16 | if(root==NULL) 17 | { 18 | root=new TreeNode(val); 19 | return root; 20 | } 21 | 22 | if(val>root->val) 23 | root->right=insertIntoBST(root->right, val); 24 | else 25 | root->left=insertIntoBST(root->left, val); 26 | 27 | return root; 28 | } 29 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/invert-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* invertTree(TreeNode* root) { 15 | 16 | if(root==NULL) 17 | return root; 18 | 19 | TreeNode* t=new TreeNode(root->val); 20 | t->right=invertTree(root->left); 21 | t->left=invertTree(root->right); 22 | 23 | return t; 24 | 25 | 26 | } 27 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/jump-game-iii.cpp: -------------------------------------------------------------------------------- 1 | //each arr[i] has two children i+arr[i], i-arr[i], we search in this tree for array element with zero, 2 | //time complexity: O(n) space O(n) 3 | 4 | class Solution { 5 | public: 6 | bool canReach(vector& arr, int start) { 7 | 8 | 9 | 10 | if(start>=0 && start=0) 11 | { 12 | if(arr[start]==0) 13 | return true; 14 | } 15 | else 16 | return false; 17 | 18 | arr[start]=-arr[start]; 19 | 20 | return canReach(arr, start+arr[start]) || canReach(arr, start-arr[start]); 21 | } 22 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/lowest-common-ancestor-of-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode* res; 13 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 14 | 15 | if(root==NULL) 16 | return root; 17 | 18 | if(root->val < p->val && root->val < q->val) 19 | return lowestCommonAncestor(root->right, p,q); 20 | else if(root->val > p->val && root->val > q->val) 21 | return lowestCommonAncestor(root->left,p,q); 22 | else 23 | { 24 | return root; 25 | } 26 | 27 | 28 | 29 | } 30 | 31 | 32 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/maximum-depth-N-ary-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | int maxDepth(Node* root) { 24 | 25 | return depth(root); 26 | } 27 | 28 | 29 | int depth(Node* root) 30 | { 31 | if(root==NULL) 32 | return 0; 33 | 34 | int max_depth=0; 35 | 36 | for(auto child:root->children) 37 | { 38 | int child_depth=depth(child); 39 | 40 | max_depth=max(max_depth, child_depth); 41 | } 42 | 43 | return 1+max_depth; 44 | } 45 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/merge-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { 15 | 16 | if(t1==NULL) 17 | return t2; 18 | 19 | if(t2==NULL) 20 | return t1; 21 | 22 | TreeNode* t=new TreeNode(t1->val+t2->val); 23 | 24 | t->left=mergeTrees(t1->left, t2->left); 25 | t->right=mergeTrees(t1->right, t2->right); 26 | 27 | return t; 28 | 29 | } 30 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/min-absolute-diff-in-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int getMinimumDifference(TreeNode* root) { 15 | 16 | int res=INT_MAX; 17 | TreeNode* pre=NULL; 18 | inorder(root, pre, res); 19 | 20 | return res; 21 | 22 | } 23 | 24 | void inorder(TreeNode* root, TreeNode* &pre, int &res) 25 | { 26 | if(root==NULL) 27 | return; 28 | 29 | inorder(root->left, pre, res); 30 | 31 | if(pre!=NULL) 32 | res=min(res, abs(pre->val-root->val)); 33 | 34 | pre=root; 35 | 36 | inorder(root->right, pre, res); 37 | } 38 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/min-diff-between-BST-nodes.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | private: 14 | int res=INT_MAX,pre=-1; 15 | public: 16 | int minDiffInBST(TreeNode* root) { 17 | 18 | 19 | 20 | if(root->left) 21 | minDiffInBST(root->left); 22 | 23 | if(pre>0) 24 | res=min(res, abs(pre-root->val)); 25 | 26 | pre=root->val; 27 | 28 | if(root->right) 29 | minDiffInBST(root->right); 30 | 31 | return res; 32 | 33 | 34 | } 35 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/min-height.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int minDepth(TreeNode* root) { 15 | 16 | if(root==NULL) 17 | return 0; 18 | 19 | int left=minDepth(root->left); 20 | int right=minDepth(root->right); 21 | 22 | if((left==0 && right !=0 )|| (right==0 && left!=0)) 23 | return 1+left+right; 24 | 25 | return 1+min(left,right); 26 | } 27 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/path-in-zigzag-labelled-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector pathInZigZagTree(int label) { 4 | 5 | int level=1; 6 | 7 | while(1< res(level); 10 | for(;label>=1; label=label/2) 11 | { 12 | res[level-1]=label; 13 | 14 | 15 | label=(1<> pathSum(TreeNode* root, int target) { 15 | 16 | vector> res; 17 | int sum=0; 18 | vector v; 19 | helper(root, 0, target, res,v); 20 | return res; 21 | 22 | 23 | } 24 | 25 | void helper(TreeNode* root, int sum,int target, vector> &res, vector v) 26 | { 27 | if(root==NULL) 28 | return; 29 | 30 | 31 | v.push_back(root->val); 32 | sum+=root->val; 33 | 34 | if(root->left==NULL && root->right==NULL) 35 | { 36 | if(sum==target) 37 | res.push_back(v); 38 | return; 39 | } 40 | else 41 | { 42 | helper(root->left, sum, target, res,v); 43 | helper(root->right, sum,target, res,v); 44 | } 45 | 46 | 47 | } 48 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/path-sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool hasPathSum(TreeNode* root, int sum) { 15 | 16 | return helper(root, 0, sum); 17 | 18 | } 19 | 20 | bool helper(TreeNode* root, int sum, int target) 21 | { 22 | if(root==NULL) 23 | return false; 24 | 25 | if(root->left==NULL && root->right==NULL) 26 | { 27 | sum+=root->val; 28 | if(sum==target) 29 | return true; 30 | } 31 | 32 | bool left=helper(root->left, sum+root->val, target); 33 | bool right=helper(root->right, sum+root->val, target); 34 | 35 | return left || right; 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/postorder-iterative.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector postorderTraversal(TreeNode* root) { 15 | 16 | if(root==NULL) 17 | return {}; 18 | 19 | stack s; 20 | s.push(root); 21 | vector res; 22 | while(!s.empty()) 23 | { 24 | TreeNode* temp=s.top(); 25 | s.pop(); 26 | 27 | res.push_back(temp->val); 28 | 29 | if(temp->left) 30 | s.push(temp->left); 31 | 32 | if(temp->right) 33 | s.push(temp->right); 34 | } 35 | 36 | reverse(res.begin(), res.end()); 37 | 38 | return res; 39 | } 40 | }; 41 | 42 | //this is similar to preoder, but we just traverse the right root first, then left root, then root 43 | 44 | //and reverse the result at the end -------------------------------------------------------------------------------- /Leetcode/Trees/preorder-iterative.cpp: -------------------------------------------------------------------------------- 1 | void preorder(TreeNode* root, vector &res) 2 | { 3 | stack s; 4 | s.push(root); 5 | 6 | 7 | while(!s.empty()) 8 | { 9 | TreeNode* temp=s.top(); 10 | s.pop(); 11 | 12 | res.push_back(temp->val); 13 | 14 | if(temp->right) 15 | s.push(temp->right); 16 | 17 | if(temp->left) 18 | s.push(temp->left); 19 | 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/Trees/range-sum-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int res; 15 | int rangeSumBST(TreeNode* root, int L, int R) { 16 | res=0; 17 | 18 | dfs(root,L,R); 19 | return res; 20 | 21 | } 22 | 23 | void dfs(TreeNode* root, int l, int r) 24 | { 25 | if(root==NULL) 26 | return; 27 | 28 | if(l<=root->val && root->val<=r) 29 | res+=root->val; 30 | 31 | if(lval) 32 | dfs(root->left,l,r); 33 | 34 | if(root->valright,l,r); 36 | 37 | return; 38 | } 39 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/same-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isSameTree(TreeNode* p, TreeNode* q) { 15 | 16 | if(p==NULL && q==NULL) 17 | return true; 18 | if(p==NULL || q==NULL) 19 | return false; 20 | 21 | return (p->val==q->val)&&isSameTree(p->left, q->left)&&isSameTree(p->right, q->right); 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/search-in-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* searchBST(TreeNode* root, int val) { 15 | 16 | if(root==NULL || root->val==val) 17 | return root; 18 | 19 | if(valval) 20 | return searchBST(root->left, val); 21 | else 22 | return searchBST(root->right, val); 23 | 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/sum-of-distances-in-tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | vector> tree; 5 | vector count,res; 6 | 7 | vector sumOfDistancesInTree(int N, vector>& edges) { 8 | 9 | tree.resize(N); 10 | count.assign(N,1); 11 | res.assign(N,0); 12 | 13 | for(auto e: edges) 14 | { 15 | tree[e[0]].insert(e[1]); 16 | tree[e[1]].insert(e[0]); 17 | } 18 | 19 | dfs1(0,-1); 20 | dfs2(0,-1); 21 | 22 | return res; 23 | 24 | } 25 | 26 | void dfs1(int root, int parent) 27 | { 28 | 29 | for(int i:tree[root]) 30 | { 31 | if(i==parent) 32 | continue; 33 | 34 | dfs1(i,root); 35 | 36 | count[root]+=count[i]; 37 | res[root]+=res[i]+count[i]; 38 | } 39 | return; 40 | } 41 | 42 | void dfs2(int root, int parent) 43 | { 44 | for(int i:tree[root]) 45 | { 46 | if(i==parent) 47 | continue; 48 | 49 | res[i]=res[root]-count[i]+count.size()-count[i]; 50 | dfs2(i,root); 51 | } 52 | return; 53 | } 54 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/sum-of-left-leaves.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isLeaf(TreeNode* root) 15 | { 16 | if(root==NULL) 17 | return false; 18 | 19 | if(root->left==NULL && root->right==NULL) 20 | return true; 21 | else 22 | return false; 23 | } 24 | 25 | int sumOfLeftLeaves(TreeNode* root) { 26 | 27 | if(root==NULL) 28 | return 0; 29 | 30 | int sum=0; 31 | 32 | if(isLeaf(root->left)) 33 | sum= root->left->val; 34 | 35 | 36 | 37 | return sum+sumOfLeftLeaves(root->left)+ sumOfLeftLeaves(root->right); 38 | 39 | } 40 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/sum-of-root-to-binary-numbers.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int res; 15 | int sumRootToLeaf(TreeNode* root) { 16 | res=0; 17 | return inorder(root, 0); 18 | 19 | 20 | 21 | } 22 | 23 | int inorder(TreeNode* root, int val) 24 | { 25 | if(root==NULL) 26 | return 0; 27 | 28 | val=val*2+root->val; 29 | 30 | if(root->left==NULL && root->right==NULL) 31 | return val; 32 | else 33 | return inorder(root->left, val)+inorder(root->right, val); 34 | } 35 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/sum-root-to-leaf-paths.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int res; 15 | int sumNumbers(TreeNode* root) { 16 | 17 | res=0; 18 | helper(root, 0); 19 | 20 | return res; 21 | } 22 | 23 | int helper(TreeNode* root, int sum) 24 | { 25 | if(root==NULL) 26 | return 0; 27 | 28 | int num=sum*10+root->val; 29 | 30 | if(root->left==NULL && root->right==NULL) 31 | res+=num; 32 | 33 | return helper(root->left,num)+helper(root->right, num); 34 | } 35 | }; -------------------------------------------------------------------------------- /Leetcode/Trees/symmetric.tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isSymmetric(TreeNode* root) { 15 | 16 | return helper(root,root); 17 | } 18 | 19 | bool helper(TreeNode* root1, TreeNode* root2) 20 | { 21 | if(root1==NULL && root2==NULL) 22 | return true; 23 | 24 | if(root1==NULL || root2==NULL) 25 | return false; 26 | 27 | bool left=helper(root1->left, root2->right); //check if left subtree of root1 == rght subtree of root2 28 | bool right=helper(root1->right, root2->left); //check if right subtree of root1 == left subtree of root2 29 | 30 | bool current=(root1->val==root2->val); 31 | 32 | return current && left && right; 33 | 34 | } 35 | }; -------------------------------------------------------------------------------- /Leetcode/backtracking/combination-sum.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Input: candidates = [2,3,6,7], target = 7 3 | // Output: [[2,2,3],[7]] 4 | // Explanation: 5 | // 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 6 | // 7 is a candidate, and 7 = 7. 7 | // These are the only two combinations. 8 | 9 | class Solution { 10 | public: 11 | vector> combinationSum(vector& candidates, int target) { 12 | 13 | vector> res; 14 | sort(candidates.begin(), candidates.end()); 15 | backtrack(res, vector(), candidates, 0, target); 16 | 17 | return res; 18 | 19 | } 20 | 21 | void backtrack(vector> &res, vector temp,vector &arr, int sum, int target) 22 | { 23 | if(sum>target) 24 | return; 25 | 26 | if(sum==target) 27 | { 28 | res.push_back(temp); 29 | return; 30 | } 31 | 32 | for(int i=0;i> combinationSum2(vector& nums, int target) { 6 | 7 | vector> res; 8 | sort(nums.begin(), nums.end()); 9 | backtrack(res, vector (), nums, target, 0); 10 | 11 | return res; 12 | } 13 | 14 | void backtrack(vector> &res, vector temp, vector &nums, int target, int pos) 15 | { 16 | if(target<0) 17 | return; 18 | 19 | if(target==0) 20 | { 21 | res.push_back(temp); 22 | return; 23 | } 24 | 25 | for(int i=pos;ipos && nums[i]==nums[i-1]) 29 | continue; 30 | 31 | temp.push_back(nums[i]); 32 | backtrack(res, temp, nums, target-nums[i], i+1); 33 | temp.pop_back(); 34 | } 35 | } 36 | }; -------------------------------------------------------------------------------- /Leetcode/backtracking/combinations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> combinationSum(vector& nums, int target) { 4 | 5 | vector> res; 6 | 7 | backtrack(res, vector(),nums, target, 0); 8 | return res; 9 | } 10 | 11 | void backtrack(vector> &res, vector temp, vector nums, int target, int pos) 12 | { 13 | if(target==0) 14 | { 15 | res.push_back(temp); 16 | return; 17 | } 18 | 19 | for(int i=pos;i> mp ={ 4 | {'2', {'a','b','c'}}, 5 | {'3', {'d','e','f'}}, 6 | {'4', {'g','h','i'}}, 7 | {'5', {'j','k','l'}}, 8 | {'6', {'m','n','o'}}, 9 | {'7', {'p','q','r', 's'}}, 10 | {'8', {'t','u','v'}}, 11 | {'9', {'w','x','y', 'z'}}, 12 | }; 13 | vector letterCombinations(string digits) { 14 | vector res; 15 | if(digits.length()==0) 16 | return res; 17 | backtrack("", digits, res, 0); 18 | 19 | return res; 20 | } 21 | 22 | void backtrack(string op, string digits, vector &res, int pos) 23 | { 24 | if(op.length()==digits.length()) 25 | { 26 | res.push_back(op); 27 | return; 28 | } 29 | 30 | 31 | for(char ch: mp[digits[pos]]) 32 | { 33 | op.push_back(ch); 34 | backtrack(op, digits, res, pos+1); 35 | op.pop_back(); 36 | } 37 | 38 | 39 | 40 | } 41 | }; -------------------------------------------------------------------------------- /Leetcode/backtracking/permutations-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permuteUnique(vector& nums) { 4 | 5 | vector> res; 6 | unordered_map used; 7 | sort(nums.begin(), nums.end()); 8 | backtrack(res, vector (), used, nums); 9 | 10 | return res; 11 | } 12 | 13 | void backtrack(vector> &res, vector temp, unordered_map used, vector &nums) 14 | { 15 | if(temp.size()==nums.size()) 16 | { 17 | res.push_back(temp); 18 | return; 19 | } 20 | 21 | else 22 | { 23 | for(int i=0;i0 && nums[i]==nums[i-1] && !used[i-1])) 26 | continue; 27 | 28 | used[i]=true; 29 | temp.push_back(nums[i]); 30 | backtrack(res, temp, used, nums); 31 | 32 | used[i]=false; 33 | temp.pop_back(); 34 | } 35 | } 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/backtracking/permutations.cpp: -------------------------------------------------------------------------------- 1 | //this is for distinct elements in nums array, complexity: 2 | 3 | class Solution { 4 | public: 5 | vector> permute(vector& nums) { 6 | 7 | unordered_map mp; 8 | vector> res; 9 | backtrack(res, vector (), mp, nums); 10 | 11 | return res; 12 | } 13 | 14 | void backtrack(vector> &res, vector temp, unordered_map mp, vector nums) 15 | { 16 | if(temp.size()==nums.size()) 17 | { 18 | res.push_back(temp); 19 | 20 | } 21 | else 22 | { 23 | for(int i=0;i> subsetsWithDup(vector& nums) { 4 | 5 | vector> res; 6 | sort(nums.begin(), nums.end()); 7 | backtrack(res, vector(), 0, nums); 8 | 9 | return res; 10 | } 11 | 12 | void backtrack(vector> &res, vector temp, int pos, vector nums) 13 | { 14 | res.push_back(temp); 15 | 16 | for(int i=pos;ipos && nums[i]==nums[i-1]) 19 | continue; 20 | 21 | temp.push_back(nums[i]); 22 | backtrack(res, temp, i+1, nums); 23 | temp.pop_back(); 24 | } 25 | 26 | return; 27 | } 28 | }; -------------------------------------------------------------------------------- /Leetcode/backtracking/subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> subsetsWithDup(vector& nums) { 4 | 5 | vector> res; 6 | sort(nums.begin(), nums.end()); 7 | backtrack(res, vector(), 0, nums); 8 | 9 | return res; 10 | } 11 | 12 | void backtrack(vector> &res, vector temp, int pos, vector nums) 13 | { 14 | res.push_back(temp); 15 | 16 | for(int i=pos;i 2 | using namespace std; 3 | 4 | -------------------------------------------------------------------------------- /Leetcode/queue/queue-recontruction-by-height.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | static bool compare(vector a, vector b) 5 | { 6 | if(a[0]==b[0]) 7 | { 8 | return a[1]b[0]; 12 | } 13 | 14 | vector> reconstructQueue(vector>& people) { 15 | 16 | 17 | 18 | int n=people.size(); 19 | 20 | if(n==0) 21 | return {}; 22 | 23 | vector> res; 24 | 25 | sort(people.begin(), people.end(), compare); 26 | 27 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int maxProduct(vector &words) 8 | { 9 | 10 | vector mask(words.size()); 11 | 12 | //we create a mask of 26 bits for every word, 1 for char 13 | for (int i = 0; i < words.size(); i++) 14 | { 15 | for (char ch : words[i]) 16 | { 17 | mask[i] = mask[i] | (1 << (ch - 'a')); 18 | } 19 | } 20 | // we take a & operation between mask[i] and mask[j] of word pair to check if they are mutually exlucisve 21 | int ans = 0; 22 | for (int i = 0; i < words.size(); i++) 23 | { 24 | for (int j = i + 1; j < words.size(); j++) 25 | { 26 | if (!(mask[i] & mask[j])) 27 | { 28 | int prod = words[i].length() * words[j].length(); 29 | ans = max(ans, prod); 30 | } 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leetcode/strings/check-for-anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | 5 | unordered_map mp; 6 | 7 | int i; 8 | 9 | 10 | for(i=0;isecond!=0) 33 | return false; 34 | } 35 | 36 | return true; 37 | } 38 | }; -------------------------------------------------------------------------------- /Leetcode/strings/group-anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | 5 | unordered_map> mp; 6 | 7 | for(int i=0;i> res; 17 | 18 | for(auto v:mp) 19 | { 20 | 21 | res.push_back(v.second); 22 | } 23 | 24 | return res; 25 | } 26 | }; 27 | 28 | //time complexity of N*KlogK where k is the max length of any string, N is the size of strs 29 | 30 | 31 | // we can also use an encoding of number of count of each char eg: aabc===2#1#1#0#0.... of length 26 for each character, then the time complexity will ne N*K 32 | 33 | -------------------------------------------------------------------------------- /Leetcode/strings/longest-common-prefix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prajwal714/DSA-Coding-Practice/fc8ccaa3feedf62102736feb39eb2cc79b5c0a5b/Leetcode/strings/longest-common-prefix.cpp -------------------------------------------------------------------------------- /Leetcode/strings/palindromic-substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSubstrings(string s) { 4 | 5 | int n=s.length(); 6 | vector> dp(n, vector(n,false)); 7 | 8 | 9 | 10 | //for length 1 strings 11 | 12 | int count=0; 13 | 14 | for(int i=0;i> partition(string s) { 4 | 5 | vector> res; 6 | backtrack(res, vector(), 0, s); 7 | 8 | return res; 9 | 10 | } 11 | 12 | void backtrack(vector> &res, vector temp, int start, string s) 13 | { 14 | if(start==s.length()) 15 | { 16 | res.push_back(temp); 17 | return; 18 | } 19 | 20 | for(int i=start;i> s; 6 | int count=0; 7 | for(int i=0;i findRepeatedDnaSequences(string s) { 8 | 9 | vector res; 10 | unordered_map mp; 11 | int i=0; 12 | int n=s.length(); 13 | while(i+10<=n) 14 | { 15 | string subs=s.substr(i,10); 16 | mp[subs]++; 17 | i++; 18 | 19 | if(mp[subs]==2) 20 | res.push_back(subs); 21 | } 22 | 23 | return res; 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode/strings/reverse-string-II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseStr(string s, int k) { 4 | 5 | int n=s.length(); 6 | int i; 7 | for(i=0;i& s) { 4 | 5 | int n=s.size(); 6 | for(int i=0;i`prj-prajwal.netlify.app` 13 | - **LinkedIn** `linkedin.com/in/prajwal714` 14 | -------------------------------------------------------------------------------- /codeforces/#673(Div2)/copy-paste.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,k; 11 | cin>>n>>k; 12 | vector arr(n); 13 | int min_elem=INT_MAX; 14 | int min_index=-1; 15 | for(int i=0;i>arr[i]; 18 | if(arr[i]=k) 32 | continue; 33 | 34 | res+=(k-arr[i])/min_elem; 35 | 36 | } 37 | 38 | cout< 2 | using namespace std; 3 | 4 | bool compare(pair a, pair b) 5 | { 6 | return a.first>t; 13 | while(t--) 14 | { 15 | int n, target; 16 | cin>>n>>target; 17 | 18 | vector a(n); 19 | for(int i=0;i>a[i]; 21 | 22 | 23 | vector> arr; 24 | for(int i=0;i res(n,0); 31 | 32 | int left=0, right=n-1; 33 | 34 | while(left