├── .DS_Store ├── README.md ├── array-questions ├── .DS_Store ├── add-arrays.cpp ├── array-reverse.cpp ├── check-rotated-sorted.cpp ├── merge-sorted-same-array.cpp ├── move-zeros-end.cpp ├── reverse-array-position.cpp └── rotate-array.cpp ├── arrays-reverse-linear search ├── extracting-size.cpp ├── initialiser.cpp ├── linearsearch.cpp ├── max-min.cpp ├── reverse-arry.cpp └── swap-alternate.cpp ├── arrays ├── goal-parser.cpp ├── input-output.cpp ├── rotate-image.cpp ├── search-2d-matrix.cpp ├── spiral-matrix.cpp └── wave-print.cpp ├── basic-concepts ├── datatype-variables.cpp ├── hello-world.cpp ├── operators.cpp └── type-casting.cpp ├── binary-decimal-concept ├── binary-to-decimal.cpp ├── decimal-to-binary.cpp ├── leetcode-problem-3.cpp ├── leetcode-problem-4.cpp ├── leetcode-problem-5.cpp ├── leetcode-problem-6.cpp └── neg-decimal-to-binary.cpp ├── binary-search ├── .vscode │ └── settings.json ├── binary-search.cpp ├── book-allocation-imp.cpp ├── first-last-occurance.cpp ├── leetcode-852.cpp ├── omptimized-binary-search.cpp ├── pivot-element-array.cpp ├── search-rotated-array.cpp └── square-root-number.cpp ├── binary-trees ├── .vscode │ └── settings.json └── tree-creation.cpp ├── bit-manipulation ├── addition-two-numbers.cpp ├── bit-set-check.cpp ├── count-set-bits.cpp ├── division-by-2k.cpp ├── equal-handle.cpp ├── kth-bit-set.cpp ├── multiply-by-2k.cpp ├── odd-even.cpp ├── power-of-two.cpp ├── swap-bit-manipulation.cpp └── toggle-kth-bit.cpp ├── character -array-strings ├── check-palidrome.cpp ├── gcd-strings.cpp ├── get-length.cpp ├── maximum-occuring-character.cpp ├── null-character.cpp ├── remove-occurances.cpp ├── replace-spaces.cpp ├── reverse-string.cpp └── to-lower-case.cpp ├── conditionals-loops ├── chararcter-check.cpp ├── cin-get-function.cpp ├── even-sum.cpp ├── farehnite-celcius.cpp ├── maximum.cpp ├── nested-loop.cpp └── prime-num.cpp ├── dynamic-programming └── hello.cpp ├── linked-list ├── .vscode │ └── settings.json ├── cycle-detect.cpp ├── delete-node.cpp ├── detect-loop.cpp ├── doubly-insertion.cpp ├── doubly-linked-list.cpp ├── linked-list-deletion.cpp ├── linked-list-insert.cpp ├── random-node.cpp ├── remove-elements.cpp └── sort-list.cpp ├── math-concepts ├── .DS_Store ├── .vscode │ └── settings.json └── seive-erotosthones.cpp ├── operators-for-loop ├── .vscode │ └── settings.json ├── bitwise-operators.cpp ├── continue.cpp ├── fibonacci-for.cpp ├── forloop-property.cpp ├── leetcode-problem1.cpp ├── leetcode-problem2.cpp ├── leftshift-rightshift.cpp ├── multiple-choice.cpp ├── pre-post-increment.cpp └── prime-for.cpp ├── patterns ├── alpha-method-2.cpp ├── alpha-triangle-count.cpp ├── alphabet-col-matrix.cpp ├── alphabet-count-matrix.cpp ├── alphabet-count-pattern.cpp ├── alphabet-matrix-pattern.cpp ├── alphabet-sq-matrix.cpp ├── alphabet-triangle.cpp ├── basic-pattern-j.cpp ├── basic-pattern.cpp ├── count-pattern.cpp ├── dabbang-pattern.cpp ├── inverted-star-pattern.cpp ├── num-equilateeral-triangle.cpp ├── optimized-right-negative.cpp ├── reverse-pattern.cpp ├── right-row-negative.cpp ├── right-row-number.cpp ├── right-row-number2.cpp ├── right-triangle.cpp └── star-spaces.cpp ├── pointers ├── default-argument.cpp ├── dynamic-array-2d.cpp ├── dynamic-array.cpp ├── inline-function.cpp ├── intro-pointer.cpp ├── macro.cpp ├── pass-value-refrence.cpp ├── pointer-array.cpp ├── pointer-chararray.cpp ├── refrence-functiion.cpp └── refrence-variable.cpp ├── recursion ├── binary-search.cpp ├── bubble-sort-recursion.cpp ├── check-palindrome.cpp ├── counting.cpp ├── factorial.cpp ├── linear-search.cpp ├── merge-sort.cpp ├── n-stairs.cpp ├── palindrome.cpp ├── power-of-2.cpp ├── power-recursion.cpp ├── print-till-n.cpp ├── quick-sort.cpp ├── say-digit.cpp ├── sorted-recursion.cpp ├── src-destination.cpp └── sum.cpp ├── sorting ├── bucket-sort.cpp ├── insertion-sort.cpp ├── optimised-bubble-sort.cpp ├── selection-sort.cpp └── task_schduling.cpp ├── switch-functions ├── amount-notes.cpp ├── calculator.cpp ├── combination.cpp ├── even-odd-funtion.cpp ├── nested-switch.cpp ├── pass-by-value.cpp ├── pow-function.cpp ├── prime-function.cpp ├── switch-case.cpp └── void-counting.cpp └── tricks ├── .vscode └── tasks.json └── sum-n-natural.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nishant-Tiwari24/data-structures-algorithms/27e5734fc42550105a2464c75dbcad30d7cd01da/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 📅 Six-Month DSA Study Plan 2 | 3 | ## Overview 4 | This structured six-month plan gradually introduces core DSA concepts and patterns, with 8-10 carefully selected practice questions each week. By the end of this plan, you’ll have covered foundational topics and advanced concepts needed for top-tier technical interviews. 5 | 6 | --- 7 | 8 | # Special Resources 9 | 1. [20 Patterns to Crack MAANG](https://drive.google.com/drive/folders/1HWKovNBImHmyoNlEFEzl71KOiVa3QezO) 10 | 2. [14 Tricks to Identify Patterns](https://drive.google.com/drive/folders/1Da_v5uHIvBscWcRRgMsYGq-hJ00dQL9Y) 11 | 12 | ## Month 1: Arrays and Strings 13 | 14 | ### **Week 1: Arrays Basics & Simple Patterns** 15 | - **Topics**: Two-pointer, Sliding Window, HashMap basics 16 | - **Patterns**: Prefix Sum, Running Sum 17 | - **Practice**: 18 | 1. **[Easy]** [Two Sum](https://leetcode.com/problems/two-sum/) 19 | 2. **[Easy]** [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) 20 | 3. **[Easy]** [Maximum Subarray (Kadane’s Algorithm)](https://leetcode.com/problems/maximum-subarray/) 21 | 4. **[Medium]** [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) 22 | 5. **[Medium]** [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) 23 | 6. **[Medium]** [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) 24 | 7. **[Medium]** [Longest Subarray with Sum K](https://practice.geeksforgeeks.org/problems/longest-sub-array-with-sum-k0809/1/) 25 | 8. **[Medium]** [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) 26 | 27 | ### **Week 2: Advanced Array Manipulations** 28 | - **Topics**: Sorting, Interval Overlap, Cyclic Sort 29 | - **Patterns**: Sorting + Two-pointer, Merge Intervals 30 | - **Practice**: 31 | 1. **[Easy]** [Sort Colors (Dutch National Flag Problem)](https://leetcode.com/problems/sort-colors/) 32 | 2. **[Medium]** [Merge Intervals](https://leetcode.com/problems/merge-intervals/) 33 | 3. **[Medium]** [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) 34 | 4. **[Medium]** [Next Permutation](https://leetcode.com/problems/next-permutation/) 35 | 5. **[Medium]** [Rotate Image](https://leetcode.com/problems/rotate-image/) 36 | 6. **[Medium]** [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) 37 | 7. **[Medium]** [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) 38 | 8. **[Medium]** [First Missing Positive](https://leetcode.com/problems/first-missing-positive/) 39 | 40 | ### **Week 3: Strings - Basics and Manipulations** 41 | - **Topics**: HashMaps for Strings, Substrings 42 | - **Patterns**: Sliding Window, Hashing 43 | - **Practice**: 44 | 1. **[Easy]** [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) 45 | 2. **[Easy]** [Valid Anagram](https://leetcode.com/problems/valid-anagram/) 46 | 3. **[Medium]** [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) 47 | 4. **[Medium]** [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) 48 | 5. **[Medium]** [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) 49 | 6. **[Medium]** [Group Anagrams](https://leetcode.com/problems/group-anagrams/) 50 | 7. **[Medium]** [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) 51 | 8. **[Medium]** [Decode Ways](https://leetcode.com/problems/decode-ways/) 52 | 53 | ### **Week 4: Advanced String Manipulations** 54 | - **Topics**: Substrings and Subsequence 55 | - **Patterns**: Two-pointer, Dynamic Programming (DP) for Strings 56 | - **Practice**: 57 | 1. **[Easy]** [Implement strStr()](https://leetcode.com/problems/implement-strstr/) 58 | 2. **[Medium]** [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) 59 | 3. **[Medium]** [Edit Distance](https://leetcode.com/problems/edit-distance/) 60 | 4. **[Medium]** [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) 61 | 5. **[Medium]** [Partition Labels](https://leetcode.com/problems/partition-labels/) 62 | 6. **[Hard]** [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) 63 | 7. **[Hard]** [Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) 64 | 8. **[Hard]** [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) 65 | 66 | --- 67 | 68 | ## Month 2: Linked Lists and Binary Trees 69 | 70 | ### **Week 1: Linked Lists Basics** 71 | - **Topics**: Singly and Doubly Linked Lists, Basic Operations 72 | - **Patterns**: Two-pointer, Fast & Slow Pointers 73 | - **Practice**: 74 | 1. **[Easy]** [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) 75 | 2. **[Easy]** [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) 76 | 3. **[Medium]** [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) 77 | 4. **[Medium]** [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) 78 | 5. **[Medium]** [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) 79 | 6. **[Medium]** [Remove N-th Node from End](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) 80 | 7. **[Medium]** [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/) 81 | 8. **[Hard]** [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) 82 | 83 | ### **Week 2: Advanced Linked Lists** 84 | - **Topics**: Complex List Manipulations, Reversal in Groups 85 | - **Patterns**: Fast & Slow Pointers, Recursive Linked List 86 | - **Practice**: 87 | 1. **[Medium]** [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) 88 | 2. **[Medium]** [Rotate List](https://leetcode.com/problems/rotate-list/) 89 | 3. **[Medium]** [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) 90 | 4. **[Medium]** [Partition List](https://leetcode.com/problems/partition-list/) 91 | 5. **[Medium]** [LRU Cache](https://leetcode.com/problems/lru-cache/) 92 | 6. **[Hard]** [LRU Cache](https://leetcode.com/problems/lru-cache/) 93 | 7. **[Hard]** [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) 94 | 8. **[Hard]** [Insert into a Cyclic Sorted List](https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/) 95 | 96 | ### **Week 3: Binary Trees Basics** 97 | - **Topics**: Binary Tree Traversals (Inorder, Preorder, Postorder) 98 | - **Patterns**: DFS and BFS Traversals 99 | - **Practice**: 100 | 1. **[Easy]** [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) 101 | 2. **[Easy]** [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) 102 | 3. **[Easy]** [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) 103 | 4. **[Medium]** [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) 104 | 5. **[Medium]** [Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) 105 | 6. **[Medium]** [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) 106 | 7. **[Medium]** [Binary Tree Left Side View](https://leetcode.com/problems/binary-tree-left-side-view/) 107 | 8. **[Medium]** [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) 108 | 109 | ### **Week 4: Advanced Trees** 110 | - **Topics**: Tree Properties, Recursive & Iterative Solutions 111 | - **Patterns**: Depth-First Search, Breadth-First Search 112 | - **Practice**: 113 | 1. **[Easy]** [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) 114 | 2. **[Medium]** [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) 115 | 3. **[Medium]** [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) 116 | 4. **[Medium]** [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) 117 | 5. **[Medium]** [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) 118 | 6. **[Hard]** [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/) 119 | 7. **[Hard]** [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) 120 | 8. **[Hard]** [Binary Tree Cameras](https://leetcode.com/problems/binary-tree-cameras/) 121 | 122 | ## Month 3: Graphs and Recursion 123 | 124 | ### **Week 1: Graphs Basics** 125 | - **Topics**: Graph Representations, BFS, DFS 126 | - **Patterns**: Breadth-First Search, Depth-First Search 127 | - **Practice**: 128 | 1. **[Easy]** [Flood Fill](https://leetcode.com/problems/flood-fill/) 129 | 2. **[Medium]** [Number of Islands](https://leetcode.com/problems/number-of-islands/) 130 | 3. **[Medium]** [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) 131 | 4. **[Medium]** [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) 132 | 5. **[Medium]** [Word Ladder](https://leetcode.com/problems/word-ladder/) 133 | 6. **[Medium]** [Clone Graph](https://leetcode.com/problems/clone-graph/) 134 | 7. **[Medium]** [Course Schedule](https://leetcode.com/problems/course-schedule/) 135 | 8. **[Medium]** [Detect Cycle in Undirected Graph](https://leetcode.com/problems/course-schedule/) 136 | 9. **[Hard]** [Minimum Number of Swaps for Sorting](https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/) 137 | 138 | ### **Week 2: Advanced Graph Algorithms** 139 | - **Topics**: Topological Sort, Connected Components 140 | - **Patterns**: Topological Sort, Union-Find 141 | - **Practice**: 142 | 1. **[Medium]** [Alien Dictionary](https://leetcode.com/problems/alien-dictionary/) 143 | 2. **[Medium]** [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) 144 | 3. **[Medium]** [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) 145 | 4. **[Medium]** [Connected Components in Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) 146 | 5. **[Medium]** [Accounts Merge](https://leetcode.com/problems/accounts-merge/) 147 | 6. **[Hard]** [Find Critical and Pseudo-Critical Edges](https://leetcode.com/problems/find-critical-and-pseudo-critical-edges/) 148 | 7. **[Hard]** [Redundant Connection](https://leetcode.com/problems/redundant-connection/) 149 | 8. **[Hard]** [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/) 150 | 151 | ### **Week 3: Recursion and Backtracking** 152 | - **Topics**: Basics of Recursion, Permutations, Combinations 153 | - **Patterns**: Backtracking, DFS 154 | - **Practice**: 155 | 1. **[Easy]** [Subsets](https://leetcode.com/problems/subsets/) 156 | 2. **[Medium]** [Combination Sum](https://leetcode.com/problems/combination-sum/) 157 | 3. **[Medium]** [Permutations](https://leetcode.com/problems/permutations/) 158 | 4. **[Medium]** [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) 159 | 5. **[Hard]** [N-Queens](https://leetcode.com/problems/n-queens/) 160 | 6. **[Hard]** [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) 161 | 7. **[Hard]** [Word Search II](https://leetcode.com/problems/word-search-ii/) 162 | 8. **[Hard]** [K-th Permutation Sequence](https://leetcode.com/problems/permutation-sequence/) 163 | 164 | ### **Week 4: Advanced Backtracking Problems** 165 | - **Topics**: Constraint Satisfaction, Optimizations 166 | - **Patterns**: Recursion + Memoization 167 | - **Practice**: 168 | 1. **[Medium]** [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) 169 | 2. **[Medium]** [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) 170 | 3. **[Medium]** [Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/) 171 | 4. **[Hard]** [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) 172 | 5. **[Hard]** [Expression Add Operators](https://leetcode.com/problems/expression-add-operators/) 173 | 6. **[Hard]** [Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) 174 | 7. **[Hard]** [Count of Unique BSTs](https://leetcode.com/problems/unique-binary-search-trees/) 175 | 8. **[Hard]** [Word Search](https://leetcode.com/problems/word-search/) 176 | 177 | --- 178 | 179 | ## Month 4: Dynamic Programming (DP) - Part 1 180 | 181 | ### **Week 1: Introduction to Dynamic Programming** 182 | - **Topics**: Fundamentals of DP, 1D DP 183 | - **Patterns**: Memoization, Tabulation 184 | - **Practice**: 185 | 1. **[Easy]** [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) 186 | 2. **[Medium]** [House Robber](https://leetcode.com/problems/house-robber/) 187 | 3. **[Medium]** [House Robber II](https://leetcode.com/problems/house-robber-ii/) 188 | 4. **[Medium]** [Decode Ways](https://leetcode.com/problems/decode-ways/) 189 | 5. **[Medium]** [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) 190 | 6. **[Medium]** [Jump Game](https://leetcode.com/problems/jump-game/) 191 | 7. **[Medium]** [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) 192 | 8. **[Hard]** [Longest Substring with K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) 193 | 194 | ### **Week 2: Intermediate DP** 195 | - **Topics**: 0/1 Knapsack and Variants 196 | - **Patterns**: Knapsack, Subset Sum 197 | - **Practice**: 198 | 1. **[Medium]** [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) 199 | 2. **[Medium]** [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) 200 | 3. **[Medium]** [Coin Change](https://leetcode.com/problems/coin-change/) 201 | 4. **[Medium]** [Target Sum](https://leetcode.com/problems/target-sum/) 202 | 5. **[Medium]** [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) 203 | 6. **[Medium]** [Minimum Subset Sum Difference](https://leetcode.com/problems/minimum-difference-subsets/) 204 | 7. **[Hard]** [Word Break](https://leetcode.com/problems/word-break/) 205 | 206 | ### **Week 3: Advanced DP - Sequence Alignment** 207 | - **Topics**: Longest Common Subsequence and Variants 208 | - **Patterns**: Substring and Subsequence Problems 209 | - **Practice**: 210 | 1. **[Medium]** [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) 211 | 2. **[Medium]** [Longest Common Substring](https://leetcode.com/problems/longest-common-substring/) 212 | 3. **[Medium]** [Minimum Insertions/Deletions](https://leetcode.com/problems/minimum-insertions-to-make-a-string-palindrome/) 213 | 4. **[Medium]** [Edit Distance](https://leetcode.com/problems/edit-distance/) 214 | 5. **[Medium]** [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) 215 | 6. **[Medium]** [Sequence Pattern Matching](https://leetcode.com/problems/sequence-pattern-matching/) 216 | 7. **[Hard]** [Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/) 217 | 8. **[Hard]** [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) 218 | 219 | ### **Week 4: Advanced DP - Matrix and Grid Problems** 220 | - **Topics**: 2D DP, Grid Path Problems 221 | - **Patterns**: Dynamic Programming on Grids 222 | - **Practice**: 223 | 1. **[Easy]** [Unique Paths](https://leetcode.com/problems/unique-paths/) 224 | 2. **[Medium]** [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) 225 | 3. **[Medium]** [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) 226 | 4. **[Medium]** [Dungeon Game](https://leetcode.com/problems/dungeon-game/) 227 | 5. **[Medium]** [Cherry Pickup](https://leetcode.com/problems/cherry-pickup/) 228 | 6. **[Hard]** [Interleaving String](https://leetcode.com/problems/interleaving-string/) 229 | 7. **[Hard]** [Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/) 230 | 8. **[Hard]** [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) 231 | 232 | 233 | ## Month 5: Greedy Algorithms, Advanced Dynamic Programming, and Heaps 234 | 235 | ### **Week 1: Greedy Algorithms - Intermediate Problems** 236 | - **Topics**: Interval Scheduling, Partitioning, Activity Selection 237 | - **Patterns**: Interval Problems, Greedy Choice Property 238 | - **Practice**: 239 | 1. **[Easy]** [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) 240 | 2. **[Medium]** [Jump Game](https://leetcode.com/problems/jump-game/) 241 | 3. **[Medium]** [Jump Game II](https://leetcode.com/problems/jump-game-ii/) 242 | 4. **[Medium]** [Gas Station](https://leetcode.com/problems/gas-station/) 243 | 5. **[Medium]** [N Meetings in One Room](https://practice.geeksforgeeks.org/problems/n-meetings-in-one-room/0/) 244 | 6. **[Medium]** [Partition Labels](https://leetcode.com/problems/partition-labels/) 245 | 7. **[Medium]** [Task Scheduler](https://leetcode.com/problems/task-scheduler/) 246 | 8. **[Hard]** [Minimum Number of Refueling Stops](https://leetcode.com/problems/minimum-number-of-refueling-stops/) 247 | 248 | ### **Week 2: Heaps - Basics and Applications** 249 | - **Topics**: Heap Basics, Priority Queue, Top-K Problems 250 | - **Patterns**: Min-Heap, Max-Heap 251 | - **Practice**: 252 | 1. **[Easy]** [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) 253 | 2. **[Medium]** [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) 254 | 3. **[Medium]** [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) 255 | 4. **[Medium]** [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) 256 | 5. **[Medium]** [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) 257 | 6. **[Hard]** [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) 258 | 7. **[Hard]** [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) 259 | 8. **[Hard]** [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/) 260 | 261 | ### **Week 3: Advanced Dynamic Programming - Optimization Problems** 262 | - **Topics**: DP on Trees, Knapsack Variants 263 | - **Patterns**: 0/1 Knapsack, DP with Trees 264 | - **Practice**: 265 | 1. **[Medium]** [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) 266 | 2. **[Medium]** [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) 267 | 3. **[Medium]** [Coin Change](https://leetcode.com/problems/coin-change/) 268 | 4. **[Medium]** [Target Sum](https://leetcode.com/problems/target-sum/) 269 | 5. **[Medium]** [Tree Diameter](https://leetcode.com/problems/diameter-of-binary-tree/) 270 | 6. **[Hard]** [Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) 271 | 7. **[Hard]** [Maximal Square](https://leetcode.com/problems/maximal-square/) 272 | 8. **[Hard]** [Burst Balloons](https://leetcode.com/problems/burst-balloons/) 273 | 274 | ### **Week 4: Advanced Greedy Algorithms & Special Topics** 275 | - **Topics**: Advanced Greedy Techniques, Miscellaneous 276 | - **Patterns**: Greedy + Dynamic Programming, Hybrid Patterns 277 | - **Practice**: 278 | 1. **[Medium]** [Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) 279 | 2. **[Medium]** [Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/) 280 | 3. **[Medium]** [Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/) 281 | 4. **[Hard]** [Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/) 282 | 5. **[Hard]** [IPO](https://leetcode.com/problems/ipo/) 283 | 6. **[Hard]** [Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling/) 284 | 7. **[Hard]** [Candy](https://leetcode.com/problems/candy/) 285 | 286 | --- 287 | 288 | ## Month 6: Tries, Advanced Data Structures, and Comprehensive Revision 289 | 290 | ### **Week 1: Tries and Prefix Trees** 291 | - **Topics**: Trie Basics, String Manipulations with Trie 292 | - **Patterns**: Trie Structure, Prefix Matching 293 | - **Practice**: 294 | 1. **[Easy]** [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) 295 | 2. **[Medium]** [Add and Search Word - Data Structure Design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) 296 | 3. **[Medium]** [Word Search II](https://leetcode.com/problems/word-search-ii/) 297 | 4. **[Medium]** [Replace Words](https://leetcode.com/problems/replace-words/) 298 | 5. **[Medium]** [Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/) 299 | 6. **[Hard]** [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) 300 | 7. **[Hard]** [Concatenated Words](https://leetcode.com/problems/concatenated-words/) 301 | 8. **[Hard]** [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) 302 | 303 | ### **Week 2: Segment Trees and Binary Indexed Trees** 304 | - **Topics**: Range Queries, Lazy Propagation 305 | - **Patterns**: Segment Tree, Fenwick Tree 306 | - **Practice**: 307 | 1. **[Medium]** [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) 308 | 2. **[Medium]** [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) 309 | 3. **[Medium]** [Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) 310 | 4. **[Medium]** [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) 311 | 5. **[Hard]** [Reverse Pairs](https://leetcode.com/problems/reverse-pairs/) 312 | 6. **[Hard]** [The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/) 313 | 7. **[Hard]** [Maximum Sum of Rectangle No Larger Than K](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/) 314 | 315 | ### **Week 3: Final Revision - Arrays, Strings, Linked Lists** 316 | - **Topics**: Comprehensive Review and Practice 317 | - **Practice**: 318 | 1. **Review** top 3-5 questions from Arrays and Strings sections 319 | 2. **Review** top 3-5 questions from Linked Lists 320 | 3. **Practice Mock Interviews** with a random mix of problems 321 | 4. Focus on any weak areas or topics that need additional review 322 | 323 | ### **Week 4: Final Revision - Trees, Graphs, DP, and Advanced Topics** 324 | - **Topics**: Comprehensive Review and Practice 325 | - **Practice**: 326 | 1. **Review** top 3-5 questions from Trees and Graphs 327 | 2. **Review** top 3-5 questions from Dynamic Programming 328 | 3. **Practice Mock Interviews** with a random mix of problems 329 | 4. Focus on polishing solutions for harder problems or any weak areas 330 | 331 | --- 332 | 333 | -------------------------------------------------------------------------------- /array-questions/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nishant-Tiwari24/data-structures-algorithms/27e5734fc42550105a2464c75dbcad30d7cd01da/array-questions/.DS_Store -------------------------------------------------------------------------------- /array-questions/add-arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | vector reverse(vector arr) { 3 | int s = 0; 4 | int e = arr.size() - 1; 5 | while(s<=e) { 6 | swap(arr[s++],arr[e--]); 7 | } 8 | return arr; 9 | } 10 | 11 | vector findArraySum(vector&a, int n, vector&b, int m) { 12 | int i=n-1; 13 | int j=m-1; 14 | vector ans; 15 | int carry=0; 16 | 17 | while(i>=0 && j>=0) { 18 | int val1 = a[i]; 19 | int val2 = b[j]; 20 | int sum = val1 + val2 + carry; 21 | carry = sum/10; 22 | sum = sum % 10; 23 | ans.push_back(sum); 24 | i--; 25 | j--; 26 | } 27 | 28 | while(i>=0) { 29 | int sum = a[i] + carry; 30 | carry = sum/10; 31 | sum = sum % 10; 32 | ans.push_back(sum); 33 | i--; 34 | 35 | } 36 | 37 | while(j>=0) { 38 | int sum = b[j] + carry; 39 | carry = sum/10; 40 | sum = sum % 10; 41 | ans.push_back(sum); 42 | j--; 43 | 44 | } 45 | 46 | while(carry) { 47 | int sum = carry; 48 | carry = sum/10; 49 | sum = sum %10; 50 | ans.push_back(sum); 51 | } 52 | 53 | return reverse(ans); 54 | 55 | 56 | } -------------------------------------------------------------------------------- /array-questions/array-reverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int reverse(int arr[], int n) { 6 | int s = 0; 7 | int e = n-1; 8 | while(s<=e) { 9 | swap(arr[s],arr[e]); 10 | s++; 11 | e--; 12 | } 13 | } 14 | 15 | int main() { 16 | int arr[5] = {2,3,4,5,6}; 17 | reverse(arr,5); 18 | 19 | } -------------------------------------------------------------------------------- /array-questions/check-rotated-sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector& nums) { 4 | int count = 0; 5 | for(int i=1; i nums[i]) { 7 | count++; 8 | } 9 | } 10 | if(nums[nums.size() - 1] > nums[0]) { 11 | count++; 12 | } 13 | 14 | if(count<=1) 15 | return true; 16 | 17 | else { 18 | return false; 19 | } 20 | } 21 | }; -------------------------------------------------------------------------------- /array-questions/merge-sorted-same-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Solution { 5 | public: 6 | void merge(vector& nums1, int m, vector& nums2, int n) { 7 | int i = m-1; 8 | int j = n-1; 9 | int k = m+n-1; 10 | 11 | while(i>=0 && j>=0) { 12 | if(nums1[i] >= nums2[j]) { 13 | nums1[k--] = nums1[i--]; 14 | } 15 | else { 16 | nums1[k--] = nums2[j--]; 17 | } 18 | } 19 | 20 | while(j >= 0) { 21 | nums1[k--] = nums2[j--]; 22 | } 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /array-questions/move-zeros-end.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | int i = 0; 5 | for(int j = 0; j &arr , int m){ 2 | int s = m+1; 3 | int e = arr.size() - 1; 4 | while(s<=e) { 5 | swap(arr[s],arr[e]); 6 | s++; 7 | e--; 8 | } 9 | } -------------------------------------------------------------------------------- /array-questions/rotate-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | vector temp(nums.size()); 5 | for(int i=0 ; i 2 | using namespace std; 3 | 4 | int main() { 5 | int fifth[10]; 6 | int size = sizeof(fifth)/sizeof(int); 7 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int arr[5] = {[0 ... 4] = 5}; 6 | 7 | } -------------------------------------------------------------------------------- /arrays-reverse-linear search/linearsearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool search(int a[], int size, int key) { 5 | for (int i = 0; i < size - 1; ++i) { 6 | if (a[i] == key) 7 | return true; 8 | } 9 | return false; 10 | } 11 | 12 | int main() { 13 | int size; 14 | cout << "Enter the number of elements in array: "; 15 | cin>>size; 16 | int a[size], key; 17 | 18 | cout<<"Enter elements of array: \n"; 19 | for (int i=0; i> a[i]; 21 | } 22 | cout << "\nEnter element to be searched: "; 23 | cin >> key; 24 | 25 | (search(a, size, key)); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /arrays-reverse-linear search/max-min.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maximum(int arr[],int size) { 5 | int max = INT_MIN; 6 | for (int i = 0; i < size; i++) 7 | { 8 | if(arr[i] > max) { 9 | max = arr[i]; 10 | } 11 | } 12 | return max; 13 | } 14 | 15 | int minimum(int arr[], int size) { 16 | int min = INT_MAX; 17 | for (int i=0; i>size; 29 | 30 | int arr[size]; 31 | for(int i=0; i>arr[i]; 33 | } 34 | maximum(arr,size); 35 | minimum(arr,size); 36 | } -------------------------------------------------------------------------------- /arrays-reverse-linear search/reverse-arry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int reverse(int a[],int n) { 5 | int start = 0, end = n-1; 6 | while (start < end) { 7 | swap(start, end); 8 | start++; 9 | end--; 10 | } 11 | } 12 | 13 | int main() { 14 | int n; 15 | cin >> n; 16 | int arr[n]; 17 | 18 | for(int i=0; i>arr[i]; 20 | } 21 | reverse(arr,n); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /arrays-reverse-linear search/swap-alternate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int swapAlternate(int arr[], int size) { 6 | for(int i = 0; i< size; i+=2) { 7 | if(i+1 > size) { 8 | swap(a[i], a[i+1]); 9 | } 10 | } 11 | return arr; 12 | } 13 | -------------------------------------------------------------------------------- /arrays/goal-parser.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string interpret(string command) { 4 | string res; 5 | for(int i = 0; i < command.length(); i++) { 6 | if(command[i] == 'G') { 7 | res.push_back('G'); 8 | } else if(command[i] == '(') { 9 | if (command[i + 1] == ')') { 10 | res.push_back('o'); 11 | i++; 12 | } else if (command[i + 1] == 'a' && command[i + 2] == 'l' && command[i + 3] == ')') { 13 | res.append("al"); 14 | i += 3; 15 | } 16 | } 17 | } 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /arrays/input-output.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nishant-Tiwari24/data-structures-algorithms/27e5734fc42550105a2464c75dbcad30d7cd01da/arrays/input-output.cpp -------------------------------------------------------------------------------- /arrays/rotate-image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | for(int i=0; i>& matrix, int target) { 4 | int s = 0; 5 | int col = matrix[0].size(); 6 | int e = (matrix.size() * matrix[0].size()) - 1; 7 | int m = s + (e-s)/2; 8 | 9 | while(s<=e) { 10 | int ele = matrix[m/col][m%col]; 11 | if(ele == target) 12 | return 1; 13 | else if(target < ele) 14 | e = m - 1; 15 | else 16 | s = m + 1; 17 | m = s + (e-s)/2; 18 | } 19 | return 0; 20 | } 21 | }; -------------------------------------------------------------------------------- /arrays/spiral-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector spiralOrder(vector>& matrix) { 4 | int sr = 0, sc = 0; 5 | int er = matrix.size() - 1; 6 | int ec = matrix[0].size() - 1; 7 | 8 | int count = 0; 9 | vector ans; 10 | 11 | while (count < (matrix.size() * matrix[0].size())) { 12 | for (int i = sc; i <= ec && count < (matrix.size() * matrix[0].size()); i++) { 13 | ans.push_back(matrix[sr][i]); 14 | count++; 15 | } 16 | sr++; 17 | 18 | for (int i = sr; i <= er && count < (matrix.size() * matrix[0].size()); i++) { 19 | ans.push_back(matrix[i][ec]); 20 | count++; 21 | } 22 | ec--; 23 | 24 | for (int i = ec; i >= sc && count < (matrix.size() * matrix[0].size()); i--) { 25 | ans.push_back(matrix[er][i]); 26 | count++; 27 | } 28 | er--; 29 | 30 | for (int i = er; i >= sr && count < (matrix.size() * matrix[0].size()); i--) { 31 | ans.push_back(matrix[i][sc]); 32 | count++; 33 | } 34 | sc++; 35 | } 36 | 37 | return ans; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /arrays/wave-print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector wavePrint(std::vector> arr, int nRows, int mCols) { 4 | std::vector ans; 5 | for (int i = 0; i < mCols; i++) { 6 | if (i & 1) { 7 | // If i is odd, go from bottom to top 8 | for (int j = nRows - 1; j >= 0; j--) { 9 | ans.push_back(arr[j][i]); 10 | } 11 | } else { 12 | // If i is even, go from top to bottom 13 | for (int j = 0; j < nRows; j++) { 14 | ans.push_back(arr[j][i]); 15 | } 16 | } 17 | } 18 | return ans; 19 | } 20 | -------------------------------------------------------------------------------- /basic-concepts/datatype-variables.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 123; 6 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | cout<<"Hello World"< 2 | using namespace std; 3 | 4 | int main() { 5 | int b = 2/5; 6 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 'd'; 6 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin>>n; 8 | int i = 0; 9 | int ans = 0; 10 | int bit; 11 | 12 | while (n!=0) 13 | { 14 | bit = n % 10; 15 | if(bit == 1) { 16 | ans = ans + pow(2,i); 17 | } 18 | n = n / 10; 19 | i++; 20 | } 21 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin>>n; 8 | int dig; 9 | int ans = 0, i = 0; 10 | 11 | while(n!=0) { 12 | dig = n & 1; 13 | ans = (pow(10,i) * dig) + ans; 14 | n = n >> 1; 15 | i++; 16 | } 17 | cout< 2 | class Solution { 3 | public: 4 | bool isPowerOfTwo(int n) { 5 | for(int i=0; i<31; i++) { 6 | if(n == pow(2,i)); 7 | return true; 8 | } 9 | } 10 | }; -------------------------------------------------------------------------------- /binary-decimal-concept/leetcode-problem-4.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | int mask = 0; 5 | int n = num; 6 | if(num == 0) { 7 | return 1; 8 | } 9 | 10 | while(n != 0) { 11 | mask = (mask << 1) | 1; 12 | n = n >> 1; 13 | } 14 | 15 | int ans = (~num) & mask; 16 | return ans; 17 | } 18 | 19 | }; -------------------------------------------------------------------------------- /binary-decimal-concept/leetcode-problem-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | int ans = 0; 5 | int i = 0; 6 | 7 | int bitwiseComplement(int n) { 8 | if(n == 0) { 9 | return 1; 10 | } 11 | 12 | while(n!=0) { 13 | 14 | int bit = n & 1; 15 | if(bit == 1) { 16 | bit = 0; 17 | } 18 | else if(bit == 0){ 19 | bit = 1; 20 | } 21 | 22 | if(bit == 1){ 23 | ans = pow(2,i) + ans; 24 | } 25 | n = n>>1; 26 | i++; 27 | } 28 | return ans; 29 | 30 | } 31 | }; -------------------------------------------------------------------------------- /binary-decimal-concept/leetcode-problem-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | long long int ans = 0; 5 | int i = 0; 6 | int reverse(int x) { 7 | while(x!=0) { 8 | int dig = x % 10; 9 | ans = (ans * 10) + dig; 10 | x = x / 10; 11 | } 12 | if(ans > pow(2,31) - 1 || ans < -pow(2,31)) { 13 | return 0; 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /binary-decimal-concept/neg-decimal-to-binary.cpp: -------------------------------------------------------------------------------- 1 | #include -------------------------------------------------------------------------------- /binary-search/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp.errorSquiggles": "disabled" 3 | } -------------------------------------------------------------------------------- /binary-search/binary-search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool search(int arr[], int n, int target) { 5 | int start = 0; 6 | int end = n - 1; 7 | 8 | int mid = (start + end)/2; 9 | 10 | while (start<=end) 11 | { 12 | if(mid == target) { 13 | return true; 14 | } 15 | else if(target < mid) { 16 | end = mid - 1; 17 | mid = (start + end)/2; 18 | } 19 | else { 20 | start = mid + 1; 21 | mid = (start + end)/2; 22 | } 23 | } 24 | return false; 25 | 26 | } 27 | 28 | int main() { 29 | int arr[10] = {2,3,4,5,6,7,8,9,10,12}; 30 | search(arr,10,4); 31 | } -------------------------------------------------------------------------------- /binary-search/book-allocation-imp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool PossibleCount(vector arr, int n, int m, int mid) { 4 | int student = 1; 5 | int pagesum = 0; 6 | 7 | for(int i =0; i m || arr[i] > mid) { 14 | return false; 15 | } 16 | pagesum = arr[i]; 17 | } 18 | } 19 | return true; 20 | } 21 | 22 | 23 | long long ayushGivesNinjatest(int n, int m, vector time) 24 | { 25 | int s = 0; 26 | int sum = 0; 27 | for(int i = 0; i < time.size(); i++) { 28 | sum +=time[i]; 29 | } 30 | int e = sum; 31 | int mid = s + (e-s)/2; 32 | int ans = -1; 33 | 34 | while(s<=e) { 35 | if(PossibleCount(time,n,m,mid)) { 36 | ans = mid; 37 | e = mid - 1; 38 | } 39 | else { 40 | s = mid + 1; 41 | } 42 | mid = s + (e-s)/2; 43 | } 44 | return ans; 45 | } -------------------------------------------------------------------------------- /binary-search/first-last-occurance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int first(vector& arr, int n, int k) { 5 | int s = 0; 6 | int e = n - 1; 7 | int ans = -1; 8 | 9 | int m = e + (s-e)/2; 10 | while (s <= e) { 11 | if (k == arr[m]) { 12 | ans = m; 13 | e = m - 1; 14 | } 15 | else if (k < arr[m]) { 16 | e = m - 1; 17 | } 18 | else { 19 | s = m + 1; 20 | } 21 | m = e + (s-e)/2; 22 | } 23 | return ans; 24 | 25 | } 26 | 27 | 28 | int last(vector& arr, int n, int k) { 29 | int s = 0; 30 | int e = n - 1; 31 | int ans = -1; 32 | 33 | int m = e + (s-e)/2; 34 | while (s <= e) { 35 | if (k == arr[m]) { 36 | ans = m; 37 | s = m + 1; 38 | } 39 | else if (k < arr[m]) { 40 | e = m - 1; 41 | } 42 | else { 43 | s = m + 1; 44 | } 45 | m = e + (s-e)/2; 46 | } 47 | return ans; 48 | } 49 | 50 | pair firstAndLastPosition(vector& arr, int n, int k) 51 | { 52 | // Write your code here 53 | pair p; 54 | p.first = first(arr, n, k); 55 | p.second = last(arr, n, k); 56 | return p; 57 | } -------------------------------------------------------------------------------- /binary-search/leetcode-852.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | int peakIndexInMountainArray(vector& arr) { 5 | int s = 0; 6 | int e = arr.size() - 1; 7 | int m = s + (e-s)/2; 8 | 9 | while(s 2 | using namespace std; 3 | 4 | bool search(int arr[], int n, int target) { 5 | int start = 0; 6 | int end = n - 1; 7 | 8 | int mid = start + (end - start)/2; 9 | 10 | while (start<=end) 11 | { 12 | if(mid == target) { 13 | return true; 14 | } 15 | else if(target < mid) { 16 | end = mid - 1; 17 | mid = start + (end - start)/2; 18 | } 19 | else { 20 | start = mid + 1; 21 | mid = start + (end - start)/2; 22 | } 23 | } 24 | return false; 25 | 26 | } 27 | 28 | int main() { 29 | int arr[10] = {2,3,4,5,6,7,8,9,10,12}; 30 | search(arr,10,4); 31 | } -------------------------------------------------------------------------------- /binary-search/pivot-element-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int pivot(int arr[],int n) { 5 | int s = 0; 6 | int e = s -1; 7 | int m = s + (e-s)/2; 8 | 9 | while(s<=e) { 10 | if(arr[m] >= arr[0]) { 11 | s = m + 1; 12 | } 13 | else { 14 | e = m; 15 | } 16 | m = s + (e - s)/2; 17 | } 18 | return s; 19 | } -------------------------------------------------------------------------------- /binary-search/search-rotated-array.cpp: -------------------------------------------------------------------------------- 1 | int getPivot(vector& arr) { 2 | int s = 0; 3 | int e = arr.size() - 1; 4 | int m = s + (e-s)/2; 5 | 6 | while(s<=e) { 7 | if(arr[m]>=arr[0]) { 8 | s = m + 1; 9 | } 10 | else { 11 | e = m; 12 | } 13 | m = s + (e-s)/2; 14 | } 15 | return s; 16 | } 17 | 18 | int bsearch(vector& arr,int s, int n, int k) { 19 | int start = s; 20 | int end = n - 1; 21 | 22 | int mid = (start + end)/2; 23 | 24 | while (start<=end) 25 | { 26 | if(mid == k) { 27 | return k; 28 | } 29 | else if(k < mid) { 30 | end = mid - 1; 31 | mid = (start + end)/2; 32 | } 33 | else { 34 | start = mid + 1; 35 | mid = (start + end)/2; 36 | } 37 | } 38 | return -1; 39 | 40 | } 41 | 42 | int search(vector& arr, int n, int k) 43 | { 44 | int pivot = getPivot(arr); 45 | if(k >= arr[pivot] && k <= arr[n-1] { 46 | return bsearch(arr,pivot, n-1, k); 47 | } 48 | else { 49 | return bsearch(arr, 0, pivot - 1 , k); 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /binary-search/square-root-number.cpp: -------------------------------------------------------------------------------- 1 | long long binarySearch(int n) { 2 | int s = 0; 3 | int e = n - 1; 4 | long long m = s + (e-s)/2; 5 | int ans = -1; 6 | 7 | while(s<=e) { 8 | if(m*m == n) { 9 | return m; 10 | } 11 | else if(m*m > n) { 12 | e = m - 1; 13 | } 14 | else { 15 | ans = m; 16 | s = m + 1; 17 | } 18 | m = s + (e - s)/2; 19 | } 20 | return ans; 21 | } 22 | 23 | int floorSqrt(int n) 24 | { 25 | return binarySearch(n); 26 | } 27 | -------------------------------------------------------------------------------- /binary-trees/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | }, 5 | "C_Cpp.errorSquiggles": "disabled" 6 | } -------------------------------------------------------------------------------- /binary-trees/tree-creation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left; 9 | node* right; 10 | 11 | node(int data) { 12 | this->data = data; 13 | this->left = NULL; 14 | this->right = NULL; 15 | } 16 | }; 17 | 18 | node* createTree(node* root) { 19 | cout<<"Enter the data"<>data; 22 | node* newnode = new node(data); 23 | if(data == -1) { 24 | return NULL; 25 | } 26 | 27 | cout<<"Enter the data for inserting in left node"<left = createTree(newnode->left); 29 | cout<<"Enter the data for inserting in right node"<right = createTree(newnode->right); 31 | return root; 32 | } 33 | 34 | node* levelOrder(node* root) { 35 | queue q; 36 | q.push(root); 37 | 38 | while(!q.empty) { 39 | node* frontnode = q.front(); 40 | 41 | q.pop(); 42 | if(temp == NULL) {#include 43 | #include 44 | using namespace std; 45 | 46 | class node { 47 | public: 48 | int data; 49 | node* left; 50 | node* right; 51 | 52 | node(int data) { 53 | this->data = data; 54 | this->left = NULL; 55 | this->right = NULL; 56 | } 57 | }; 58 | 59 | node* createTree(node* root) { 60 | cout << "Enter the data: "; 61 | int data; 62 | cin >> data; 63 | 64 | if (data == -1) { 65 | return NULL; 66 | } 67 | 68 | node* newnode = new node(data); 69 | 70 | cout << "Enter the data for inserting in the left of " << data << ": "; 71 | newnode->left = createTree(newnode->left); 72 | 73 | cout << "Enter the data for inserting in the right of " << data << ": "; 74 | newnode->right = createTree(newnode->right); 75 | 76 | return newnode; 77 | } 78 | 79 | void levelOrder(node* root) { 80 | if (root == NULL) { 81 | return; 82 | } 83 | 84 | queue q; 85 | q.push(root); 86 | 87 | while (!q.empty()) { 88 | node* frontnode = q.front(); 89 | q.pop(); 90 | 91 | if (frontnode != NULL) { 92 | cout << "Data extracted: " << frontnode->data << endl; 93 | if (frontnode->left) { 94 | q.push(frontnode->left); 95 | } 96 | if (frontnode->right) { 97 | q.push(frontnode->right); 98 | } 99 | } 100 | } 101 | } 102 | 103 | void inorder(node* root) { 104 | if(root == NULL) { 105 | return; 106 | } 107 | inorder(root->left); 108 | cout<<"Extracted data is "<data<right); 110 | 111 | } 112 | 113 | void preorder(node* root) { 114 | if(root == NULL) { 115 | return; 116 | } 117 | cout<<"Extracted data is "<data<left); 119 | inorder(root->right); 120 | 121 | } 122 | 123 | void postorder(node* root) { 124 | if(root == NULL) { 125 | return; 126 | } 127 | inorder(root->left); 128 | inorder(root->right); 129 | cout<<"Extracted data is "<data<data<left) { 147 | q.push(frontnode->left); 148 | } 149 | if(frontnode->right) { 150 | q.push(frontnode->right); 151 | } 152 | } 153 | 154 | } 155 | } 156 | 157 | int main () { 158 | node* root = NULL; 159 | root = createTree(root); 160 | } -------------------------------------------------------------------------------- /bit-manipulation/addition-two-numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int set(int a,int b) { 5 | return (a^b) + 2(a&b); 6 | } -------------------------------------------------------------------------------- /bit-manipulation/bit-set-check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int set(int k) { 5 | int x; 6 | return x&(1< 2 | using namespace std; 3 | 4 | int count(int x) { 5 | return __builtin_popcount(x); 6 | } 7 | 8 | int count(long long x) { 9 | return __builtin_popcountll(x); 10 | } -------------------------------------------------------------------------------- /bit-manipulation/division-by-2k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int divide(int x,int k) { 5 | return x>>k; 6 | } -------------------------------------------------------------------------------- /bit-manipulation/equal-handle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int set(int x,int y) { 5 | x = x^y^x; 6 | } -------------------------------------------------------------------------------- /bit-manipulation/kth-bit-set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int set(int k) { 5 | int x; 6 | return x|(1< 2 | using namespace std; 3 | 4 | int multiply(int x,int k) { 5 | return x< 2 | using namespace std; 3 | 4 | int main() { 5 | int x; 6 | if(x&1) cout<<"odd number"; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /bit-manipulation/power-of-two.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int pow(int x) { 5 | return !x&(x-1); 6 | } -------------------------------------------------------------------------------- /bit-manipulation/swap-bit-manipulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int set(int x,int y) { 5 | x = x^y; 6 | y = y^x; 7 | x = x^y; 8 | } -------------------------------------------------------------------------------- /bit-manipulation/toggle-kth-bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int set(int k) { 5 | int x; 6 | return x^(1< 2 | using namespace std; 3 | 4 | char toLowercase(char ch) { 5 | if (ch >= 'A' && ch <= 'Z') 6 | return ch - ('A' - 'a'); 7 | else 8 | return ch; 9 | } 10 | 11 | int checkPalindrome(char ch[], int n) { 12 | int s = 0; 13 | int e = n - 1; 14 | while(s<=e) { 15 | if(toLowercase(ch[s]) != toLowercase(ch[e])) 16 | return 0; 17 | else { 18 | s++; 19 | e--; 20 | } 21 | } 22 | 23 | return 1; 24 | } -------------------------------------------------------------------------------- /character -array-strings/gcd-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string gcdOfStrings(string str1, string str2) { 4 | if(str1 + str2 != str2 + str1) { 5 | return ""; 6 | } 7 | else { 8 | return str1.substr(0,gcd(str1.size(),str2.size())); 9 | } 10 | } 11 | }; -------------------------------------------------------------------------------- /character -array-strings/get-length.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getLength(char name[]) { 5 | int i = 0, count = 0; 6 | while (name[i] != '\0') 7 | { 8 | count++; 9 | i++; 10 | } 11 | return count; 12 | } -------------------------------------------------------------------------------- /character -array-strings/maximum-occuring-character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getmaxocc(string s) { 5 | int arr[26] = {0}; 6 | 7 | for(int i=0; i arr[i]) { 17 | ans = i; 18 | maxi = arr[i]; 19 | } 20 | } 21 | return ans + 'a'; 22 | } -------------------------------------------------------------------------------- /character -array-strings/null-character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char name[20]; 6 | cout<<"Enter your name : "; 7 | cin>>name; 8 | name[2] = '\0'; 9 | cout< 2 | string replaceSpaces(string &str){ 3 | string temp; 4 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | int reverse(char arr[], int n) { 6 | int s = 0, e = n-1; 7 | while (s <= e) { 8 | swap(arr[s++],arr[e--]); 9 | } 10 | } -------------------------------------------------------------------------------- /character -array-strings/to-lower-case.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char toLowercase(char ch) { 5 | if (ch >= 'A' && ch <= 'Z') 6 | return ch - ('A' - 'a'); 7 | else 8 | return ch; 9 | } -------------------------------------------------------------------------------- /conditionals-loops/chararcter-check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int ch; 6 | cout<<"Enter the character: "; 7 | cin>>ch; 8 | 9 | if(ch>='a' && ch<='z') { 10 | cout<<"Char is in lowercase"<='A' && ch<='Z') { 14 | cout<<"Char is in uppercase"<='0' && ch<='9') { 18 | cout<<"Char is a number"< 2 | using namespace std; 3 | 4 | int main() { 5 | int a; 6 | a = cin.get(); //provides ascii value of given int 7 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 50; 6 | int sum = 0; 7 | int i = 0; 8 | 9 | while(i 2 | using namespace std; 3 | 4 | int main() { 5 | int cel = 100; 6 | int f,i = 0; 7 | 8 | while(i 2 | using namespace std; 3 | 4 | int main() { 5 | int a,b; 6 | cin>>a>>b; 7 | 8 | if(a>b) { 9 | cout<<"A is greater"< 2 | using namespace std; 3 | 4 | int main() { 5 | int a; 6 | cout<<"Enter the value of a : "< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter the number: "; 7 | cin>>n; 8 | int i = 2; 9 | int flag = 0; 10 | 11 | while(i < n) { 12 | if(n%i == 0) { 13 | flag = flag + 1; 14 | } 15 | else { 16 | flag = 0; 17 | } 18 | i = i + 1; 19 | } 20 | 21 | if(flag >= 1) { 22 | cout<<"Not a prime number"<next) return false; 13 | ListNode* slow = head; 14 | ListNode* fast = head; 15 | 16 | while(fast != NULL && fast->next != NULL) { 17 | slow = slow->next; 18 | fast = fast->next->next; 19 | if(slow == fast) { 20 | return true; 21 | break; 22 | } 23 | } 24 | return false; 25 | } 26 | }; -------------------------------------------------------------------------------- /linked-list/delete-node.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 | ListNode* prev = NULL; 13 | while(node != NULL && node->next != NULL) { 14 | node->val = node->next->val; 15 | prev=node; 16 | node = node->next; 17 | } 18 | prev->next = NULL; 19 | delete(node); 20 | } 21 | }; -------------------------------------------------------------------------------- /linked-list/detect-loop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | int data; 6 | Node* next; 7 | 8 | Node(int data) { 9 | this -> data = data; 10 | this -> next = NULL; 11 | } 12 | }; 13 | 14 | bool detectLoop(Node* head) { 15 | if(!head || !head->next) return false; 16 | Node* temp = head; 17 | map mp; 18 | while(temp) { 19 | if(mp[temp]) return true; 20 | mp[temp] = true; 21 | temp = temp->next; 22 | } 23 | return false; 24 | } -------------------------------------------------------------------------------- /linked-list/doubly-insertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | Node* prev; 9 | 10 | Node(int d) { 11 | this -> data = d; 12 | Node* next = NULL; 13 | Node* prev = NULL; 14 | } 15 | }; 16 | 17 | void insertHead(Node* &head,int d) { 18 | Node* temp = head; 19 | Node* newnode = new Node(d); 20 | temp -> next = head; 21 | head -> prev = temp; 22 | head = newnode; 23 | } -------------------------------------------------------------------------------- /linked-list/doubly-linked-list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | Node* prev; 9 | 10 | Node(int d) { 11 | this -> data = d; 12 | Node* next = NULL; 13 | Node* prev = NULL; 14 | } 15 | }; 16 | 17 | void print(Node* head) { 18 | Node* ptr = head; 19 | while(!ptr) { 20 | cout << ptr->data << endl; 21 | ptr = ptr->next; 22 | } 23 | cout<next; 32 | } 33 | cout<< count < 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | 9 | Node(int data) { 10 | this -> data = data; 11 | this -> next = NULL; 12 | } 13 | 14 | ~Node() { 15 | int val = this -> data; 16 | if(this -> next != NULL) { 17 | delete next; 18 | next = NULL; 19 | } 20 | } 21 | }; 22 | 23 | void deletenode(int pos, Node* &head) { 24 | if(pos == 1) { 25 | Node* temp = head; 26 | head = head->next; 27 | delete (head); 28 | } 29 | 30 | else { 31 | Node* curr = head; 32 | Node* prev = NULL; 33 | int i=1; 34 | while(i next; 37 | i++; 38 | } 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /linked-list/linked-list-insert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | 9 | Node(int data) { 10 | this -> data = data; 11 | this -> next = NULL; 12 | } 13 | }; 14 | 15 | void insertHead(Node* &head, int val) { 16 | Node* temp = new Node(val); 17 | temp->next = head; 18 | head = temp; 19 | } 20 | 21 | void insertTail(Node* &tail, int val) { 22 | Node* temp = new Node(val); 23 | tail->next = temp; 24 | tail = tail->next; 25 | } 26 | 27 | void insertPosition(Node* &head, int val, int k) { 28 | Node* temp = head; 29 | int i=1; 30 | while(i < k-1) { 31 | temp = temp->next; 32 | i++; 33 | } 34 | Node* nodeInsert = new Node(val); 35 | nodeInsert->next = temp->next; 36 | temp->next = nodeInsert; 37 | } 38 | 39 | void print(Node* &head) { 40 | Node* temp = head; 41 | while(temp != NULL) { 42 | cout << temp->data << " "; 43 | temp = temp->next; 44 | } 45 | cout << endl; 46 | } 47 | 48 | int main() { 49 | Node* node1 = new Node(10); 50 | } -------------------------------------------------------------------------------- /linked-list/random-node.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 | vector ans; 14 | Solution(ListNode* head) { 15 | ListNode* temp = head; 16 | while(temp != NULL) { 17 | ans.push_back(temp->val); 18 | temp=temp->next; 19 | } 20 | } 21 | 22 | int getRandom() { 23 | int n = ans.size(); 24 | int random = rand()%n; 25 | return ans[random]; 26 | } 27 | }; 28 | 29 | /** 30 | * Your Solution object will be instantiated and called as such: 31 | * Solution* obj = new Solution(head); 32 | * int param_1 = obj->getRandom(); 33 | */ -------------------------------------------------------------------------------- /linked-list/remove-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 | while (head && head->val == val) { 15 | ListNode* temp = head; 16 | head = head->next; 17 | delete temp; 18 | } 19 | ListNode* ptr = head; 20 | while (ptr && ptr->next != NULL) { 21 | if (ptr->next->val == val) { 22 | ListNode* temp = ptr->next; 23 | ptr->next = temp->next; 24 | delete temp; 25 | } 26 | else ptr = ptr->next; 27 | } 28 | return head; 29 | } 30 | }; -------------------------------------------------------------------------------- /linked-list/sort-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* sortList(ListNode* head) { 14 | vector ans; 15 | ListNode* ptr = head; 16 | while(ptr != NULL) { 17 | ans.push_back(ptr->val); 18 | ptr = ptr->next; 19 | } 20 | sort(ans.begin(),ans.end()); 21 | 22 | ListNode* newHead = NULL; 23 | ListNode* current = NULL; 24 | 25 | for (int value : ans) { 26 | ListNode* newNode = new ListNode(value); 27 | if (!newHead) { 28 | newHead = newNode; 29 | current = newNode; 30 | } else { 31 | current->next = newNode; 32 | current = newNode; 33 | } 34 | } 35 | 36 | return newHead; 37 | 38 | } 39 | }; -------------------------------------------------------------------------------- /math-concepts/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nishant-Tiwari24/data-structures-algorithms/27e5734fc42550105a2464c75dbcad30d7cd01da/math-concepts/.DS_Store -------------------------------------------------------------------------------- /math-concepts/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp.errorSquiggles": "disabled" 3 | } -------------------------------------------------------------------------------- /math-concepts/seive-erotosthones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int countPrimes(int n) { 6 | if (n <= 2) return 0; 7 | 8 | int count = 0; 9 | vector isPrime(n, true); 10 | isPrime[0] = isPrime[1] = false; 11 | 12 | for (int i = 2; i < n; i++) { 13 | if (isPrime[i]) { 14 | for (int j = 2 * i; j < n; j += i) { 15 | isPrime[j] = false; 16 | } 17 | } 18 | } 19 | for (int i = 2; i < n; i++) { 20 | if (isPrime[i]) { 21 | count++; 22 | } 23 | } 24 | return count; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /operators-for-loop/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp.errorSquiggles": "disabled" 3 | } -------------------------------------------------------------------------------- /operators-for-loop/bitwise-operators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 4; 6 | int b = 7; 7 | 8 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | for (int i = 0; i < 5; i++) 6 | { 7 | cout<<"hey"; 8 | cout<<"hi"; 9 | continue; //skips the itreation 10 | cout<<"no cout"; 11 | } 12 | return 0; 13 | } -------------------------------------------------------------------------------- /operators-for-loop/fibonacci-for.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 10; 6 | int a = 0; 7 | int b = 1; 8 | 9 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i = 1; 6 | for( ; ; ) { 7 | if (i<=n) { 8 | cout<=0 && b>=1; a--, b--) { 15 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int rem = 0; 8 | int sum = 0; 9 | int pro = 1; 10 | 11 | while(n!=0) { 12 | rem = n % 10; 13 | n = n/10; 14 | 15 | sum=sum + rem; 16 | pro = pro*rem; 17 | } 18 | 19 | cout<<"Diffrence of sum and product is : "< 2 | using namespace std; 3 | 4 | int main() { 5 | uint32_t n; 6 | int count = 0; 7 | cin>>n; 8 | 9 | while (n!=0) 10 | { 11 | if(n&1) { 12 | count++; 13 | } 14 | n = n>>1; 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | cout<< (9>>2) < 2 | using namespace std; 3 | 4 | int main() { 5 | for (int i = 0; i < 5; i++) 6 | { 7 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i = 7; 6 | cout<<(i++)< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | bool flag = 1; 8 | 9 | for(int i = 2; i 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | 9 | while(i<=n) { 10 | int j = 1; 11 | while(j<=n) { 12 | char val = 96 + i + j - 1; 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | 9 | while(i<=n) { 10 | int j = 1; 11 | char val = 96 + n - i + 1; 12 | while(j<=i) { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | while(i<=n) { 9 | int j = 1; 10 | char ch = j + 96; 11 | while(j<=n) { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | int count = 1; 9 | while(i<=n) { 10 | int j = 1; 11 | while(j<=n) { 12 | char ch = count + 96; 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | 9 | while(i<=n) { 10 | int j = 1; 11 | while(j<=i) { 12 | char val = 96 + n - i + j - 1; 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | 9 | while(i<=n) { 10 | int j = 1; 11 | char val = 96 + i; 12 | while(j<=n) { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | while(i<=n) { 9 | int j = 1; 10 | char ch = i + 96; 11 | while(j<=n) { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | 9 | while(i<=n) { 10 | int j = 1; 11 | while(j<=i) { 12 | char val = 96 + i; 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter the number of rows: "; 7 | cin>>n; 8 | int i = 0; 9 | 10 | while(i <= n) { 11 | int j = 0; // initialize inside 12 | while(j <= n) { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter the number of rows: "; 7 | cin>>n; 8 | int i = 0; 9 | 10 | while(i <= n) { 11 | int j = 0; // initialize inside 12 | while(j <= n) { 13 | cout<<"*"<<" "; 14 | j = j + 1; 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter the number of rows: "; 7 | cin>>n; 8 | int i = 0; 9 | int count = 0; 10 | 11 | while(i <= n) { 12 | int j = 0; // initialize inside 13 | while(j <= n) { 14 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int i = 1; 9 | while(i<=n) { 10 | int j = n; 11 | while(j) { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter number of rows: "; 7 | cin>>n; 8 | int i = 1; 9 | while(i <= n) { 10 | int space = n - i; 11 | while(space) { 12 | cout<<" "<<" "; 13 | space = space + 1; 14 | } 15 | 16 | int j = 1; 17 | while(j <= i) { 18 | cout<<"*"<<" "; 19 | j = j + 1; 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Ente the number of rows"; 7 | cin>>n; 8 | 9 | int i = 1; 10 | while(i <= n) { 11 | int spaces = n - i; 12 | while(spaces) { 13 | cout << " "; 14 | spaces = spaces - 1; 15 | } 16 | 17 | int j = 1; 18 | while(j <= n) { 19 | cout << j; 20 | j = j + 1; 21 | } 22 | 23 | int k = i - 1; 24 | while(k) { 25 | cout << k; 26 | k = k - 1; 27 | 28 | } 29 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | while(i<=n) { 9 | int j = 1; 10 | while(j<=n) { 11 | if(j > 0) { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter the number of rows: "; 7 | cin>>n; 8 | int i = 0; 9 | 10 | while(i <= n) { 11 | int j = 0; // initialize inside 12 | while(j <= n) { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int i = 1; 8 | while(i<=n) { 9 | int count = i; 10 | int j = 1; 11 | while(j<=n) { 12 | if(count > 0) { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int i = 1; 9 | 10 | while(i<=n) { 11 | int j = 1; 12 | int row = i; 13 | while(j<=i) { 14 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int i = 1; 9 | while(i<=n) { 10 | int j = 1; 11 | int count = i; 12 | while(j<=i) { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter the number of rows : "; 7 | cin>>n; 8 | 9 | int i = 1; 10 | while(i<=n) { 11 | int j = n - i + 1; // or j = 1; 12 | while(j<=n) { // j < = i 13 | cout<<"*"<<" "; 14 | j = j + 1; 15 | } 16 | cout<<"\n"; 17 | i = i + 1; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /patterns/star-spaces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cout<<"Enter number of rows: "; 7 | cin>>n; 8 | int i = 1; 9 | while(i <= n) { 10 | int j = n - i; 11 | while(j) { 12 | cout<<"*"<<" "; 13 | j = j + 1; 14 | } 15 | 16 | int spaces = 1; 17 | while(spaces <= i) { 18 | cout<<" "<<" "; 19 | spaces = spaces + 1; 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | void update2(int a = 0) { //default value 5 | a++; 6 | } 7 | 8 | int main() { 9 | int n = 5; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 5; 6 | int m = 5; 7 | int** arr = new int*[n]; 8 | 9 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main() { 5 | char ch = 'a'; 6 | cout<< sizeof(ch)<< endl; 7 | char *c = &ch; 8 | cout< 2 | using namespace std; 3 | 4 | int inline max(int &a, int &b) { 5 | return (a > b)? a : b; 6 | } 7 | 8 | int main() { 9 | int a = 1; 10 | int b = 5; 11 | cout< 2 | using namespace std; 3 | 4 | int main () { 5 | 6 | int num = 5 ; 7 | 8 | cout << num << endl; 9 | 10 | // address of Operator - & 11 | 12 | cout <<" address of num is " << &num << endl; 13 | 14 | int *ptr = # 15 | 16 | cout << "Address is : " << ptr << endl; 17 | cout << "value is : " << *ptr << endl; 18 | 19 | double d = 4.3; 20 | double *p2 = &d; 21 | 22 | cout << "Address is : " << p2 << endl; 23 | cout << "value is : " << *p2 << endl; 24 | 25 | cout << " size of integer is " << sizeof(num) << endl; 26 | cout << " size of pointer is " << sizeof(ptr) << endl; 27 | cout << " size of pointer is " << sizeof(p2) << endl; 28 | 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /pointers/macro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI 3.14 5 | 6 | int main() { 7 | cout< 2 | using namespace std; 3 | 4 | void update(int a) { 5 | a++; 6 | } 7 | 8 | void update2(int &a) { //refrence 9 | a++; 10 | } 11 | 12 | int main() { 13 | int n = 5; 14 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int arr[10] = {2,9,4,5,6,7,8,3,2,6}; 6 | cout<<"memory of first block of arr is "<<&arr[0]< 2 | using namespace std; 3 | 4 | int main() { 5 | char ch[9] = "computer"; 6 | cout<< ch << endl; 7 | char *c = &ch[0]; 8 | cout< 2 | using namespace std; 3 | 4 | int& update2(int a) { //bad practice 5 | a++; 6 | } 7 | 8 | int main() { 9 | int n = 5; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i = 5; 6 | int &j = i; 7 | j++; 8 | cout< 2 | using namespace std; 3 | 4 | bool search(int arr[], int n, int x, int s, int e) { 5 | int s=0; int e=n-1; 6 | if(s>e) return false; 7 | int m = s+(e-s)/2; 8 | if(arr[m] == x) return true; 9 | else if(arr[m] > x) search(arr, n, x,s, m-1); 10 | search(arr, n, x,m+1,e); 11 | } -------------------------------------------------------------------------------- /recursion/bubble-sort-recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void sortArray(int arr[], int n) { 5 | if(n == 0 || n == 1) return; 6 | for(int i=0; i arr[i+1]) swap(arr[i], arr[i+1]); 8 | sortArray(arr, n-1); 9 | } -------------------------------------------------------------------------------- /recursion/check-palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool checkPalindrome(string str, int i, int j) { 5 | if(i>j) return 0; 6 | if(str[i] != str[j]) return false; 7 | return checkPalindrome(str,i++,j--); 8 | } -------------------------------------------------------------------------------- /recursion/counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int counting(int n) { 5 | if(n == 0) 6 | return 1; 7 | return n + counting(n-1); 8 | } -------------------------------------------------------------------------------- /recursion/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n) { 5 | if(n == 0) return 1; 6 | return n*factorial(n-1); 7 | } -------------------------------------------------------------------------------- /recursion/linear-search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int search(int arr[], int n, int target) { 5 | if(n == 0) return 0; 6 | if(target == arr[0]) return 1; 7 | search(arr + 1, n - 1, target); 8 | } -------------------------------------------------------------------------------- /recursion/merge-sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int arr[], int s, int e) { 5 | int m = s + (e - s) / 2; 6 | int l1 = m - s + 1; 7 | int l2 = e - m; 8 | int *first = new int[l1]; 9 | int *second = new int[l2]; 10 | 11 | int mainArr = s; 12 | for (int i = 0; i < l1; i++) { 13 | first[i] = arr[mainArr++]; 14 | } 15 | mainArr = m + 1; 16 | for (int i = 0; i < l2; i++) { 17 | second[i] = arr[mainArr++]; 18 | } 19 | 20 | int i1 = 0; 21 | int i2 = 0; 22 | mainArr = s; 23 | while (i1 < l1 && i2 < l2) { 24 | if (first[i1] < second[i2]) { 25 | arr[mainArr++] = first[i1++]; 26 | } else { 27 | arr[mainArr++] = second[i2++]; 28 | } 29 | } 30 | while (i2 < l2) { 31 | arr[mainArr++] = second[i2++]; 32 | } 33 | while (i1 < l1) { 34 | arr[mainArr++] = first[i1++]; 35 | } 36 | delete[] first; 37 | delete[] second; 38 | } 39 | 40 | void mergeSort(int arr[], int s, int e) { 41 | if (s >= e) return; 42 | int m = s + (e - s) / 2; 43 | mergeSort(arr, s, m); 44 | mergeSort(arr, m + 1, e); 45 | merge(arr, s, e); 46 | } 47 | 48 | int main() { 49 | int arr[5] = {2, 3, 1, 4, 5}; 50 | int n = 5; 51 | 52 | mergeSort(arr, 0, n - 1); 53 | for (int i = 0; i < n; i++) 54 | cout << arr[i] << " "; 55 | cout << endl; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /recursion/n-stairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int countDistinctWays(int nStairs) { 3 | if(nStairs < 0) return 0; 4 | if(nStairs == 0)return 1; 5 | return countDistinctWays(nStairs - 1) + countDistinctWays(nStairs - 2); 6 | } -------------------------------------------------------------------------------- /recursion/palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void reverse(string &str, int i, int j) { 3 | if(i>j) 4 | return; 5 | swap(str[i],str[j]); 6 | i++; 7 | j--; 8 | reverse(str, i, j); 9 | } 10 | 11 | string reverseString(string str) 12 | { 13 | int i=0; int j=str.length() - 1; 14 | reverse(str,i,j); 15 | return str; 16 | } -------------------------------------------------------------------------------- /recursion/power-of-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int n) { 5 | if(n == 0) 6 | return 1; 7 | return 2*power(n-1); 8 | } -------------------------------------------------------------------------------- /recursion/power-recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int x,int n) { 5 | if(n == 0) return 1; 6 | if(n == 1) return x; 7 | int p = power(x,n/2); 8 | if(x&1) return p*p*x; 9 | else return p*p; 10 | } -------------------------------------------------------------------------------- /recursion/print-till-n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void factorial(int n) { 5 | if(n == 0) 6 | return; 7 | print(n-1); 8 | cout< 2 | using namespace std; 3 | 4 | int pivot(int arr[], int s, int e) { 5 | int p = arr[s];int count=0; 6 | for(int i=s+1;i<=e;i++) { 7 | if(arr[i] <= p) count++; 8 | } 9 | int pivotIndex=s+count; 10 | swap(arr[s],arr[pivotIndex]); 11 | int i=s;int j=e; 12 | while(ipivotIndex) { 13 | while(arr[i]<=p) i++; 14 | while(arr[j]>p) j--; 15 | if(ipivotIndex) swap(arr[i++],arr[j--]); 16 | } 17 | return pivotIndex; 18 | } 19 | 20 | void quickSort(int arr[], int s, int e) { 21 | if(s>=e) return; 22 | int p = pivot(arr, s, e); 23 | quickSort(arr, s, p-1); 24 | quickSort(arr,p+1,e); 25 | } 26 | 27 | int main() { 28 | int arr[5] = {4,3,5,2,9}; 29 | int n = 5; 30 | quickSort(arr,0,n-1); 31 | for(int i=0; i 2 | using namespace std; 3 | 4 | void saydigit(int n) { 5 | if(n == 0) return; 6 | int dig = n%10; 7 | n/=10; 8 | saydigit(n); 9 | cout << dig << endl; 10 | } -------------------------------------------------------------------------------- /recursion/sorted-recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool sorted(int * arr[],int n) { 5 | if(n==0 || n==1) return 1; 6 | if(arr[0] > arr[1]) return false; 7 | return sorted(arr+1,n-1); 8 | } -------------------------------------------------------------------------------- /recursion/src-destination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reachhome(int src,int dest) { 5 | if(src==dest) 6 | cout << "Reach Home" << endl; 7 | reachhome(src+1,dest); 8 | } -------------------------------------------------------------------------------- /recursion/sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sum(int arr[],int n) { 5 | if(n==0) return 0; 6 | if(n == 1) return arr[0]; 7 | return arr[0] + sum(arr + 1, n - 1); 8 | } 9 | 10 | int main() { 11 | int arr[] = {1, 2, 3, 4, 5}; 12 | int size = sizeof(arr) / sizeof(arr[0]); 13 | 14 | int result = sum(arr, size); 15 | 16 | cout << "Sum of array elements: " << result << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /sorting/bucket-sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void bucketSort(vector& arr) { 8 | int n = arr.size(); 9 | vector> bucket(n); 10 | 11 | for(int i=0; i arr = {0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68}; 30 | 31 | cout << "Original array: "; 32 | for (double val : arr) { 33 | cout << val << " "; 34 | } 35 | cout << endl; 36 | 37 | bucketSort(arr); 38 | 39 | cout << "Sorted array: "; 40 | for (double val : arr) { 41 | cout << val << " "; 42 | } 43 | cout << endl; 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /sorting/insertion-sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void insertionSort(int n, vector &arr){ 3 | for(int i = 1; i=0; j--) { 7 | 8 | if(arr[j] > temp) { 9 | arr[j+1] = arr[j]; 10 | } 11 | else { 12 | break; 13 | } 14 | } 15 | 16 | arr[j+1] = temp; 17 | } 18 | } -------------------------------------------------------------------------------- /sorting/optimised-bubble-sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void bubbleSort(vector& arr, int n) 3 | { 4 | for(int i=1; i arr[j+1]) 10 | swap(arr[j], arr[j+1]); 11 | swapped = true; 12 | } 13 | 14 | if(swapped == false) { 15 | break; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sorting/selection-sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void selectionSort(vector& arr, int n) 3 | { 4 | for(int i = 0; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int leastInterval(vector& tasks, int n) { 9 | int freq[26] = {0}; 10 | for(char task : tasks){ 11 | freq[task - 'A']++; 12 | } 13 | sort(begin(freq), end(freq), greater()); 14 | int chunk = freq[0] - 1; 15 | int idle = chunk * n; 16 | 17 | for(int i = 1; i < 26 && freq[i] > 0; i++){ 18 | idle -= min(chunk, freq[i]); 19 | } 20 | 21 | return idle < 0 ? tasks.size() : tasks.size() + idle; 22 | } 23 | }; 24 | 25 | int main() { 26 | // Input 27 | int taskCount, n; 28 | cin >> taskCount; 29 | vector tasks(taskCount); 30 | for (int i = 0; i < taskCount; i++) { 31 | cin >> tasks[i]; 32 | } 33 | cin >> n; 34 | 35 | Solution solution; 36 | int minInterval = solution.leastInterval(tasks, n); 37 | 38 | // Output 39 | cout << "Minimum interval: " << minInterval << endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /switch-functions/amount-notes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int amount; 6 | cout << "Enter the amount: "; 7 | cin >> amount; 8 | 9 | int notes; 10 | 11 | switch (notes) { 12 | case 100: 13 | int hundred = amount / 100; 14 | amount = amount - (hundred * 100); 15 | cout << "Hundred rupee notes: " << hundred << endl; 16 | break; 17 | case 50: 18 | int fifty = amount / 50; 19 | amount = amount - (fifty * 50); 20 | cout << "Fifty rupee notes: " << fifty << endl; 21 | break; 22 | case 20: 23 | int twenty = amount / 20; 24 | amount = amount - (twenty * 20); 25 | cout << "Twenty rupee notes: " << twenty << endl; 26 | break; 27 | case 10: 28 | int ten = amount / 10; 29 | amount = amount - (ten * 10); 30 | cout << "Ten rupee notes: " << ten << endl; 31 | break; 32 | case 1: 33 | int one = amount / 1; 34 | amount = amount - (one * 1); 35 | cout << "One rupee notes: " << one << endl; 36 | break; 37 | default: 38 | cout << "Amount is less than 1" << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /switch-functions/calculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char op; 6 | cout<<"Enter the operator: "; 7 | cin>>op; 8 | int operand1, operand2; 9 | cout<<"Enter two operands: "; 10 | cin>>operand1>>operand2; 11 | 12 | switch(op) { 13 | case '+': cout< 2 | using namespace std; 3 | 4 | int fact(int r) { 5 | if(r == 0) { 6 | return 1; 7 | } 8 | int res = 1; 9 | 10 | for(int i = 1; i<=r; i++) { 11 | res *= i; 12 | } 13 | return res; 14 | } 15 | 16 | int nCr(int n, int r) { 17 | return fact(n)/(fact(n-r) * fact(r)); 18 | } 19 | 20 | int main() { 21 | int n; 22 | cin >> n; 23 | int r; 24 | cin >> r; 25 | cout< 2 | using namespace std; 3 | 4 | bool isEven(int a) { 5 | if(a&1) { 6 | return true; 7 | } 8 | else { 9 | return false; 10 | } 11 | } -------------------------------------------------------------------------------- /switch-functions/nested-switch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int ch = '1'; 6 | cout << "Enter a character: "; 7 | cin >> ch; 8 | 9 | switch(ch) { 10 | case '1': cout<<"first"< 2 | using namespace std; 3 | 4 | int dummy(int n) { 5 | return n++; 6 | } 7 | 8 | int main() { 9 | int n; 10 | cin>>n; 11 | dummy(n); 12 | cout< 2 | using namespace std; 3 | 4 | int power(int a,int b) { 5 | int result = 1; 6 | while (b > 0) { 7 | result *= a; 8 | } 9 | return result; 10 | } 11 | 12 | int main() { 13 | int a = 5, b = 10; 14 | cout << "The value of x is: " << power(a,b) << endl; 15 | } -------------------------------------------------------------------------------- /switch-functions/prime-function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPrime(int n) { 5 | for(int i = 2; i < n; i++) { 6 | if(n%i == 0) 7 | return false; 8 | } 9 | return true; 10 | } 11 | 12 | int main() { 13 | int n; 14 | cout << "Enter the number: "; 15 | cin >> n; 16 | 17 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int num; 6 | num = 2; 7 | 8 | switch (num) 9 | { 10 | case 1: cout<<"first case"; 11 | break; 12 | 13 | case 2: cout<<"second case\n"; 14 | break; 15 | 16 | default: 17 | break; 18 | } 19 | } -------------------------------------------------------------------------------- /switch-functions/void-counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void counting(int n) { 5 | int i = 0; 6 | while(n>0) { 7 | cout<> n; 16 | counting(n); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /tricks/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang build active file", 6 | "command": "/usr/bin/clang", 7 | "args": [ 8 | "-fcolor-diagnostics", 9 | "-fansi-escape-codes", 10 | "-g", 11 | "${file}", 12 | "-o", 13 | "${fileDirname}/${fileBasenameNoExtension}" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": { 22 | "kind": "build", 23 | "isDefault": true 24 | }, 25 | "detail": "Task generated by Debugger." 26 | } 27 | ], 28 | "version": "2.0.0" 29 | } -------------------------------------------------------------------------------- /tricks/sum-n-natural.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | string insertCommas(int num) { 7 | string str = to_string(num); 8 | int n = str.size(); 9 | int commas = (n - 1) / 3; 10 | 11 | // Create a reversed string 12 | string reversedStr; 13 | for (int i = n - 1; i >= 0; i--) { 14 | reversedStr.push_back(str[i]); 15 | } 16 | 17 | // Insert commas into the reversed string 18 | for (int i = 1; i <= commas; i++) { 19 | reversedStr.insert(i * 4 - 1, ","); 20 | } 21 | 22 | // Reverse the reversed string to get the final result 23 | string result; 24 | for (int i = reversedStr.size() - 1; i >= 0; i--) { 25 | result.push_back(reversedStr[i]); 26 | } 27 | 28 | return result; 29 | } 30 | 31 | int main() { 32 | int num = 123456789; 33 | cout << insertCommas(num) << 34 | --------------------------------------------------------------------------------