├── 1 Two Sum Easy.cpp ├── 10 Regular Expression Matching Hard.java ├── 100 Same Tree Easy.cpp ├── 1004 Max Consecutive Ones III Medium.cpp ├── 101 Symmetric Tree Easy.cpp ├── 1010. Pairs of Songs With Total Durations Divisible by 60 Medium.cpp ├── 1011 Capacity To Ship Packages Within D Days Medium.cpp ├── 102 Binary Tree Level Order Traversal Medium.cpp ├── 1020. Number of Enclaves.cpp ├── 1025 Divisor Game Easy.cpp ├── 1026 Maximum Difference Between Node and Ancestor Medium.cpp ├── 103 Binary Tree Zigzag Level Order Traversal Medium.cpp ├── 104 Maximum Depth of Binary Tree Easy.cpp ├── 1046 Last Stone Weight Easy.cpp ├── 1047 Remove All Adjacent Duplicates In String Easy.cpp ├── 105 Construct Binary Tree from Preorder and Inorder Traversal Medium.cpp ├── 107 Binary Tree Level Order Traversal II Medium.cpp ├── 1071. Greatest Common Divisor of Strings Easy.cpp ├── 108 Convert Sorted Array to Binary Search Tree Easy.cpp ├── 109. Convert Sorted List to Binary Search Tree Medium.cpp ├── 1091. Shortest Path in Binary Matrix Medium.cpp ├── 11 Container With Most Water Medium.java ├── 110 Balanced Binary Tree Easy.cpp ├── 112 Path Sum Easy.cpp ├── 1122 Relative Sort Array Easy.cpp ├── 1129. Shortest Path with Alternating Colors Medium.cpp ├── 113 Path Sum II Medium.cpp ├── 114 Flatten Binary Tree to Linked List Medium.cpp ├── 1155 Number of Dice Rolls With Target Sum Medium.cpp ├── 116 Populating Next Right Pointers in Each Node Medium.cpp ├── 1161 Maximum Level Sum of a Binary Tree Medium.cpp ├── 1162. As Far from Land as Possible Medium.cpp ├── 117 Populating Next Right Pointers in Each Node II Medium.cpp ├── 12 Integer to Roman Medium.cpp ├── 120. Triangle Medium.cpp ├── 1207 Unique Number of Occurrences Easy.cpp ├── 1209 Remove All Adjacent Duplicates in String II Medium.cpp ├── 122 Best Time to Buy and Sell Stock II Medium.cpp ├── 1235 Maximum Profit in Job Scheduling Hard.cpp ├── 1239 Maximum Length of a Concatenated String with Unique Characters Medium.cpp ├── 124 Binary Tree Maximum Path Sum Hard.cpp ├── 129 Sum Root to Leaf Numbers Medium.cpp ├── 1290 Convert Binary Number in a Linked List to Integer Easy.cpp ├── 1293 Shortest Path in a Grid with Obstacles Elimination Hard.cpp ├── 1295 Find Numbers with Even Number of Digits Easy.cpp ├── 130. Surrounded Regions Medium.cpp ├── 1305 All Elements in Two Binary Search Trees Medium.cpp ├── 131 Palindrome Partitioning Medium.cpp ├── 1310 XOR Queries of a Subarray Medium.cpp ├── 1318 Minimum Flips to Make a OR b Equal to c Medium.cpp ├── 1323 Maximum 69 Number Easy.cpp ├── 1328 Break a Palindrome Medium.cpp ├── 1329 Sort the Matrix Diagonally Medium.cpp ├── 1332 Remove Palindromic Subsequences Easy.cpp ├── 1333 Filter Restaurants by Vegan-Friendly, Price and Distance Medium.cpp ├── 1335 Minimum Difficulty of a Job Schedule Hard.cpp ├── 1338 Reduce Array Size to The Half Medium.cpp ├── 1339 Maximum Product of Splitted Binary Tree Medium.cpp ├── 1343 Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold Medium.cpp ├── 1347 Minimum Number of Steps to Make Two Strings Anagram Medium.cpp ├── 1348 Tweet Counts Per Frequency Medium.cpp ├── 1352 Product of the Last K Numbers Medium.cpp ├── 1353 Maximum Number of Events That Can Be Attended Medium.cpp ├── 1358 Number of Substrings Containing All Three Characters.cpp ├── 136 Single Number Easy.cpp ├── 137 Single Number II Medium.cpp ├── 1382 Balance a Binary Search Tree Medium.cpp ├── 1383 Maximum Performance of a Team Hard.cpp ├── 1396 Design Underground System Medium.cpp ├── 14 Longest Common Prefix Easy.cpp ├── 1400 Construct K Palindrome Strings Medium.cpp ├── 1410 HTML Entity Parser Medium.cpp ├── 1411 Number of Ways to Paint N × 3 Grid Hard.cpp ├── 1414 Find the Minimum Number of Fibonacci Numbers Whose Sum Is K Medium.cpp ├── 142. Linked List Cycle II Medium.cpp ├── 1423 Maximum Points You Can Obtain from Cards Medium.cpp ├── 144 Binary Tree Preorder Traversal Easy.cpp ├── 1444. Number of Ways of Cutting a Pizza Hard.cpp ├── 1448 Count Good Nodes in Binary Tree Medium.cpp ├── 145 Binary Tree Postorder Traversal Easy.cpp ├── 1456 Maximum Number of Vowels in a Substring of Given Length Medium.cpp ├── 1457 Pseudo-Palindromic Paths in a Binary Tree Medium.cpp ├── 1457 Pseudo-Palindromic Paths in a Binary Tree.cpp ├── 1470. Shuffle the Array Easy.cpp ├── 148. Sort List Medium.cpp ├── 1480 Running Sum of 1d Array Easy.cpp ├── 149. Max Points on a Line Hard.cpp ├── 1493 Longest Subarray of 1's After Deleting One Element Medium.cpp ├── 1497 Check If Array Pairs Are Divisible by k Medium.cpp ├── 1498 Number of Subsequences That Satisfy the Given Sum Condition Medium.cpp ├── 15 3Sum Medium.cpp ├── 150 Evaluate Reverse Polish Notation Medium.cpp ├── 1504. Count Submatrices With All Ones Medium.cpp ├── 151 Reverse Words in a String Medium.cpp ├── 152 Maximum Product Subarray Medium.cpp ├── 1523. Count Odd Numbers in an Interval Range Easy.cpp ├── 153. Find Minimum in Rotated Sorted Array Medium.cpp ├── 1530 Number of Good Leaf Nodes Pairs Medium.cpp ├── 1531 String Compression II Hard.cpp ├── 1539. Kth Missing Positive Number Easy.cpp ├── 1544 Make The String Great Easy.cpp ├── 155 Min Stack Medium.cpp ├── 1567 Maximum Length of Subarray With Positive Product Medium.cpp ├── 1577 Number of Ways Where Square of Number Is Equal to Product of Two Numbers Medium.cpp ├── 1578 Minimum Time to Make Rope Colorful Medium.cpp ├── 1588 Sum of All Odd Length Subarrays Easy.cpp ├── 1593 Split a String Into the Max Number of Unique Substrings Medium.cpp ├── 1599 Maximum Profit of Operating a Centennial Wheel Medium.cpp ├── 16 3Sum Closest Medium.cpp ├── 1603 Design Parking System Easy.cpp ├── 1605 Find Valid Matrix Given Row and Column Sums Medium.cpp ├── 1609 Even Odd Tree Medium.cpp ├── 162. Find Peak Element Medium.cpp ├── 1624 Largest Substring Between Two Equal Characters Easy.cpp ├── 1625 Lexicographically Smallest String After Applying Operations Medium.cpp ├── 1626 Best Team With No Conflicts Medium.cpp ├── 1626. Best Team With No Conflicts Medium.cpp ├── 1627 Graph Connectivity With Threshold Hard.cpp ├── 1630 Arithmetic Subarrays Medium.cpp ├── 1631 Path With Minimum Effort Medium.cpp ├── 1636 Sort Array by Increasing Frequency.cpp ├── 1637 Widest Vertical Area Between Two Points Containing No Points Medium.cpp ├── 1641 Count Sorted Vowel Strings Medium.cpp ├── 1642 Furthest Building You Can Reach Medium.cpp ├── 1646 Get Maximum in Generated Array Easy.cpp ├── 1657 Determine if Two Strings Are Close Medium.cpp ├── 1658 Minimum Operations to Reduce X to Zero Medium.cpp ├── 1662 Check If Two String Arrays are Equivalent Easy.cpp ├── 167. Two Sum II - Input Array Is Sorted Medium.cpp ├── 1670 Design Front Middle Back Queue Medium.cpp ├── 1675. Minimize Deviation in Array Hard.cpp ├── 1680 Concatenation of Consecutive Binary Numbers Medium.cpp ├── 1684 Count the Number of Consistent Strings Easy.cpp ├── 1688 Count of Matches in Tournament Easy.cpp ├── 169 Majority Element Easy.cpp ├── 1695 Maximum Erasure Value Medium3 ├── 17. Letter Combinations of a Phone Number Medium.cpp ├── 1700 Number of Students Unable to Eat Lunch Easy.cpp ├── 1701 Average Waiting Time Medium.cpp ├── 1702 Maximum Binary String After Change Medium ├── 1704 Determine if String Halves Are Alike Easy.cpp ├── 1706 Where Will the Ball Fall Medium.cpp ├── 1720 Decode XORed Array Easy.cpp ├── 173. Binary Search Tree Iterator Medium.cpp ├── 1770 Maximum Score from Performing Multiplication Operations Hard.cpp ├── 1770 Maximum Score from Performing Multiplication Operations.cpp ├── 18. 4Sum Medium.cpp ├── 1832 Check if the Sentence Is Pangram Easy.cpp ├── 188 Best Time to Buy and Sell Stock IV Hard.java ├── 189 Rotate Array Medium.cpp ├── 19 Remove Nth Node From End of List Medium.cpp ├── 1926 Nearest Exit from Entrance in Maze Medium.cpp ├── 199 Binary Tree Right Side View Medium.cpp ├── 1996 The Number of Weak Characters in the Game Medium.cpp ├── 2 Add Two Numbers Medium.cpp ├── 20 Valid Parentheses Easy.cpp ├── 200 Number of Islands Medium.cpp ├── 2007 Find Original Array From Doubled Array Medium.cpp ├── 202 Happy Number Easy.cpp ├── 206 Reverse Linked List Easy.c ├── 206 Reverse Linked List Easy.cpp ├── 207. Course Schedule Medium.cpp ├── 209 Minimum Size Subarray Sum Medium.cpp ├── 2095 Delete the Middle Node of a Linked List Medium.cpp ├── 210. Course Schedule II Medium.cpp ├── 212 Word Search II Hard.cpp ├── 213. House Robber II Medium.cpp ├── 2131 Longest Palindrome by Concatenating Two Letter Words Medium.cpp ├── 2136 Earliest Possible Day of Full Bloom Hard.cpp ├── 215. Kth Largest Element in an Array Medium.cpp ├── 217 Contains Duplicate Easy.cpp ├── 218 The Skyline Problem Hard.cpp ├── 219 Contains Duplicate II Easy.cpp ├── 22. Generate Parentheses Medium.cpp ├── 222 Count Complete Tree Nodes Medium.cpp ├── 2225 Find Players With Zero or One Losses Medium.cpp ├── 223 Rectangle Area Medium.cpp ├── 224 Basic Calculator Hard.cpp ├── 225 Implement Stack using Queues Easy.cpp ├── 2256 Minimum Average Difference Medium.cpp ├── 226 Invert Binary Tree Easy.cpp ├── 23. Merge k Sorted Lists Hard.cpp ├── 230. Kth Smallest Element in a BST Medium.cpp ├── 2306. Naming a Company Hard.cpp ├── 232 Implement Queue using Stacks Easy.cpp ├── 234 Palindrome Linked List Easy.cpp ├── 235 Lowest Common Ancestor of a Binary Search Tree Medium.cpp ├── 236 Lowest Common Ancestor of a Binary Tree Medium.cpp ├── 237 Delete Node in a Linked List Medium.cpp ├── 238 Product of Array Except Self Medium.cpp ├── 239 Sliding Window Maximum Hard.java ├── 2413 Smallest Even Multiple Easy.cpp ├── 2433 Find The Original Array of Prefix Xor Medium.cpp ├── 2444. Count Subarrays With Fixed Bounds Hard.cpp ├── 2477. Minimum Fuel Cost to Report to the Capital Medium.cpp ├── 258 Add Digits Easy.cpp ├── 260 Single Number III Medium.cpp ├── 268 Missing Number Easy.cpp ├── 278. First Bad Version Easy.cpp ├── 279 Perfect Squares Medium.cpp ├── 28 Find the Index of the First Occurrence in a String Medium.cpp ├── 283 Move Zeroes Easy.cpp ├── 287. Find the Duplicate Number Medium.cpp ├── 29. Divide Two Integers Medium ├── 295 Find Median from Data Stream Hard.cpp ├── 299. Bulls and Cows Medium.cpp ├── 3 Longest Substring Without Repeating Characters Medium.java ├── 322. Coin Change Medium.cpp ├── 326 Power of Three Easy.cpp ├── 328 Odd Even Linked List Medium.cpp ├── 33. Search in Rotated Sorted Array Medium.cpp ├── 334 Increasing Triplet Subsequence Medium.cpp ├── 336 Palindrome Pairs Hard.cpp ├── 338 Counting Bits Easy.cpp ├── 34 Find First and Last Position of Element in Sorted Array Medium.cpp ├── 344 Reverse String Easy.cpp ├── 345 Reverse Vowels of a String Easy.cpp ├── 347 Top K Frequent Elements Medium.cpp ├── 352. Data Stream as Disjoint Intervals Hard.cpp ├── 36 Valid Sudoku Medium.cpp ├── 363 Max Sum of Rectangle No Larger Than K Hard.cpp ├── 37 Sudoku Solver Hard.cpp ├── 374 Guess Number Higher or Lower Easy.cpp ├── 38 Count and Say Medium.cpp ├── 380 Insert Delete GetRandom O(1) Medium.cpp ├── 383 Ransom Note Easy.cpp ├── 39 Combination Sum Medium.cpp ├── 392. Is Subsequence Easy.cpp ├── 393 UTF-8 Validation Medium.cpp ├── 394. Decode String Medium.cpp ├── 4 Median Of Two Sorted Arrays Hard.cpp ├── 40 Combination Sum II Medium.cpp ├── 404 Sum of Left Leaves Easy.cpp ├── 409. Longest Palindrome Easy.cpp ├── 413. Arithmetic Slices Medium.cpp ├── 417 Pacific Atlantic Water Flow Medium.cpp ├── 42 Trapping Rain Water Hard.cpp ├── 424. Longest Repeating Character Replacement Medium.cpp ├── 429 N-ary Tree Level Order Traversal Medium.cpp ├── 43. Multiply Strings Medium.cpp ├── 433 Minimum Genetic Mutation Medium.cpp ├── 437. Path Sum III Medium.cpp ├── 443. String Compression Medium.cpp ├── 446 Arithmetic Slices II - Subsequence Hard.cpp ├── 45. Jump Game II Medium.cpp ├── 451 Sort Characters By Frequency Medium.cpp ├── 46 Permutations Medium.cpp ├── 472. Concatenated Words Hard.cpp ├── 48 Rotate Image Medium.cpp ├── 485 Max Consecutive Ones Easy.cpp ├── 49 Group Anagrams Medium.cpp ├── 496 Next Greater Element I Easy.cpp ├── 5 Longest Palindromic Substring Medium.java ├── 502. IPO Hard.cpp ├── 509. Fibonacci Number Easy.cpp ├── 51 N-Queens Hard.cpp ├── 515 Find Largest Value in Each Tree Row Medium.cpp ├── 523 Continuous Subarray Sum Medium.cpp ├── 53 Maximum Subarray Medium.cpp ├── 54. Spiral Matrix Medium.cpp ├── 540. Single Element in a Sorted Array Medium.cpp ├── 542. 01 Matrix Medium.cpp ├── 543 Diameter of Binary Tree Easy.cpp ├── 547. Number of Provinces Medium.cpp ├── 557 Reverse Words in a String III Easy.cpp ├── 56. Merge Intervals Medium.cpp ├── 560 Subarray Sum Equals K Medium.cpp ├── 567. Permutation in String Medium.cpp ├── 572 Subtree of Another Tree Easy.cpp ├── 587 Erect the Fence Hard.cpp ├── 589. N-ary Tree Preorder Traversal Easy.cpp ├── 6 Zigzag Conversion Medium.java ├── 60 Permutation Sequence Hard.cpp ├── 606 Construct String from Binary Tree Easy.cpp ├── 609 Find Duplicate File in System Medium.cpp ├── 61 Rotate List Medium.cpp ├── 611. Valid Triangle Number Medium.cpp ├── 617 Merge Two Binary Trees Easy.cpp ├── 62. Unique Paths Medium.cpp ├── 622 Design Circular Queue Medium.cpp ├── 622. Design Circular Queue Medium.cpp ├── 623 Add One Row to Tree Medium.cpp ├── 637 Average of Levels in Binary Tree Easy.cpp ├── 645 Set Mismatch Easy.cpp ├── 652. Find Duplicate Subtrees Medium.cpp ├── 653 Two Sum IV - Input is a BST.cpp ├── 658 Find K Closest Elements Medium.cpp ├── 66 Plus One Easy.cpp ├── 673. Number of Longest Increasing Subsequence Medium.cpp ├── 684 Redundant Connection Medium.cpp ├── 695. Max Area of Island Medium.cpp ├── 7 Reverse Integer Medium.java ├── 700 Search in a Binary Search Tree Easy.cpp ├── 704 Binary Search Easy.cpp ├── 713. Subarray Product Less Than K Medium.cpp ├── 718 Maximum Length of Repeated Subarray Medium.cpp ├── 72. Edit Distance Hard.cpp ├── 724. Find Pivot Index Easy.cpp ├── 725 Split Linked List in Parts Medium.cpp ├── 732. My Calendar III Hard.cpp ├── 733. Flood Fill Easy.cpp ├── 74. Search a 2D Matrix Medium.cpp ├── 746. Min Cost Climbing Stairs Easy.cpp ├── 75 Sort Colors Medium.cpp ├── 76 Minimum Window Substring Hard.cpp ├── 766 Toeplitz Matrix Easy.cpp ├── 77. Combinations Medium.cpp ├── 78. Subsets Medium.cpp ├── 784. Letter Case Permutation Medium.cpp ├── 785. Is Graph Bipartite? Medium.cpp ├── 79 Word Search Medium.cpp ├── 791 Custom Sort String Medium.cpp ├── 8 String to Integer (atoi) Medium.cpp ├── 802. Find Eventual Safe States Medium.cpp ├── 814 Binary Tree Pruning Medium.cpp ├── 82. Remove Duplicates from Sorted List II Medium.cpp ├── 83 Remove Duplicates from Sorted List Easy.cpp ├── 835 Image Overlap Medium.cpp ├── 838. Push Dominoes Medium.cpp ├── 84 Largest Rectangle in Histogram Hard.cpp ├── 844. Backspace String Compare Easy.cpp ├── 845. Longest Mountain in Array Medium.cpp ├── 852 Peak Index in a Mountain Array Medium.cpp ├── 856 Score of Parentheses Medium.cpp ├── 869 Reordered Power of 2 Medium.cpp ├── 872 Leaf-Similar Trees Easy.cpp ├── 875. Koko Eating Bananas Medium.cpp ├── 876 Middle of the Linked List Easy.cpp ├── 877 Stone Game Medium.cpp ├── 899 Orderly Queue Hard.cpp ├── 9 Palindrome Number.cpp ├── 90 Subsets II Medium.cpp ├── 901 Online Stock Span Medium.cpp ├── 904. Fruit Into Baskets Medium.cpp ├── 905 Sort Array By Parity Easy.cpp ├── 907 Sum of Subarray Minimums Medium monotonic stack.cpp ├── 91 Decode Ways Medium.cpp ├── 912. Sort an Array Medium.cpp ├── 923. 3Sum With Multiplicity Medium.cpp ├── 930 Binary Subarrays With Sum Medium.cpp ├── 937 Reorder Data in Log Files Medium.java ├── 938 Range Sum of BST Easy.cpp ├── 94 Binary Tree Inorder Traversal Easy.cpp ├── 947 Most Stones Removed with Same Row or Column Medium.cpp ├── 948 Bag of Tokens Medium.cpp ├── 953. Verifying an Alien Dictionary Easy.cpp ├── 967 Numbers With Same Consecutive Differences Medium.cpp ├── 974 Subarray Sums Divisible by K Medium.cpp ├── 976 Largest Perimeter Triangle Easy.cpp ├── 977 Squares of a Sorted Array Easy.cpp ├── 98 Validate Binary Search Tree Medium.cpp ├── 981 Time Based Key-Value Store Medium.cpp ├── 985 Sum of Even Numbers After Queries Medium.java ├── 986. Interval List Intersections Medium.cpp ├── 987 Vertical Order Traversal of a Binary Tree Hard.cpp ├── 989. Add to Array-Form of Integer Easy.cpp ├── 99. Recover Binary Search Tree Medium.cpp ├── 994 Rotting Oranges Medium.cpp ├── 997 Find the Town Judge Easy.cpp ├── AVL Tree Deletion.cpp ├── AVL Tree Insertion.cpp ├── Add 1 to a Linked List Number.cpp ├── Alien Dictionary.cpp ├── All Unique Permutations of an array.cpp ├── Ancestors in Binary Tree.cpp ├── Anti Diagonal Traversal of Matrix.cpp ├── Array Operations EASY GFG.cpp ├── Array Pair Sum Divisibility Problem.cpp ├── Array to BST.cpp ├── BFS of graph GFG.cpp ├── Better String.cpp ├── Binary Tree to BST.cpp ├── Binary matrix having maximum number of 1s Easy GFG.cpp ├── Binary matrix having maximum number of 1s Easy GFG.cpp ├── Bit Magic Easy GFG.cpp ├── Boolean Matrix.cpp ├── Bottom View of Binary Tree.cpp ├── Boundary traversal of matrix.cpp ├── Brothers From Different Roots.cpp ├── Bus Conductor Easy GFG.cpp ├── Buy Maximum Stocks if i stocks can be bought on i-th day.cpp ├── Cake Distribution Problem Hard GFG.cpp ├── CamelCase Pattern Matching Medium GFG.cpp ├── Candy.cpp ├── Check for BST.cpp ├── Check if Tree is Isomorphic.cpp ├── Check if a string is repetition of its substring of k-length.cpp ├── Check if all leaves are at same level.cpp ├── Check if string is rotated by two places.cpp ├── Check if strings are rotations of each other or not.cpp ├── Check whether BST contains Dead End.cpp ├── Check whether K-th bit is set or not.cpp ├── Chocolate Distribution Problem GFG.cpp ├── Coin change. cpp ├── Column name from a given column number.cpp ├── Consecutive 1's not allowed.cpp ├── Construct Binary Tree from Parent Array.cpp ├── Count Binary Strings With No Consecutive 1s Hard GFG.cpp ├── Count Inversions.cpp ├── Count More than n by k Occurences.cpp ├── Count Smaller elements.cpp ├── Count Total Set bits MediumGFG.cpp ├── Count digit groupings of a number.cpp ├── Count number of hops.cpp ├── Count number of substrings.cpp ├── Count ways to N'th Stair(Order does not matter).cpp ├── Course Schedule.cpp ├── Coverage of all Zeros in a Binary Matrix.cpp ├── Cutting Binary String Medium GFG.cpp ├── DFS of Graph GFG.cpp ├── Decimal Equivalent of Binary Linked List.cpp ├── Delete Middle Element Of a stack GFG.cpp ├── Delete a Node in Single Linked List.cpp ├── Delete node in Doubly Linked List.cpp ├── Delete nodes having greater value on right.cpp ├── Detect Cycle using DSU.cpp ├── Determinant of a Matrix.cpp ├── Determine if Two Trees are Identical.cpp ├── Distribute candies in a binary tree.cpp ├── Duplicate Subtrees.cpp ├── Duplicate subtree in Binary Tree.cpp ├── Equilibrium Point.cpp ├── Euler circuit and Path.cpp ├── Eventual Safe States.cpp ├── Express as sum of power of natural numbers GFG.cpp ├── Expression Add Operators Hard GFG.cpp ├── Extract the Number from the String.cpp ├── Find All Four Sum Numbers.cpp ├── Find Kth permutation Medium GFG.cpp ├── Find Maximum Equal sum of Three Stacks Medium GFG.cpp ├── Find Transition Point.cpp ├── Find Triplets with zero Sum GFG.cpp ├── Find duplicate rows in a binary matrix.cpp ├── Find duplicates in an array.cpp ├── Find first set bit.cpp ├── Find k-th smallest element in given n ranges Medium GFG.cpp ├── Find kth element of spiral matrix Medium GFG.cpp ├── Find length of Loop.cpp ├── Find maximum volume of a cuboid.cpp ├── Find the Closest Element in BST GFG.cpp ├── Find the closest pair from two arrays.cpp ├── Find the string in grid.cpp ├── First non repeating character in a stream GFG.cpp ├── Flip bits gfg.cpp ├── Floor in BST.cpp ├── Form a number divisible by 3 using array digits.cpp ├── Form a palindrome.cpp ├── Fraction pairs with sum 1 GFG.cpp ├── Frequencies of Limited Range Array Elements.cpp ├── Frequency Game Easy GFG.cpp ├── Game of XOR.cpp ├── Given a linked list of 0s, 1s and 2s, sort it GFG.cpp ├── Gold Mine Problem.cpp ├── Good Subtrees GFG.cpp ├── Grinding Geek.cpp ├── Hands of Straights Medium GFG .cpp ├── Heap Sort GFG.cpp ├── How Many X's?.cpp ├── Identical Linked Lists.cpp ├── Implement Atoi.cpp ├── Implement two stacks in an array Easy GFG.cpp ├── Inorder Successor in a bst GFG.cpp ├── Inorder Traversal and BST.cpp ├── Insert a node in a BST.cpp ├── Insertion Sort for Singly Linked List.cpp ├── Integral Points Inside Triangle.cpp ├── Intersection Point in Y Shaped Linked Lists.cpp ├── Intersection of two sorted Linked lists.cpp ├── Is it a tree ?.cpp ├── Isomorphic Strings.cpp ├── Job Sequencing Problem.cpp ├── K Sum Paths.cpp ├── K-Pangrams.cpp ├── K-th element of two Arrays.cpp ├── Knapsack with Duplicate Items.cpp ├── Kth Smallest.cpp ├── Kth ancestor in a tree GFG.cpp ├── Kth distance.cpp ├── Kth largest element in BST.cpp ├── LCS of three strings.cpp ├── Largest BST.cpp ├── Largest Number formed from an Array.cpp ├── Largest Sum Subarray of Size at least K.cpp ├── Largest number possible.cpp ├── Largest prime factor GFG.cpp ├── Largest rectangular sub-matrix whose sum is 0.cpp ├── Largest square formed in a matrix.cpp ├── Leaders in an array gfg.cpp ├── Leaf under budget.cpp ├── Least Prime Factor GFG.c[p ├── Left Rotate Matrix K times.cpp ├── Left View of Binary Tree.cpp ├── Leftmost and rightmost nodes of binary tree.cpp ├── Letters Collection.cpp ├── Level of Nodes.cpp ├── Level order traversal in spiral form GFG.cpp ├── Longest Common Prefix of Strings.cpp ├── Longest Common Subsequence GFG.cpp ├── Longest Common Substring.cpp ├── Longest K unique characters substring.cpp ├── Longest Palindrome Subsequence GFG.cpp ├── Longest Palindrome in a String Medium GFG.cpp ├── Longest Repeating Subsequence Medium GFG.cpp ├── Longest alternating subsequence.cpp ├── Longest increasing subsequence gfg.cpp ├── Lowest common ancestorGFg.cpp ├── Lucky Numbers.cpp ├── Majority Element II.cpp ├── Make Binary Tree From Linked List.cpp ├── Make Matrix Beautiful.cpp ├── Make Palindrome Easy GFG.cpp ├── Make array elements unique.cpp ├── Max Coins Medium GFG.cpp ├── Max Sum Subarray of size K.cpp ├── Max sum path in two arrays.cpp ├── Maximize Array Value After Rearrangement.cpp ├── Maximize The Cut Segments.cpp ├── Maximum Connected group.cpp ├── Maximum Difference.cpp ├── Maximum Identical Bowls Easy GFG.cpp ├── Maximum Meetings in One Room.cpp ├── Maximum Product Subarray.cpp ├── Maximum Sum Combination.cpp ├── Maximum index medium GFG.cpp ├── Maximum product subset of an array.cpp ├── Median of BST GFG.cpp ├── Merge two BST 's.cpp ├── Merger without extra space gap method GFG.cpp ├── Min distance between two given nodes of a Binary Tree.cpp ├── Minimize the Heights I.cpp ├── Minimize the Heights II.cpp ├── Minimum Multiplications to reach End.cpp ├── Minimum Operations.cpp ├── Minimum distance between two numbers.cpp ├── Minimum sum.cpp ├── Modified Game of Nim.cpp ├── Modify Linked List-1 Medium GFG.cpp ├── Mother Vertex.cpp ├── Move all zeroes to end of array.cpp ├── Multiply two strings.cpp ├── N meetings in one room.cpp ├── Nearly sorted.cpp ├── New Year Resolution.cpp ├── Next Smallest Palindrome gfg.cpp ├── Node at distance.cpp ├── Non Repeating Character Easy GFG.cpp ├── Non repeating numbers gfg.cpp ├── Normal BST to Balanced BST.cpp ├── Nth catalan number gfg.cpp ├── Nth fibonacci number gfg.cpp ├── Nth node from end of linked list Easy GFG.cpp ├── Number Of Enclaves.cpp ├── Number following a pattern.cpp ├── Number of occurrence.cpp ├── Number of paths.cpp ├── Number of subarrays with maximum values in given range.cpp ├── Overlapping Intervals.cpp ├── Painting the Fence.cpp ├── Pairwise swap elements of a linked list.cpp ├── Palindrome Linked List.cpp ├── Palindrome with minimum sum Medium GFG.cpp ├── Palindromic Partitioning.cpp ├── Panagram Checking.cpp ├── Partition Equal Subset Sum.cpp ├── Pascal Triangle.cpp ├── Paths from root with a specified sum.cpp ├── Peak element.cpp ├── Perfect Numbers.cpp ├── Perfect Sum Problem.cpp ├── Populate Inorder Successor for all nodes.cpp ├── Power of 2.cpp ├── Power of Numbers medium GFG.cpp ├── Powerfull Integer EASY GFG ├── Predecessor and Successor Medium GFG.cpp ├── Predict the Column.cpp ├── Print Bracket Number.cpp ├── Print Matrix in snake Pattern.cpp ├── Print Pattern.cpp ├── Print adjacency list.cpp ├── Print first n Fibonacci Numbers.cpp ├── Product array puzzle.cpp ├── Pythagorean Triplet.cpp ├── Queue Operations Easy GFG.cpp ├── Queue Reversal Basic GFG.cpp ├── Quick Sort medium GFG.cpp ├── Rat in a Maze Problem - I.cpp ├── Reach the Nth point.cpp ├── Rearrange an array with O(1) extra space GFG.cpp ├── Recamans sequence.cpp ├── Recursive sequence.cpp ├── Remaining String.cpp ├── Remove Duplicates.cpp ├── Remove K Digits.cpp ├── Remove all occurences of duplicates in a linked list.cpp ├── Remove duplicate element from sorted Linked List.cpp ├── Remove duplicate from an unsorted linked list GFG.cpp ├── Replace O's with X's.cpp ├── Reverse Coding Easy GFg.cpp ├── Reverse First K elements of Queue.cpp ├── Reverse a Doubly Linked List.cpp ├── Reverse a Linked List in groups of given size Medium GFG.cpp ├── Reverse a String gfg.cpp ├── Reverse a stack GFG.cpp ├── Reverse alternate nodes in Link List.cpp ├── Reversing the equation Easy GFG.cpp ├── Right view of Binary tree GFG.cpp ├── Rightmost different bit.cpp ├── Roman Number to Integer.cpp ├── Roof Top.cpp ├── Root to leaf path sum.cpp ├── Root to leaf paths sum.cpp ├── Rotate a Linked List.cpp ├── Row with max 1s.cpp ├── Santa Banta Medium GFG.cpp ├── Search Pattern (KMP-Algorithm).cpp ├── Search in Rotated Sorted Array.cpp ├── Segregate 0s and 1s.cpp ├── Sequence of Sequence.cpp ├── Shortest Common Supersequence.cpp ├── Shortest Path in Weighted undirected graph.cpp ├── Shortest Prime Path.cpp ├── Shortest Source to Destination Path GFG.cpp ├── Shortest path from 1 to n.cpp ├── Shortest path in Directed Acyclic Graph Medium.cpp ├── Shuffle Integers.cpp ├── Smallest Positive missing number medium GFG.cpp ├── Smallest number.cpp ├── Smallest window containing 0, 1 and 2.cpp ├── Smallest window in a string containing all the characters of another string.cpp ├── Smith Number.cpp ├── Solve Sudoku GFG.cpp ├── Sorted insert for circular linked list.cpp ├── Sorting Elements of an Array by Frequency.cpp ├── Spirally traversing a matrix.cpp ├── Split array in three equal sum subarrays.cpp ├── Square root of a number.cpp ├── Stickler Thief ├── Stock Buy and Sell – Max one Transaction Allowed.cpp ├── Stock Buy and Sell – Multiple Transaction Allowed.cpp ├── Stock buy and sell II medium GFG.cpp ├── String Mirror Easy GFG.cpp ├── String Permutations GFG.cpp ├── String's Count.cpp ├── Subarray with 0 sum.cpp ├── Subarray with given sum.cpp ├── Sum Tree.cpp ├── Sum of all divisors from 1 to n.cpp ├── Sum of bit differences.cpp ├── Sum of dependencies in a graph.cpp ├── Sum of upper and lower triangles.cpp ├── Sum without Adjacents.cpp ├── Sum-string.cpp ├── Summed Matrix.cpp ├── Surround the 1's.cpp ├── Swap and Maximize.cpp ├── Symmetric Tree.cpp ├── Techfest and the Queue.cpp ├── The Celebrity Problem.cpp ├── The Palindrome Pattern.cpp ├── Ticket Counter Easy GFG.cpp ├── Toeplitz matrix.cpp ├── Top K Frequent Elements in Array.cpp ├── Top k numbers in a stream.cpp ├── Topological sort Medium GFG.cpp ├── Total count.cpp ├── Total cuts Easy GFG.cpp ├── Trace Paths Easy GFG.cpp ├── Transform to prime.cpp ├── Transitive closure of a Graph.cpp ├── Transpose of Matrix Easy GFG.cpp ├── Union of Two Sorted Arrays with Distinct Elements.cpp ├── Unique Number of Occurrences Easy GFG.cpp ├── Unique rows in boolean matrix Easy GFG.cpp ├── Validate an IP Address.cpp ├── Vertex Cover.cpp ├── Vertical Width of a Binary Tree.cpp ├── Water the plants.cpp ├── Wave array.cpp ├── Winner of an election.cpp └── linked list of strings forms a palindrome.cpp /1 Two Sum Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | vector re; 5 | int i,j; 6 | 7 | for(i=0;i& nums, int k) { 4 | 5 | int left =0, right=0, maxLen=INT_MIN, zero= 0 ; 6 | 7 | while(right < nums.size()){ 8 | if(nums[right] == 0){ 9 | if(zero < k){ 10 | zero++ ; 11 | } 12 | else{ 13 | while(nums[left++] != 0) ; 14 | } 15 | } 16 | 17 | maxLen = max(maxLen, right-left+1) ; 18 | right++ ; 19 | } 20 | 21 | return maxLen ; 22 | } 23 | }; -------------------------------------------------------------------------------- /101 Symmetric Tree Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool ans=false; 4 | bool match(TreeNode* root1, TreeNode* root2){ 5 | if(root1!=NULL && root2!=NULL ) 6 | { 7 | bool a=match(root1->left,root2->right); 8 | bool b=match(root1->right,root2->left); 9 | 10 | if(root1->val==root2->val && a && b) 11 | return true; 12 | else 13 | return false; 14 | }else if(root1==NULL && root2==NULL) 15 | return true; 16 | else 17 | return false; 18 | } 19 | 20 | bool isSymmetric(TreeNode* root) { 21 | if(root==NULL) 22 | return true; 23 | return match(root->left,root->right); 24 | } 25 | }; -------------------------------------------------------------------------------- /1026 Maximum Difference Between Node and Ancestor Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dfs(TreeNode* root, int mx, int mn){ 4 | if(!root) return mx-mn; 5 | 6 | mx=max(mx, root->val); 7 | mn=min(mn,root->val); 8 | 9 | int left = dfs(root->left,mx,mn); 10 | int right = dfs(root->right,mx,mn); 11 | 12 | return max(left,right); 13 | } 14 | 15 | int maxAncestorDiff(TreeNode* root) { 16 | if(!root) return 0; 17 | return dfs(root,root->val,root->val); 18 | } 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /104 Maximum Depth of Binary Tree Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(TreeNode* root) { 4 | if(root==NULL) 5 | return 0; 6 | else return max(maxDepth(root->left),maxDepth(root->right))+1; 7 | } 8 | }; -------------------------------------------------------------------------------- /1046 Last Stone Weight Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastStoneWeight(vector& stones) { 4 | priority_queue q; 5 | int n =stones.size(); 6 | for(int i=0;i1){ 9 | int y=q.top(); 10 | q.pop(); 11 | int x=q.top(); 12 | q.pop(); 13 | if(y==x) continue; 14 | else 15 | q.push(y-x); 16 | } 17 | if(!q.size()) return 0; 18 | return q.top(); 19 | } 20 | }; -------------------------------------------------------------------------------- /1047 Remove All Adjacent Duplicates In String Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s) { 4 | stackst; 5 | string str=""; 6 | int n=s.size(); 7 | int i=0; 8 | while(i& nums){ 4 | if(low > end) 5 | return NULL ; 6 | 7 | int mid = (low+end)/2 ; 8 | TreeNode* root = new TreeNode(nums[mid]) ; 9 | 10 | root->left = solve(low, mid-1, nums) ; 11 | root->right = solve(mid+1, end, nums) ; 12 | 13 | return root ; 14 | } 15 | 16 | TreeNode* sortedArrayToBST(vector& nums) { 17 | return solve(0, nums.size()-1, nums) ; 18 | } 19 | }; -------------------------------------------------------------------------------- /11 Container With Most Water Medium.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxArea(int[] height) { 3 | int l=0; 4 | int r=height.length-1; 5 | int max=0; 6 | while(lleft),height(root->right))+1; 8 | } 9 | 10 | void inorder(TreeNode* root, bool &ans){ 11 | if(root!=NULL){ 12 | inorder(root->left,ans); 13 | int l=height(root->left); 14 | int h=height(root->right); 15 | if(abs(l-h)>1) 16 | ans=ans&& false; 17 | inorder(root->right,ans); 18 | } 19 | } 20 | bool isBalanced(TreeNode* root) { 21 | bool ans=true; 22 | inorder(root,ans); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /112 Path Sum Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasPathSum(TreeNode* root, int targetSum) { 4 | if(!root) return false; 5 | 6 | if(!root->left && !root->right){ 7 | if(targetSum-root->val==0) 8 | return true; 9 | } 10 | 11 | return hasPathSum(root->left, targetSum-root->val) or hasPathSum(root->right, targetSum-root->val); 12 | } 13 | }; -------------------------------------------------------------------------------- /113 Path Sum II Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector>ans; 4 | vectorpath; 5 | void dfs(TreeNode* root, int targetSum, int sum) 6 | { 7 | if (!root) 8 | return; 9 | path.push_back(root->val); 10 | sum+=root->val; 11 | 12 | if(sum == targetSum && !root->left && !root->right) { 13 | ans.push_back(path); 14 | } 15 | 16 | dfs(root->left,targetSum,sum); 17 | dfs(root->right,targetSum,sum); 18 | path.pop_back(); 19 | } 20 | vector> pathSum(TreeNode* root, int targetSum) { 21 | int sum = 0; 22 | dfs(root,targetSum,sum); 23 | return ans; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /120. Triangle Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumTotal(vector>& nums) { 4 | int n=nums.size(); 5 | vector dp(n,0); 6 | for(int i=0;i=0;i--){ 11 | for(int j=0;j& arr) { 4 | mapmp; 5 | 6 | for(auto a:arr) mp[a]++; 7 | 8 | for(auto it:mp){ 9 | auto x=it.second; 10 | int count=0; 11 | for(auto b:mp ){ 12 | auto y=b.second; 13 | if(y==x){ 14 | count++; 15 | if(count>1) 16 | return false; 17 | } 18 | } 19 | } 20 | return true; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /1209 Remove All Adjacent Duplicates in String II Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s, int k) { 4 | vector> st; 5 | for(auto c: s){ 6 | if(st.size()==0 || st.back().first!=c) 7 | st.push_back({c,1}); 8 | else 9 | st.back().second++; 10 | 11 | if(st.back().second==k) 12 | st.pop_back(); 13 | } 14 | string ans; 15 | for(auto c: st){ 16 | ans.append(c.second,c.first); 17 | } 18 | return ans; 19 | 20 | } 21 | }; -------------------------------------------------------------------------------- /122 Best Time to Buy and Sell Stock II Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | 5 | int profit=0; 6 | 7 | for(int i=1;iprices[i-1]) 9 | profit+=prices[i]-prices[i-1]; 10 | } 11 | return profit; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /124 Binary Tree Maximum Path Sum Hard.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(TreeNode* root, int& maxi){ 4 | if(!root) 5 | return 0 ; 6 | 7 | int left = solve(root->left, maxi) ; 8 | if(left < 0) left = 0 ; 9 | int right = solve(root->right, maxi) ; 10 | if(right < 0) right =0 ; 11 | 12 | maxi = max(maxi, left+right+root->val) ; 13 | 14 | return max(left, right) + root->val ; 15 | } 16 | 17 | int maxPathSum(TreeNode* root) { 18 | int maxi = INT_MIN ; 19 | solve(root, maxi) ; 20 | 21 | return maxi ; 22 | } 23 | }; -------------------------------------------------------------------------------- /129 Sum Root to Leaf Numbers Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int helper(TreeNode*root ,int sum){ 4 | if(root==NULL) return 0 ; 5 | 6 | sum =sum*10 + root->val ; 7 | if(!root->left && !root->right) return sum ; 8 | 9 | return helper(root->left, sum) + helper(root->right ,sum) ; 10 | } 11 | 12 | int sumNumbers(TreeNode* root) { 13 | return helper(root,0) ; 14 | } 15 | }; -------------------------------------------------------------------------------- /1290 Convert Binary Number in a Linked List to Integer Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getDecimalValue(ListNode* head) { 4 | string s=""; 5 | while(head!=NULL){ 6 | int data=head->val; 7 | char x=(data==1)? '1':'0'; 8 | s+=x; 9 | head=head->next; 10 | } 11 | int data=0; 12 | int base=1; 13 | for(int i=s.length()-1;i>=0;i--){ 14 | if(s[i]=='1') 15 | data+=base; 16 | base=base*2; 17 | } 18 | return data; 19 | } 20 | }; -------------------------------------------------------------------------------- /1295 Find Numbers with Even Number of Digits Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNumbers(vector& nums) { 4 | int count=0; 5 | for(int i=0;i xorQueries(vector& A, vector>& queries) { 4 | vector ans; 5 | for(int i = 1; i < A.size(); i++) 6 | A[i] = A[i]^A[i-1]; 7 | 8 | for(int i = 0; i < queries.size(); i++) { 9 | int start = queries[i][0]; 10 | int end = queries[i][1]; 11 | 12 | if(start == 0) 13 | ans.push_back(A[end]); 14 | else 15 | ans.push_back(A[start-1]^A[end]); 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /1323 Maximum 69 Number Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximum69Number (int num){ 4 | string s=to_string(num); 5 | for(int i=0;i& arr, int k, int threshold) { 4 | int left=0, right=0, sum=0, count=0; 5 | 6 | while(right < arr.size()){ 7 | sum += arr[right] ; 8 | 9 | if(right-left+1 == k){ 10 | int avg = sum/k ; 11 | 12 | if(avg >= threshold){ 13 | count++ ; 14 | } 15 | 16 | sum -= arr[left] ; 17 | left++ ; 18 | } 19 | 20 | right++ ; 21 | } 22 | 23 | return count ; 24 | } 25 | }; -------------------------------------------------------------------------------- /1358 Number of Substrings Containing All Three Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfSubstrings(string s) { 4 | int left=0, right=0, count=0, n= s.size()-1 ; 5 | unordered_map mpp ; 6 | 7 | while(right < s.size()){ 8 | mpp[s[right]]++ ; 9 | 10 | while(mpp['a'] && mpp['b'] && mpp['c']){ 11 | count += 1+ (n - right) ; 12 | 13 | mpp[s[left]]-- ; 14 | left++ ; 15 | } 16 | 17 | right++ ; 18 | } 19 | 20 | return count ; 21 | } 22 | }; -------------------------------------------------------------------------------- /136 Single Number Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | unordered_mapmp; 5 | for(int i=0;i& nums) { 27 | int a=0; 28 | for(int i=0;i& strs) { 4 | int minlen=1000,count=0; 5 | string str; 6 | for(int i=0;imp; 6 | for(auto a:s)mp[a]++; 7 | int no=0; 8 | for(auto x:mp) if(x.second%2==1) no++; 9 | return no<=k; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /1411 Number of Ways to Paint N × 3 Grid Hard.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numOfWays(int n) { 4 | int ans=0; 5 | long c2=6,c3=6; 6 | for(int i =2;i<=n;i++){ 7 | long temp=c2; 8 | c2=(2*c3+3*c2)%1000000007; 9 | c3=(2*temp+2*c3)%1000000007; 10 | } 11 | return (c2+c3)%1000000007; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /1414 Find the Minimum Number of Fibonacci Numbers Whose Sum Is K Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinFibonacciNumbers(int k) { 4 | vectorv{1,1}; 5 | int f=1,s=1; 6 | while(1){ 7 | if(f+s>k) break; 8 | v.push_back(f+s); 9 | f=s; 10 | s=v.back(); 11 | } 12 | int ans=0; 13 | for(int i=v.size()-1;i>=0;i--){ 14 | if(v[i]<=k){ 15 | ans++; 16 | k=k-v[i]; 17 | } 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /142. Linked List Cycle II Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *detectCycle(ListNode *head) { 4 | ListNode* slow=head; 5 | ListNode* fast=head; 6 | 7 | if(!head || !head->next) return NULL; 8 | 9 | while( fast && fast->next){ 10 | slow=slow->next; 11 | fast=fast->next->next; 12 | if(slow==fast) break; 13 | } 14 | if(!fast || !fast->next) return NULL; 15 | while(head!=slow){ 16 | head=head->next; 17 | slow=slow->next; 18 | } 19 | return head; 20 | 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /144 Binary Tree Preorder Traversal Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void preorder(TreeNode* root, vector &ans){ 4 | if(root!=NULL){ 5 | ans.push_back(root->val); 6 | preorder(root->left,ans); 7 | preorder(root->right,ans); 8 | } 9 | } 10 | vector preorderTraversal(TreeNode* root) { 11 | vector ans; 12 | preorder(root,ans); 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /145 Binary Tree Postorder Traversal Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void postorder(TreeNode *root, vector &ans){ 4 | if(root!=NULL){ 5 | postorder(root->left,ans); 6 | postorder(root->right,ans); 7 | ans.push_back(root->val); 8 | } 9 | } 10 | vector postorderTraversal(TreeNode* root) { 11 | vector ans; 12 | postorder(root,ans); 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /1457 Pseudo-Palindromic Paths in a Binary Tree Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int ans = 0; 4 | void trav(TreeNode* root, vector& cur){ 5 | if(!root) return; 6 | cur[root->val]++; 7 | trav(root->left, cur); 8 | trav(root->right, cur); 9 | if(root->left == nullptr && root->right == nullptr){ 10 | int odd=0; 11 | for(int i=1;i<=9;i++){ 12 | if(cur[i]%2!=0) 13 | odd++; 14 | } 15 | if(odd<=1) ans++; 16 | } 17 | cur[root->val]--; 18 | } 19 | int pseudoPalindromicPaths (TreeNode* root) { 20 | vector cur(10, 0); 21 | trav(root, cur); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /1457 Pseudo-Palindromic Paths in a Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | int ans = 0; 5 | void trav(TreeNode* root, vector& cur){ 6 | if(!root) return; 7 | cur[root->val]++; 8 | trav(root->left, cur); 9 | trav(root->right, cur); 10 | if(root->left == nullptr && root->right == nullptr){ 11 | int odd=0; 12 | for(int i=1;i<=9;i++){ 13 | if(cur[i]%2!=0) 14 | odd++; 15 | } 16 | if(odd<=1) ans++; 17 | } 18 | cur[root->val]--; 19 | } 20 | int pseudoPalindromicPaths (TreeNode* root) { 21 | vector cur(10, 0); 22 | trav(root, cur); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /1480 Running Sum of 1d Array Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector runningSum(vector& nums) { 4 | vector ans(nums.size()); 5 | for(int i=0;i& nums) { 4 | int left=0, right=0, zero=0, count=INT_MIN, zeroInd=-1 ; 5 | 6 | while(right < nums.size()){ 7 | if(nums[right] == 0){ 8 | if(zero == 0){ 9 | zero =1 ; 10 | zeroInd =right ; 11 | } 12 | else{ 13 | left =zeroInd+1 ; 14 | zeroInd = right ; 15 | } 16 | } 17 | count = max(count, right-left) ; 18 | right++ ; 19 | } 20 | 21 | return count ; 22 | } 23 | }; -------------------------------------------------------------------------------- /1497 Check If Array Pairs Are Divisible by k Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canArrange(vector& arr, int k) { 4 | unordered_mapmp; 5 | for(auto a:arr) mp[(a%k+k)%k]++; // for negative values the mod wil be positive 6 | if(mp[0]%2==1)return false; 7 | for(int i=1;i& nums, int target) 5 | { 6 | sort(nums.begin(),nums.end()); 7 | int res=0,n=nums.size(),i=0,j=n-1; 8 | vectorpow2(n+1,1); 9 | for(int i=1;i<=n;i++) 10 | { 11 | pow2[i]=(2*pow2[i-1])%mod; 12 | } 13 | while(i<=j) 14 | { 15 | if(nums[j]+nums[i]<=target) 16 | { 17 | res=(res+pow2[j-i])%mod; 18 | i++; 19 | } 20 | else 21 | j--; 22 | } 23 | return res; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /152 Maximum Product Subarray Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | int ma=nums[0]; 5 | int mi=nums[0]; 6 | int ans=nums[0]; 7 | 8 | for(int i=1;i& nums) { 4 | int l = 0; 5 | int r = nums.size() - 1; 6 | 7 | while (l < r) { 8 | const int m = (l + r) / 2; 9 | if (nums[m] < nums[r]) 10 | r = m; 11 | else 12 | l = m + 1; 13 | } 14 | 15 | return nums[l]; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /1539. Kth Missing Positive Number Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthPositive(vector& arr, int k) { 4 | int n = arr.size(); 5 | int start = 0, end = n-1; 6 | while (start <= end) { 7 | int mid = start + (end - start) / 2; 8 | if (arr[mid] - (mid + 1) < k) 9 | start = mid + 1; 10 | else 11 | end = mid - 1; 12 | } 13 | return start + k; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /1544 Make The String Great Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string makeGood(string s) { 4 | if(s.size()==0 || s.size()==1) return s; 5 | string ans=""; 6 | stack st; 7 | for(int i=0;i st1, st2; 4 | MinStack() { 5 | 6 | } 7 | 8 | void push(int val) { 9 | if(st2.empty()|| val<=st2.top()) 10 | st2.push(val); 11 | st1.push(val); 12 | } 13 | 14 | void pop() { 15 | if(st1.top()==st2.top()) 16 | st2.pop(); 17 | 18 | st1.pop(); 19 | } 20 | 21 | int top() { 22 | return st1.top(); 23 | } 24 | 25 | int getMin() { 26 | return st2.top(); 27 | } 28 | }; -------------------------------------------------------------------------------- /1578 Minimum Time to Make Rope Colorful Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCost(string colors, vector& neededTime) { 4 | int ans=0; 5 | for(int i=1;ineededTime[i]) swap(neededTime[i-1],neededTime[i]); 9 | } 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /1588 Sum of All Odd Length Subarrays Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOddLengthSubarrays(vector& arr) { 4 | int n=arr.size(); 5 | int sum=0; 6 | for(int i=0;ismall=small; 6 | this->big=big; 7 | this->medium=medium; 8 | } 9 | 10 | bool addCar(int carType) { 11 | if(carType==1 &&big-- >0) return true; 12 | else if(carType==2 && medium-- >0) return true; 13 | else if(carType==3 && small-- >0) return true; 14 | else return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /1605 Find Valid Matrix Given Row and Column Sums Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> restoreMatrix(vector& rowSum, vector& colSum) { 4 | int n=rowSum.size(); 5 | int m=colSum.size(); 6 | int i=0,j=0; 7 | vector>ans(n,vector(m,0)); 8 | while(i& nums) { 4 | int n=nums.size(); 5 | if(n==1) return 0; 6 | int low=0,high=n-1; 7 | while(lowvis(26,-1); 7 | 8 | for(int i=0;i& scores, vector& ages) { 4 | vector>v; 5 | int n=scores.size(); 6 | for(int i=0;i=0;j--){ 14 | if(v[i][1]<=v[j][1]){ 15 | dp[i]=max(dp[i],dp[j]+v[i][1]); 16 | } 17 | } 18 | ans=max(ans,dp[i]); 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /1636 Sort Array by Increasing Frequency.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector frequencySort(vector& nums) { 4 | unordered_map umap; 5 | for(auto x: nums) 6 | umap[x]++; 7 | 8 | sort(nums.begin(),nums.end(), [&](int a, int b) 9 | {return umap[a]!=umap[b]? umap[a]b;} ); 10 | 11 | return nums; 12 | } 13 | }; -------------------------------------------------------------------------------- /1637 Widest Vertical Area Between Two Points Containing No Points Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxWidthOfVerticalArea(vector>& points) { 4 | vector x; 5 | for(int i=0;i& word1, vector& word2) { 4 | string s1=""; 5 | string s2=""; 6 | for(int i=0;i twoSum(vector& nums, int target) { 4 | int n=nums.size(); 5 | int i=0,j=n-1; 6 | while(i<=j){ 7 | if(nums[i]+nums[j]==target) return {i+1,j+1}; 8 | else if(nums[i]+nums[j]>target) j--; 9 | else i++; 10 | } 11 | return {}; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /1688 Count of Matches in Tournament Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfMatches(int n) { 4 | int ans=0; 5 | while(n/2>0){ 6 | if(n%2==0){ 7 | n=n/2; 8 | ans+=n; 9 | } 10 | else{ 11 | n=n/2; 12 | ans+=n+1; 13 | } 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /169 Majority Element Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | int n=nums.size(); 5 | sort(nums.begin(),nums.end()); 6 | return nums[n/2]; 7 | } 8 | }; 9 | 10 | 11 | 12 | 13 | class Solution { 14 | public: 15 | int majorityElement(vector& nums) { 16 | int v=0,c=0; 17 | for(auto a: nums){ 18 | if(v==0) 19 | c=a; 20 | if(c==a) 21 | v++; 22 | else 23 | v--; 24 | } 25 | return c; 26 | } 27 | }; -------------------------------------------------------------------------------- /1701 Average Waiting Time Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double averageWaitingTime(vector>& customers) { 4 | double wait=0; 5 | int t=0; 6 | for(auto x: customers){ 7 | t=max(t,x[0]); 8 | t+=x[1]; 9 | wait+=(t-x[0]); 10 | } 11 | return wait/customers.size(); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /1702 Maximum Binary String After Change Medium: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string maximumBinaryString(string s) { 4 | int c=0; 5 | int n=s.size(); 6 | string ans(n,'1'); 7 | for(int i=0;i decode(vector& encoded, int first) { 4 | int n=encoded.size(); 5 | vector ans(n+1); 6 | ans[0]=first; 7 | int pre_xor=0; 8 | for(int i=0;i st; 4 | BSTIterator(TreeNode* root) { 5 | while(root) { 6 | st.push(root); 7 | root = root->left; 8 | } 9 | } 10 | 11 | int next() { 12 | TreeNode* t1 = st.top(); 13 | st.pop(); 14 | TreeNode* t = t1->right; 15 | while(t) 16 | { 17 | st.push(t); 18 | t = t->left; 19 | } 20 | return t1->val; 21 | } 22 | 23 | bool hasNext() { 24 | if(st.empty()) 25 | return false; 26 | return true; 27 | 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /1770 Maximum Score from Performing Multiplication Operations Hard.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | int rec(int idx,int st,int end,vector& nums,vector& multi,vector> &dp){ 5 | if(idx==multi.size()) return 0; 6 | if(dp[idx][st]!=INT_MIN) return dp[idx][st]; 7 | int ans=INT_MIN; 8 | ans= max(multi[idx]*nums[st] + rec(idx+1,st+1,end,nums,multi,dp), 9 | multi[idx]*nums[end] + rec(idx+1,st,end-1,nums,multi,dp)); 10 | return dp[idx][st]= ans; 11 | } 12 | int maximumScore(vector& nums, vector& multi) { 13 | vector> dp(multi.size(),vector(multi.size(),INT_MIN)); 14 | return rec(0,0,nums.size()-1,nums,multi,dp); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /20 Valid Parentheses Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | //"{[]}" 4 | bool isValid(string s) { 5 | stack st; 6 | int n=s.size(); 7 | if(s[0]==')' || s[0]=='}'||s[0]==']') 8 | return false; 9 | for(auto c: s){ 10 | if(st.empty()) 11 | st.push(c); 12 | else if(st.top()=='(' && c==')' || 13 | st.top()=='{' && c=='}' || 14 | st.top()=='[' && c==']' 15 | ) 16 | st.pop(); 17 | else 18 | st.push(c); 19 | } 20 | if(!st.empty()) 21 | return false; 22 | 23 | return true; 24 | } 25 | }; -------------------------------------------------------------------------------- /2007 Find Original Array From Doubled Array Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findOriginalArray(vector& changed) { 4 | int n = changed.size(); 5 | if (n % 2 == 1) return {}; 6 | sort(changed.begin(), changed.end()); 7 | vector ans; 8 | map mp; 9 | for (int i = 0; i < n; i++) { 10 | mp[changed[i]]++; 11 | } 12 | for (int i = 0; i < n; i++) { 13 | if (mp[changed[i]] == 0) continue; 14 | if (mp[changed[i] * 2] == 0) return {}; 15 | ans.push_back(changed[i]); 16 | mp[changed[i]]--; 17 | mp[changed[i] * 2]--; 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /202 Happy Number Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int help(int n){ 5 | int ans=0; 6 | while(n>0){ 7 | int rem=n%10; 8 | ans+=rem*rem; 9 | n=n/10; 10 | } 11 | return ans; 12 | } 13 | 14 | bool isHappy(int n) { 15 | int slow=n; 16 | int fast=n; 17 | do{ 18 | slow=help(slow); 19 | fast=help(help(fast)); 20 | }while(slow!=fast); 21 | return slow==1; 22 | } 23 | }; -------------------------------------------------------------------------------- /206 Reverse Linked List Easy.c: -------------------------------------------------------------------------------- 1 | struct ListNode* reverseList(struct ListNode* head){ 2 | struct ListNode *nextnode,*currentnode,*prevnode=NULL; 3 | nextnode=currentnode=head; 4 | while(nextnode!=NULL) 5 | { 6 | nextnode=nextnode->next; 7 | currentnode->next=prevnode; 8 | prevnode=currentnode; 9 | currentnode=nextnode; 10 | } 11 | head=prevnode; 12 | 13 | return head; 14 | } -------------------------------------------------------------------------------- /206 Reverse Linked List Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* reverseList(ListNode* head) { 4 | ListNode *nextnode,*currentnode,*prevnode=NULL; 5 | nextnode=currentnode=head; 6 | while(nextnode!=NULL) 7 | { 8 | nextnode=nextnode->next; 9 | currentnode->next=prevnode; 10 | prevnode=currentnode; 11 | currentnode=nextnode; 12 | } 13 | head=prevnode; 14 | 15 | return head; 16 | } 17 | }; -------------------------------------------------------------------------------- /209 Minimum Size Subarray Sum Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int target, vector& nums) { 4 | int left=0, right=0, n=nums.size() ; 5 | int currSum=0, minLen= INT_MAX ; 6 | bool found =false ; 7 | 8 | while(right < n){ 9 | currSum += nums[right] ; 10 | 11 | while(currSum >= target){ 12 | found =true ; 13 | minLen = min(minLen, right-left+1) ; 14 | currSum -= nums[left] ; 15 | left++ ; 16 | } 17 | 18 | right++ ; 19 | } 20 | 21 | if(!found) 22 | return 0 ; 23 | return minLen ; 24 | } 25 | }; -------------------------------------------------------------------------------- /2095 Delete the Middle Node of a Linked List Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* deleteMiddle(ListNode* head) { 4 | if(head->next==NULL)return NULL; 5 | ListNode*slow=head,*fast=head->next->next; 6 | while(fast&&fast->next){ 7 | slow=slow->next; 8 | fast=fast->next->next; 9 | } 10 | if(slow->next->next) 11 | slow->next=slow->next->next; 12 | else 13 | slow->next=NULL; 14 | return head; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /217 Contains Duplicate Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& nums) { 4 | unordered_set u_set; 5 | 6 | for (int i=0; i& nums, int k) { 4 | unordered_map hashmap; 5 | 6 | for (int i = 0; i < nums.size(); i++) { 7 | if (hashmap.count(nums[i]) > 0 && abs(i - hashmap[nums[i]]) <= k) { 8 | return true; 9 | } 10 | else { 11 | hashmap[nums[i]] = i; 12 | } 13 | } 14 | 15 | return false; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /22. Generate Parentheses Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void solve(int n, vector&ans , int open , int close, string temp){ 4 | if(temp.size()==n*2){ 5 | ans.push_back(temp); 6 | return; 7 | } 8 | 9 | if(open generateParenthesis(int n) { 14 | vector ans; 15 | solve(n, ans, 0 , 0 ,""); 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /222 Count Complete Tree Nodes Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countNodes(TreeNode* root) { 4 | if(!root) return 0 ; 5 | int count =0 ; 6 | 7 | queue q ; 8 | q.push(root) ; 9 | 10 | while(!q.empty()){ 11 | int size = q.size() ; 12 | 13 | for(int i=0; ileft) 19 | q.push(node->left) ; 20 | if(node->right) 21 | q.push(node->right) ; 22 | } 23 | } 24 | 25 | return count ; 26 | } 27 | }; -------------------------------------------------------------------------------- /223 Rectangle Area Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { 4 | int all_area= (ax2-ax1)*(ay2-ay1)+ (bx2-bx1)*(by2-by1); 5 | int x_over = max(min(ax2,bx2) - max(ax1,bx1),0) ; // [3] overlap of sides along x-axis 6 | int y_over = max(min(ay2,by2) - max(ay1,by1),0) ; //[4] overlap of sides along y-axis 7 | int area_0 = x_over * y_over; 8 | return all_area-area_0; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /225 Implement Stack using Queues Easy.cpp: -------------------------------------------------------------------------------- 1 | class MyStack { 2 | public: 3 | queue q1, q2; 4 | MyStack() { 5 | 6 | } 7 | 8 | void push(int x) { 9 | while(!q1.empty()){ 10 | q2.push(q1.front()); 11 | q1.pop(); 12 | } 13 | q1.push(x); 14 | while(!q2.empty()){ 15 | q1.push(q2.front()); 16 | q2.pop(); 17 | } 18 | } 19 | 20 | int pop() { 21 | int x=q1.front(); 22 | q1.pop(); 23 | return x; 24 | } 25 | 26 | int top() { 27 | return q1.front(); 28 | } 29 | 30 | bool empty() { 31 | return q1.empty(); 32 | } 33 | }; -------------------------------------------------------------------------------- /2256 Minimum Average Difference Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumAverageDifference(vector& nums) { 4 | 5 | long long sum=0; 6 | int n=nums.size(); 7 | vectorv(n); 8 | if(n==1) return 0; 9 | for(int i=0;ist; 6 | st.push(root); 7 | while(!st.empty()){ 8 | TreeNode* node=st.top(); 9 | st.pop(); 10 | if(node!=NULL){ 11 | st.push(node->left); 12 | st.push(node->right); 13 | swap(node->left,node->right); 14 | } 15 | } 16 | return root; 17 | } 18 | }; -------------------------------------------------------------------------------- /2306. Naming a Company Hard.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long distinctNames(vector& ideas) { 4 | long long disName = 0; 5 | vector> arr(26); 6 | for (string s : ideas) 7 | arr[s[0] - 'a'].insert(s.substr(1)); 8 | 9 | for (int i = 0; i < 25; i++) { 10 | for (int j = i + 1; j < 26; j++) { 11 | unordered_set set; 12 | set.insert(arr[i].begin(), arr[i].end()); 13 | set.insert(arr[j].begin(), arr[j].end()); 14 | disName += (arr[i].size() - set.size()) * (arr[j].size() - set.size()); 15 | } 16 | } 17 | return disName * 2; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /232 Implement Queue using Stacks Easy.cpp: -------------------------------------------------------------------------------- 1 | class MyQueue { 2 | public: 3 | stackst1 , st2; 4 | MyQueue() { 5 | 6 | } 7 | 8 | void push(int x) { 9 | st1.push(x); 10 | } 11 | 12 | int pop() { 13 | int a= peek(); 14 | st2.pop(); 15 | return a; 16 | } 17 | 18 | int peek() { 19 | if(st2.empty()){ 20 | while(!st1.empty()){ 21 | int x=st1.top(); 22 | st2.push(x); 23 | st1.pop(); 24 | } 25 | } 26 | return st2.top(); 27 | } 28 | 29 | bool empty() { 30 | return (st1.empty() && st2.empty()); 31 | } 32 | }; -------------------------------------------------------------------------------- /235 Lowest Common Ancestor of a Binary Search Tree Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 4 | if(!root) return NULL ; 5 | 6 | if(p->val > root->val && q->val > root->val) 7 | return lowestCommonAncestor(root->right, p, q) ; 8 | else if(p->val < root->val && q->val < root->val) 9 | return lowestCommonAncestor(root->left, p, q) ; 10 | 11 | return root ; 12 | } 13 | }; -------------------------------------------------------------------------------- /237 Delete Node in a Linked List Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void deleteNode(ListNode* node) { 4 | ListNode* temp; 5 | node->val=node->next->val; 6 | node->next=node->next->next; 7 | } 8 | }; -------------------------------------------------------------------------------- /2413 Smallest Even Multiple Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestEvenMultiple(int n) { 4 | if(n%2==0) 5 | return n; 6 | else return n*2; 7 | } 8 | }; -------------------------------------------------------------------------------- /2433 Find The Original Array of Prefix Xor Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findArray(vector& pref) { 4 | int n=pref.size(); 5 | vector ans(n); 6 | ans[0]=pref[0]; 7 | for(int i=1;i findArray(vector& pref) 21 | { 22 | int n = pref.size(); 23 | int pre_xor=0; 24 | 25 | for(int i=1; i0){ 6 | sum+=num%10; 7 | num=num/10; 8 | } 9 | return sum; 10 | } 11 | 12 | int addDigits(int num) { 13 | while(num>9){ 14 | num=add(num); 15 | } 16 | 17 | return num; 18 | } 19 | }; -------------------------------------------------------------------------------- /260 Single Number III Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector& nums) { 4 | int x=nums[0]; 5 | for(int i=1;i& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int m=-1; 6 | int i; 7 | int n=nums.size(); 8 | for( i=0;i=0) 16 | return m; 17 | else return n; 18 | } 19 | }; 20 | 21 | 22 | class Solution { 23 | public: 24 | int missingNumber(vector& nums) { 25 | int n=nums.size(); 26 | int sum=(n*(n+1))/2; 27 | for(int i=0;i dp(n+1,INT_MAX); 5 | dp[0]=0; 6 | int count = 1; 7 | while(count*count <= n) { 8 | int sq = count*count; 9 | for(int i = sq; i < n+1; i++) { 10 | dp[i] = min(dp[i-sq] + 1,dp[i]); 11 | } 12 | count++; 13 | } 14 | return dp[n]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /287. Find the Duplicate Number Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | 5 | int fast = nums[0]; 6 | int slow = nums[0]; 7 | 8 | do{ 9 | slow = nums[slow]; 10 | fast = nums[nums[fast]]; 11 | }while(slow != fast); 12 | 13 | fast = nums[0]; 14 | while(slow != fast) 15 | { 16 | slow = nums[slow]; 17 | fast = nums[fast]; 18 | } 19 | return slow; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /29. Divide Two Integers Medium: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | if(dividend==INT_MIN && divisor==-1) return INT_MAX; 5 | if(dividend==INT_MIN && divisor==1) return INT_MIN; 6 | 7 | long long dd=abs(dividend); 8 | long long dv=abs(divisor); 9 | 10 | int ans=0; 11 | while(dd>=dv){ 12 | int shift = 0; 13 | while(dd >= (dv<0 && divisor>0) || (dividend<0 && divisor<0)) return ans; 18 | return -1*ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /322. Coin Change Medium.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | int coinChange(vector& coins, int amount) { 5 | int m=coins.size()+1; 6 | int n=amount+1; 7 | vector> dp(m,vector(n+1,0)); 8 | for(int i=1;ij) 15 | dp[i][j]=dp[i-1][j]; 16 | else 17 | dp[i][j]=min(dp[i-1][j], 1+dp[i][j-coins[i-1]]); 18 | } 19 | } 20 | return dp[m-1][n-1]>10000?-1: dp[m-1][n-1]; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /326 Power of Three Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool isPowerOfThree(int n) { 5 | 6 | if(n==0){ 7 | return false; 8 | } 9 | 10 | while(n!=1){ 11 | 12 | if(n%3!=0){ 13 | return false; 14 | } 15 | 16 | n=n/3; 17 | 18 | } 19 | 20 | return true; 21 | } 22 | }; -------------------------------------------------------------------------------- /328 Odd Even Linked List Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* oddEvenList(ListNode* head) { 4 | if(!head || !head->next || !head->next->next) return head; 5 | ListNode* odd=head; 6 | ListNode* even=head->next; 7 | ListNode* odd_temp=head; 8 | ListNode* even_temp=head->next; 9 | 10 | while(even && even->next){ 11 | odd->next=even->next; 12 | odd=odd->next; 13 | even->next=odd->next; 14 | even=even->next; 15 | } 16 | odd->next=even_temp; 17 | return head; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /334 Increasing Triplet Subsequence Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool increasingTriplet(vector& nums) { 4 | if(nums.size() < 3){ 5 | return false; 6 | } 7 | int x = INT_MAX, y = INT_MAX; 8 | for(int i : nums) { 9 | if(i <= x) 10 | x = i; 11 | else if(i <= y) 12 | y = i; 13 | else 14 | return true; 15 | } 16 | return false; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /338 Counting Bits Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | vector ans(int (n+1)); 5 | int x,i,count=0; 6 | for(i=0;i<=n;i++) 7 | { 8 | x=i; 9 | count=0; 10 | while(x>0) 11 | { 12 | if(x%2==1) 13 | count++; 14 | x=x/2; 15 | 16 | } 17 | ans[i]=count; 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /344 Reverse String Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | int n=s.size(); 5 | for(int i=0;i topKFrequent(vector& nums, int k) { 4 | priority_queue< pair, vector> , greater>> pq; 5 | unordered_map mp; 6 | for(auto x: nums) 7 | mp[x]++; 8 | 9 | for(auto x: mp){ 10 | pq.push(make_pair(x.second,x.first)); 11 | if(pq.size()>k) 12 | pq.pop(); 13 | } 14 | vector ans; 15 | while(!pq.empty()){ 16 | pair temp=pq.top(); 17 | pq.pop(); 18 | ans.push_back(temp.second); 19 | } 20 | 21 | 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /380 Insert Delete GetRandom O(1) Medium.cpp: -------------------------------------------------------------------------------- 1 | class RandomizedSet { 2 | unordered_map vals; 3 | vector idxs; 4 | 5 | public: 6 | RandomizedSet() {} 7 | bool insert(int val) { 8 | if (vals.count(val)) return false; 9 | vals[val] = idxs.size(); 10 | idxs.push_back(val); 11 | return true; 12 | } 13 | bool remove(int val){ 14 | if (!vals.count(val)) return false; 15 | int lst = idxs.back(); 16 | int pos = vals[val]; 17 | vals[lst] = pos; 18 | idxs[pos] = lst; 19 | vals.erase(val); 20 | idxs.pop_back(); 21 | return true; 22 | } 23 | int getRandom() { 24 | return idxs[rand() % idxs.size()]; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /392. Is Subsequence Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int i=0,j=0; 5 | while(ileft && !root->right) ? true : false ; 6 | } 7 | 8 | int sumOfLeftLeaves(TreeNode* root) { 9 | if(!root) return 0 ; 10 | 11 | if(root->left && isLeaf(root->left)) leftSum += root->left->val ; 12 | 13 | int left = sumOfLeftLeaves(root->left) ; 14 | int right = sumOfLeftLeaves(root->right) ; 15 | 16 | 17 | return leftSum ; 18 | } 19 | }; -------------------------------------------------------------------------------- /409. Longest Palindrome Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) { 4 | 5 | unordered_mapans; 6 | for(int i=0;i1){ 16 | return s.length()-count+1; 17 | } 18 | return s.length(); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /413. Arithmetic Slices Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfArithmeticSlices(vector& nums) { 4 | int n=nums.size(); 5 | if(n<3) return 0; 6 | int ans=0,x=0; 7 | for(int i=1;i mp; 6 | int ans=0, j=0, mx=0; 7 | for(int i=0;i k) 12 | { 13 | mp[s[j]]--; 14 | j++; 15 | } 16 | ans=max(ans, i-j+1); 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /443. String Compression Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int compress(vector& chars) { 4 | int i=0,j=0; 5 | int n=chars.size(); 6 | if(n==1) return 1; 7 | while(i1){ 15 | string s=to_string(count); 16 | for(auto c: s) 17 | chars[j++]=c; 18 | } 19 | } 20 | return j; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /446 Arithmetic Slices II - Subsequence Hard.cpp: -------------------------------------------------------------------------------- 1 | //from prinzeop3 2 | class Solution { 3 | public: 4 | int numberOfArithmeticSlices(vector& a) { 5 | long long ans = 0, n = a.size(); 6 | vector> dp(n); 7 | for (int i = 0; i < n; ++i) { 8 | for (int j = 0; j < i; ++j) { 9 | long long diff = (long long)a[i] - (long long)a[j]; 10 | 11 | dp[i][diff]++; 12 | if (dp[j].count(diff)) { 13 | dp[i][diff] += dp[j][diff]; 14 | ans += dp[j][diff]; 15 | } 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /45. Jump Game II Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | int n=nums.size(); 5 | for(int i=1;i freq; 5 | vector bucket(s.size()+1, ""); 6 | string res; 7 | 8 | 9 | for(char c:s) freq[c]++; 10 | //put character into frequency bucket 11 | for(auto& it:freq) { 12 | int n = it.second; 13 | char c = it.first; 14 | bucket[n].append(n, c); 15 | } 16 | //form descending sorted string 17 | for(int i=s.size(); i>0; i--) { 18 | if(!bucket[i].empty()) 19 | res.append(bucket[i]); 20 | } 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /48 Rotate Image Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void rotate(vector> &matrix) 5 | { 6 | for (int i = 0; i < matrix.size(); i++) 7 | { 8 | for (int j = 0; j < i; j++) 9 | { 10 | swap(matrix[i][j], matrix[j][i]); 11 | } 12 | } 13 | 14 | for (int i = 0; i < matrix.size(); i++) 15 | { 16 | reverse(matrix[i].begin(), matrix[i].end()); 17 | } 18 | } 19 | }; -------------------------------------------------------------------------------- /485 Max Consecutive Ones Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector& nums) { 4 | int n=nums.size(); 5 | int count=0; 6 | int ans=0; 7 | for(int i=0;i> groupAnagrams(vector& strs) { 4 | vector> ans; 5 | unordered_map> umap; 6 | 7 | for(auto x: strs){ 8 | string temp =x; 9 | sort(x.begin(),x.end()); 10 | umap[x].push_back(temp); 11 | } 12 | for(auto x: umap){ 13 | ans.push_back(x.second); 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /496 Next Greater Element I Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector nextGreaterElement(vector& nums1, vector& nums2) { 4 | vectorans; 5 | int n=nums2.size(); 6 | unordered_mapmp; 7 | stack st; 8 | for(int i=n-1;i>=0;i--){ 9 | while(!st.empty() && st.top()<=nums2[i]) 10 | st.pop(); 11 | 12 | int res=st.empty()? -1: st.top(); 13 | mp.insert({nums2[i], res}); 14 | st.push(nums2[i]); 15 | } 16 | 17 | for(auto x: nums1) 18 | ans.push_back(mp[x]); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /509. Fibonacci Number Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | if(n == 0) return 0; 5 | if(n==1) return 1; 6 | 7 | int ans = fib(n-1) + fib(n-2); 8 | return ans; 9 | } 10 | }; 11 | 12 | 13 | 14 | // class Solution { 15 | // public: 16 | // int fib(int n) { 17 | // vectorfib(n+1); 18 | // if(n==0) return 0; 19 | // if(n==1 || n==2) return 1; 20 | // fib[1]=1; 21 | // fib[2]=1; 22 | // for(int i=3;i<=n;i++){ 23 | // fib[i]=fib[i-1]+fib[i-2]; 24 | // } 25 | // return fib[n]; 26 | // } 27 | // }; 28 | -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector& nums) { 4 | int x=nums[0]; 5 | for(int i=1;i& nums) { 14 | int n=nums.size(),l=0,r=n-1,m; 15 | while(l& nums, int k) { 4 | int n=nums.size(); 5 | unordered_mapmp; 6 | int sum=0; 7 | int count=0; 8 | 9 | for(int i=0;i cnt(26, 0); 5 | for(auto x: s1){ 6 | cnt[x - 'a']++; 7 | } 8 | 9 | int n1 = s1.size(), n2 = s2.size(); 10 | 11 | for(int i = 0; i < n2 - n1 + 1; i++){ 12 | vector cnt2(26, 0); 13 | for(int j = i; j < i + n1; j++){ 14 | cnt2[s2[j] - 'a']++; 15 | } 16 | if(cnt == cnt2) return true; 17 | } 18 | return false; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /6 Zigzag Conversion Medium.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String convert(String s, int numRows) { 3 | StringBuffer[] sb= new StringBuffer[numRows]; 4 | for(int i=0;i0 && i numbers; 6 | for(int i=1;i& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int n=nums.size(); 6 | int ans=0; 7 | for(int i =n-1; i>1; i--){ 8 | int j=0, k=i-1; 9 | while(jval += root2->val ; 9 | root1->left = mergeTrees(root1->left , root2->left) ; 10 | root1->right = mergeTrees(root1->right ,root2->right) ; 11 | 12 | return root1 ; 13 | } 14 | }; -------------------------------------------------------------------------------- /62. Unique Paths Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | vector> grid(m, vector(n, 0)); 5 | 6 | for (int i = 0; i < m; i++) { 7 | grid[i][0] = 1; 8 | } 9 | for (int j = 0; j < n; j++) { 10 | grid[0][j] = 1; 11 | } 12 | for (int i = 1; i < m; i++) { 13 | for (int j = 1; j < n; j++) { 14 | grid[i][j] = grid[i - 1][j] + grid[i][j - 1]; 15 | } 16 | } 17 | 18 | return grid[m - 1][n - 1]; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /637 Average of Levels in Binary Tree Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector averageOfLevels(TreeNode* root) { 4 | queueq; 5 | vectorans; 6 | q.push(root); 7 | while(!q.empty()) 8 | { 9 | int n=q.size(); 10 | double sum=0; 11 | for(int i=0;ileft) q.push(node->left); 14 | if(node->right) q.push(node->right); 15 | sum+=node->val; 16 | q.pop(); 17 | 18 | } 19 | ans.push_back(sum/n); 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /658 Find K Closest Elements Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findClosestElements(vector& arr, int k, int x) { 4 | vector ans; 5 | priority_queue>pq; 6 | for(auto ele: arr){ 7 | pq.push(make_pair(abs(x-ele), ele)); 8 | if(pq.size()>k) 9 | pq.pop(); 10 | } 11 | while(!pq.empty()){ 12 | ans.push_back(pq.top().second); 13 | pq.pop(); 14 | } 15 | sort(ans.begin(),ans.end()); 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /7 Reverse Integer Medium.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) 3 | { 4 | long num=0; 5 | while(x!=0){ 6 | num=num*10+ x%10; 7 | x=x/10; 8 | } 9 | if(num>Integer.MAX_VALUE || numval < val) 7 | return searchBST(root->right, val) ; 8 | else if(root->val > val) 9 | return searchBST(root->left, val) ; 10 | 11 | return root ; 12 | } 13 | }; -------------------------------------------------------------------------------- /704 Binary Search Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int n=nums.size(); 5 | int low=0; 6 | int high=n-1; 7 | 8 | while(low<=high){ 9 | int mid=(high+low)/2; 10 | if(nums[mid]==target) 11 | return mid; 12 | else if(target>nums[mid]) 13 | low=mid+1; 14 | else 15 | high=mid-1; 16 | } 17 | return -1; 18 | } 19 | }; -------------------------------------------------------------------------------- /713. Subarray Product Less Than K Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarrayProductLessThanK(vector& nums, int k) { 4 | int n=nums.size(); 5 | long long product=1; 6 | int count=0,j=0; 7 | for(int i=0;i=k){ 10 | product/=nums[j]; 11 | j++; 12 | } 13 | count+=i-j+1; 14 | } 15 | return count; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /718 Maximum Length of Repeated Subarray Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLength(vector& nums1, vector& nums2) { 4 | 5 | int m = nums1.size(), n = nums2.size(); 6 | vector> dp(m + 1, vector(n + 1, 0)); 7 | int ans = 0; 8 | for(int i = 1; i <= m; i++){ 9 | for(int j = 1; j <= n; j++){ 10 | if(nums1[i-1] == nums2[j-1]){ 11 | dp[i][j] = 1 + dp[i-1][j-1]; 12 | ans = max(ans, dp[i][j]); 13 | } 14 | } 15 | } 16 | return ans; 17 | 18 | } 19 | }; -------------------------------------------------------------------------------- /724. Find Pivot Index Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int pivotIndex(vector& nums) { 4 | int sum = 0, msum = 0; 5 | for(auto i: nums){ 6 | sum += i; 7 | } 8 | 9 | int len = nums.size(); 10 | for(int i = 0; i < len; i++){ 11 | sum -= nums[i]; 12 | if(sum == msum){ 13 | return i; 14 | } 15 | msum += nums[i]; 16 | } 17 | return -1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /732. My Calendar III Hard.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendarThree { 2 | public: 3 | map mp; 4 | MyCalendarThree() { 5 | 6 | } 7 | 8 | int book(int start, int end) { 9 | mp[start]++; 10 | mp[end]--; 11 | int sum=0; 12 | int curr_sum=0; 13 | for(auto it : mp){ 14 | curr_sum+= it.second; 15 | sum=max(sum,curr_sum); 16 | } 17 | return sum; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /74. Search a 2D Matrix Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | int row=matrix.size(); 5 | int col=matrix[0].size(); 6 | 7 | int i=0, j=col-1; 8 | while(i>=0 && i=0 && j target){ 14 | j--; 15 | } 16 | } 17 | return false; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /746. Min Cost Climbing Stairs Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | int n=cost.size(); 5 | for(int i=2;i>& matrix) { 4 | int n=matrix.size(); 5 | int m=matrix[0].size(); 6 | for(int i=1;i>ans; 4 | void backtrack(int n,int start,vector&temp,int k){ 5 | if(temp.size()>k)return; 6 | if(temp.size()==k)ans.push_back(temp); 7 | 8 | for(int i=start;i<=n;i++){ 9 | temp.push_back(i); 10 | backtrack(n,i+1,temp,k); 11 | temp.pop_back(); 12 | } 13 | } 14 | vector> combine(int n, int k) { 15 | vectortemp; 16 | backtrack(n,1,temp,k); 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /784. Letter Case Permutation Medium.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | vectorans; 5 | void help(int i, string temp, string s){ 6 | if(i==s.size()){ 7 | ans.push_back(temp); 8 | return; 9 | } 10 | if(s[i]>='0' && s[i]<='9'){ 11 | temp+=s[i]; 12 | help(i+1,temp,s); 13 | } 14 | else{ 15 | temp+=toupper(s[i]); 16 | help(i+1,temp,s); 17 | temp.pop_back(); 18 | temp+=tolower(s[i]); 19 | help(i+1,temp,s); 20 | } 21 | 22 | } 23 | vector letterCasePermutation(string s) { 24 | string temp=""; 25 | help(0,temp,s); 26 | return ans; 27 | } 28 | }; 29 | 30 | -------------------------------------------------------------------------------- /791 Custom Sort String Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string customSortString(string order, string s) { 4 | mapmp; 5 | string ans=""; 6 | for(auto x: s) 7 | mp[x]++; 8 | 9 | for(auto it: order){ 10 | if(mp.find(it)!=mp.end()){ 11 | auto temp=mp.find(it); 12 | int count= temp->second; 13 | 14 | string s(count,it); 15 | ans+=s; 16 | 17 | mp.erase(it); 18 | } 19 | } 20 | for(auto x: mp){ 21 | string s(x.second,x.first); 22 | ans+=s; 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /8 String to Integer (atoi) Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int myAtoi(string s) { 4 | int n = s.length(); 5 | int c = 0; 6 | for (int i = 0; i < n; i++) 7 | { 8 | if (s[i] == ' ') 9 | c++; 10 | } 11 | if (c == n) 12 | return 0; 13 | else{ 14 | stringstream s1(s); 15 | int x; 16 | s1>>x; 17 | 18 | return x; 19 | } 20 | } 21 | }; -------------------------------------------------------------------------------- /814 Binary Tree Pruning Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode* pruneTree(TreeNode* root) { 4 | if(!root) return NULL; 5 | 6 | TreeNode *left=pruneTree(root->left); 7 | TreeNode *right=pruneTree(root->right); 8 | 9 | if(root->val==0 and !left and !right) return NULL; 10 | 11 | root->left=left; 12 | root->right=right; 13 | return root; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /83 Remove Duplicates from Sorted List Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* deleteDuplicates(ListNode* head) { 4 | ListNode* temp=head; 5 | if(!temp) return NULL; 6 | while(temp->next!=NULL){ 7 | if(temp->val==temp->next->val){ 8 | temp->next =temp->next->next; 9 | } 10 | else 11 | temp=temp->next; 12 | } 13 | return head; 14 | } 15 | }; -------------------------------------------------------------------------------- /845. Longest Mountain in Array Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestMountain(vector& nums) { 4 | int n=nums.size(); 5 | if(n<3) return 0; 6 | int ans=0; 7 | int i=1; 8 | while(inums[i]){ 18 | i++; 19 | lower++; 20 | } 21 | 22 | if(upper && lower) 23 | ans=max(ans,upper+lower+1); 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /872 Leaf-Similar Trees Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void traversal(TreeNode* root, vector &r){ 4 | if(!root) return ; 5 | traversal(root->left,r); 6 | if(!root->left && !root->right) r.push_back(root->val); 7 | traversal(root->right,r); 8 | 9 | } 10 | bool leafSimilar(TreeNode* root1, TreeNode* root2) { 11 | vector r1; 12 | vector r2; 13 | traversal(root1,r1); 14 | traversal(root2,r2); 15 | for(int i=0;inext!=NULL){ 7 | slow=slow->next; 8 | fast=fast->next->next; 9 | } 10 | return slow; 11 | } 12 | }; 13 | 14 | 15 | 16 | class Solution { 17 | public: 18 | ListNode* middleNode(ListNode* head) { 19 | vector ans={head}; 20 | while(ans.back()->next!=NULL){ 21 | ans.push_back(ans.back()->next); 22 | } 23 | return ans[ans.size()/2]; 24 | } 25 | }; -------------------------------------------------------------------------------- /899 Orderly Queue Hard.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string orderlyQueue(string s, int k) 5 | { 6 | if (k > 1) { sort(s.begin(), s.end()); return s; } 7 | string ss = s; 8 | for (int i = 1; i < s.length(); ++i) 9 | ss = min(ss, s.substr(i) + s.substr(0,i)); 10 | return ss; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /9 Palindrome Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | 5 | if(x<0 ||(x%10==0 && x!=0)) return false; 6 | 7 | int num=0; 8 | while(x>num) 9 | { 10 | num=num*10 + x%10; 11 | x=x/10; 12 | } 13 | return x==num|| x==num/10; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /901 Online Stock Span Medium.cpp: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | public: 3 | stack>st; 4 | StockSpanner() { 5 | 6 | } 7 | 8 | int next(int price) { 9 | int span=1; 10 | while(!st.empty() && st.top().first<=price){ 11 | span+=st.top().second++; 12 | st.pop(); 13 | } 14 | st.push({price,span}); 15 | return span; 16 | } 17 | }; -------------------------------------------------------------------------------- /904. Fruit Into Baskets Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int totalFruit(vector& fruits) { 4 | unordered_map count; 5 | int i, j; 6 | for (i = 0, j = 0; j < fruits.size(); ++j) { 7 | count[fruits[j]]++; 8 | if (count.size() > 2) { 9 | if (--count[fruits[i]] == 0)count.erase(fruits[i]); 10 | i++; 11 | } 12 | } 13 | return j - i; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /905 Sort Array By Parity Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector& nums) { 4 | int i=0 ,c=0, r=nums.size(); 5 | int temp; 6 | for(i=0;i& nums, int goal) { 4 | 5 | unordered_map freqMap ; 6 | int sum=0, count=0 ; 7 | 8 | for(int i=0; ival >= low && root->val <= high){ 9 | return root->val + rangeSumBST(root->left ,low ,high) +rangeSumBST(root->right ,low ,high) ; 10 | } 11 | 12 | else if(root->val > low){ 13 | return rangeSumBST(root->left ,low ,high) ; 14 | } 15 | else{ 16 | return rangeSumBST(root->right ,low ,high) ; 17 | } 18 | } 19 | 20 | }; 21 | -------------------------------------------------------------------------------- /948 Bag of Tokens Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bagOfTokensScore(vector& tokens, int power) { 4 | sort(tokens.begin(), tokens.end()); 5 | int res = 0, points = 0, i = 0, j = tokens.size() - 1; 6 | while (i <= j) { 7 | if (power >= tokens[i]) { 8 | power -= tokens[i++]; 9 | res = max(res, ++points); 10 | } else if (points > 0) { 11 | points--; 12 | power += tokens[j--]; 13 | } else { 14 | break; 15 | } 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /974 Subarray Sums Divisible by K Medium.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraysDivByK(vector& nums, int k) { 4 | int n=nums.size(); 5 | unordered_mapmp; 6 | int ans=0; 7 | int sum=0,rem=0; 8 | for(int i=0;i& nums) { 4 | sort(nums.begin(),nums.end()); 5 | for(int i=nums.size()-1;i>=2;i--) 6 | if(nums[i] sortedSquares(vector& arr) { 4 | int i,j,temp; 5 | int x=arr.size(); 6 | for(i=0;ival >= maxVal || root->val <= minVal) 8 | return false ; 9 | 10 | return solve(root->left, minVal, root->val) && solve(root->right, root->val, maxVal) ; 11 | } 12 | 13 | bool isValidBST(TreeNode* root) { 14 | return solve(root, LONG_MIN, LONG_MAX) ; 15 | } 16 | }; -------------------------------------------------------------------------------- /981 Time Based Key-Value Store Medium.cpp: -------------------------------------------------------------------------------- 1 | class TimeMap { 2 | public: 3 | unordered_map>> mp; 4 | TimeMap() { 5 | mp.clear(); 6 | } 7 | 8 | void set(string key, string value, int timestamp) { 9 | mp[key].insert({timestamp, value}); 10 | } 11 | 12 | string get(string key, int timestamp) { 13 | if(mp[key].size() != 0){ 14 | auto it = mp[key].upper_bound({timestamp, "~"}); 15 | if(it == mp[key].begin()) return ""; 16 | --it; 17 | return it->second; 18 | } 19 | return ""; 20 | } 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /997 Find the Town Judge Easy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findJudge(int n, vector>& trust) { 4 | vector>ans(n+1,{0,0}); 5 | for(int i=0;i ans; 5 | void dfs(struct Node *root, int target, vector &arr){ 6 | if(!root) return; 7 | if(root->data==target){ 8 | ans=arr; 9 | return; 10 | } 11 | arr.push_back(root->data); 12 | dfs(root->left,target,arr); 13 | dfs(root->right,target,arr); 14 | arr.pop_back(); 15 | } 16 | vector Ancestors(struct Node *root, int target) { 17 | vector arr; 18 | dfs(root,target,arr); 19 | reverse(ans.begin(),ans.end()); 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Anti Diagonal Traversal of Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector antiDiagonalPattern(vector> matrix) 4 | { 5 | int n=matrix.size(); 6 | int m=matrix[0].size(); 7 | vectorans; 8 | int col=0,row=0; 9 | while(col=0 && j &arr) { 4 | int ans = 0; 5 | int zerocnt = 0; 6 | for(int i = 0; i < n; ++i){ 7 | if(arr[i] == 0){ 8 | zerocnt++; 9 | if(i > 0 && arr[i-1] != 0) ans++; 10 | } 11 | } 12 | if(arr[n-1] != 0) ans++; 13 | 14 | if(zerocnt == n) return 0; 15 | if(zerocnt == 0) return -1; 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Array Pair Sum Divisibility Problem.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canPair(vector nums, int k) { 4 | int n = nums.size(); 5 | if(n % 2) return false; 6 | vector mp(k + 1, 0); 7 | for(auto x : nums){ 8 | x %= k; 9 | mp[x]++; 10 | mp[k-x]--; 11 | } 12 | for(int i = 1; i < k; i++) if(mp[i]>0) return false; 13 | return true; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Array to BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | Node *buildBSTFromInorder(vector &inorder, int start, int end){ 5 | if (start > end) 6 | { 7 | return nullptr; 8 | } 9 | 10 | int mid = (start + end) / 2; 11 | Node *node = new Node(inorder[mid]); 12 | 13 | node->left = buildBSTFromInorder(inorder, start, mid - 1); 14 | node->right = buildBSTFromInorder(inorder, mid + 1, end); 15 | 16 | return node; 17 | } 18 | 19 | Node* sortedArrayToBST(vector& nums) { 20 | Node *root = buildBSTFromInorder(nums, 0, nums.size() - 1); 21 | return root; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /BFS of graph GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // Function to return Breadth First Traversal of given graph. 4 | vector bfsOfGraph(int V, vector adj[]) { 5 | vector ans; 6 | queue q; 7 | vector vis(V,0); 8 | q.push(0); 9 | vis[0]=1; 10 | while(!q.empty()){ 11 | int front=q.front(); 12 | q.pop(); 13 | ans.push_back(front); 14 | for(auto it: adj[front]){ 15 | if(!vis[it]){ 16 | q.push(it); 17 | vis[it]=1; 18 | } 19 | } 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Binary matrix having maximum number of 1s Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Bit Magic Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bitMagic(int n, vector &arr) { 4 | int count=0; 5 | int i=0, j=n-1; 6 | while(i boundaryTraversal(vector > matrix, int n, int m) 7 | { 8 | vector ans; 9 | int i; 10 | for(i=0;i=0;i--) ans.push_back(matrix[n-1][i]); 14 | if(m==1) return ans; 15 | for(i=n-2;i>0;i--) ans.push_back(matrix[i][0]); 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Brothers From Different Roots.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void inorder(Node* root, unordered_set& b1){ 5 | if(!root) return; 6 | inorder(root->left,b1); 7 | b1.insert(root->data); 8 | inorder(root->right,b1); 9 | } 10 | int countPairs(Node* root1, Node* root2, int x) 11 | { 12 | unordered_set b1; 13 | unordered_set b2; 14 | inorder(root1,b1); 15 | inorder(root2,b2); 16 | int ans=0; 17 | for(auto it: b1){ 18 | if(b2.find(x-it)!=b2.end()) ans++; 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Bus Conductor Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMoves(int n, vector c, vector p) { 4 | sort(c.begin(),c.end()); 5 | sort(p.begin(),p.end()); 6 | 7 | int ans=0; 8 | for(int i=0;i> v; 5 | for (int i = 0; i < n; ++i) 6 | v.push_back({price[i], i + 1}); 7 | 8 | sort(v.begin(), v.end()); 9 | 10 | int ans = 0; 11 | 12 | for (auto it: v) { 13 | int maxBuy = min(k / it.first, it.second); 14 | ans += maxBuy; 15 | k -= it.first * maxBuy; 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Candy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCandy(int N, vector &ratings) { 4 | if(N==0) return 0; 5 | vector left(N,0); 6 | vector right(N,0); 7 | left[0]=1; 8 | right[N-1]=1; 9 | for(int i=1;iratings[i-1]) left[i]=left[i-1]+1; 11 | else left[i]=1; 12 | } 13 | for(int i=N-2;i>=0;i--){ 14 | if(ratings[i]>ratings[i+1]) right[i]=right[i+1]+1; 15 | else right[i]=1; 16 | } 17 | 18 | int ans=0; 19 | for(int i=0;i &arr){ 5 | if(!root) return; 6 | inorder(root->left,arr); 7 | arr.push_back(root->data); 8 | inorder(root->right,arr); 9 | } 10 | bool isBST(Node* root) { 11 | 12 | vector arr; 13 | inorder(root,arr); 14 | int n=arr.size(); 15 | if(n==1 or n==0) return true; 16 | for(int i=1;i=arr[i]) return false; 18 | } 19 | return true; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Check if a string is repetition of its substring of k-length.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int kSubstrConcat (int n, string s, int k) 4 | { 5 | if(n%k) return 0; 6 | 7 | mapmpp; 8 | string temp=""; 9 | temp+=s[0]; 10 | 11 | for(int i=1;i2) 21 | return 0; 22 | return 1; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Check if string is rotated by two places.cpp: -------------------------------------------------------------------------------- 1 | int n=s1.length(); 2 | if(n==2) return s1==s2; 3 | for(int i=0;i>1; 9 | } 10 | return n&1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Chocolate Distribution Problem GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | long long findMinDiff(vector a, long long n, long long m){ 4 | long long ans=INT_MAX; 5 | sort(a.begin(),a.end()); 6 | int i=0,j=m-1; 7 | while(j0){ 8 | int rem = n % 26; 9 | ans = s[rem] + ans; 10 | if (rem == 0) rem = 26; 11 | n -= rem; 12 | n /= 26; 13 | } 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Consecutive 1's not allowed.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // #define ll long long 4 | int mod=1e9+7; 5 | ll countStrings(int n) { 6 | ll f=1; 7 | ll s=2; 8 | 9 | for(int i=2;i<=n;i++){ 10 | ll t=(f+s)%mod; 11 | f=s; 12 | s=t; 13 | } 14 | return s; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Count More than n by k Occurences.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to find all elements in array that appear more than n/k times. 5 | int countOccurence(int arr[], int n, int k) { 6 | int ans=0; 7 | unordered_map mp; 8 | for(int i=0;i n/k) ans++; 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Count digit groupings of a number.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int help(int ind , int sum , string &s , vector> &dp){ 4 | if(ind==s.length()) 5 | return 1; 6 | if(dp[ind][sum]!=-1) 7 | return dp[ind][sum]; 8 | int x = 0 ,ans = 0; 9 | for(int i = ind ; i= sum){ 12 | ans += help(i+1 , x , s,dp); 13 | } 14 | } 15 | return dp[ind][sum] = ans; 16 | } 17 | int TotalCount(string str){ 18 | int n = str.length() ; 19 | vector> dp(n , vector(901 , -1)); 20 | return help(0 , 0 , str , dp); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Count number of hops.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long countWays(int n) 5 | { 6 | int mod= 1e9+7; 7 | if(n==0 || n==1) return 1; 8 | if(n==2) return 2; 9 | long long ans=0; 10 | long long one=1; 11 | long long two=1; 12 | long long three=2; 13 | for(int i=3;i<=n;i++){ 14 | ans= (one+two+three)%mod; 15 | one=two; 16 | two=three; 17 | three=ans; 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Count number of substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long int upto(string s, int k){ 5 | int l=0,r=0,cnt=0; 6 | long long int ans=0; 7 | vector mp(26,0); 8 | while(rk){ 12 | mp[s[l]-'a']--; 13 | if(mp[s[l]-'a']<=0){ 14 | cnt--; 15 | } 16 | l++; 17 | } 18 | ans+=(r-l+1); 19 | r++; 20 | } 21 | return ans; 22 | 23 | } 24 | long long int substrCount (string s, int k) { 25 | return upto(s,k)-upto(s,k-1); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Count ways to N'th Stair(Order does not matter).cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int nthStair(int n){ 4 | return 1+n/2; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /DFS of Graph GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void dfs(int node, vector adj[], vector& ans, vector&vis){ 5 | vis[node]=1; 6 | ans.push_back(node); 7 | for(auto it: adj[node]){ 8 | if(vis[it]==-1){ 9 | dfs(it,adj,ans,vis); 10 | } 11 | } 12 | } 13 | 14 | vector dfsOfGraph(int V, vector adj[]) { 15 | vector ans; 16 | vector vis(V,-1); 17 | for(auto it: adj){ 18 | if(vis[it]==-1){ 19 | dfs(it,adj,ans,vis); 20 | } 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Delete Middle Element Of a stack GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void solve(stack& s, int n, int ind) { 5 | if (ind == n/2) 6 | { 7 | s.pop(); 8 | return; 9 | } 10 | 11 | int x = s.top(); 12 | s.pop(); 13 | ind++; 14 | 15 | solve(s, n, ind); 16 | 17 | s.push(x); 18 | } 19 | void deleteMid(stack&s, int sizeOfStack) 20 | { 21 | solve(s,sizeOfStack,0); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Delete a Node in Single Linked List.cpp: -------------------------------------------------------------------------------- 1 | Node* deleteNode(Node *head,int x) 2 | { 3 | if(x==1) return head->next; 4 | Node* temp=head; 5 | while(x>2){ 6 | temp=temp->next; 7 | x--; 8 | } 9 | temp->next=temp->next->next; 10 | return head; 11 | } 12 | -------------------------------------------------------------------------------- /Delete node in Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | Node* deleteNode(Node* head, int x) { 4 | if(x==1) return head->next; 5 | Node* temp=head; 6 | Node* pre=NULL; 7 | while(temp && x>0){ 8 | if(x==1){ 9 | pre->next=temp->next; 10 | temp=temp->next; 11 | if(temp) temp->prev=pre; 12 | break; 13 | } 14 | pre=temp; 15 | temp=temp->next; 16 | x--; 17 | } 18 | return head; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Delete nodes having greater value on right.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | Node *compute(Node *head) 5 | { 6 | if(head->next == NULL) return head; 7 | 8 | Node * next = compute(head->next); 9 | 10 | if(next->data <= head->data){ 11 | head->next = next; 12 | return head; 13 | } 14 | return next; 15 | } 16 | 17 | }; 18 | -------------------------------------------------------------------------------- /Determine if Two Trees are Identical.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isIdentical(Node *r1, Node *r2) 5 | { 6 | if(!r1 and !r2 ) return true; 7 | if(!r1 || !r2) return false; 8 | if(r1->data != r2->data) return false; 9 | return isIdentical(r1->left,r2->left) && isIdentical(r1->right,r2->right); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Distribute candies in a binary tree.cpp: -------------------------------------------------------------------------------- 1 | static int moves; 2 | static int solve(Node root) 3 | { 4 | if(root == null) 5 | return 0; 6 | 7 | int ln = solve(root.left); 8 | int rn = solve(root.right); 9 | 10 | moves += Math.abs(ln) + Math.abs(rn); 11 | return root.data - 1 + ln + rn; 12 | } 13 | 14 | public static int distributeCandy(Node root) 15 | { 16 | moves = 0; 17 | solve(root); 18 | return moves; 19 | } 20 | -------------------------------------------------------------------------------- /Duplicate Subtrees.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | string solve(Node* root,unordered_map &mp,vector &ans){ 5 | if (!root) return "N"; 6 | string s = to_string(root->data) + "," + solve(root->left,mp,ans) + ","+solve(root->right,mp,ans); 7 | mp[s]++; 8 | if (mp[s] == 2) { 9 | ans.push_back(root); 10 | } 11 | return s; 12 | } 13 | vector printAllDups(Node* root) 14 | { 15 | unordered_mapmp; 16 | vector ans; 17 | solve(root,mp, ans); 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Equilibrium Point.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int equilibriumPoint(long long a[], int n) { 4 | int sum=0; 5 | if(n==1) return 1; 6 | for(int i=0;iadj[]){ 4 | int odd = 0; 5 | for (int i = 0; i < V; ++i) { 6 | if (adj[i].size() % 2 != 0){ 7 | odd += 1; 8 | } 9 | } 10 | if (odd > 2) { 11 | return 0; 12 | } 13 | return (odd == 0) ? 2 : 1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Find Kth permutation Medium GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string kthPermutation(int n, int k) 5 | { 6 | int fact=1; 7 | vector nums; 8 | for(int i=1;i=0){ 16 | ans=ans+ to_string(nums[k/fact]); 17 | nums.erase(nums.begin() +k/fact); 18 | if(nums.size()==0) break; 19 | k=k%fact; 20 | fact=fact/nums.size(); 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Find Transition Point.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | 5 | int transitionPoint(int arr[], int n) { 6 | int ans=-1; 7 | int l=0,r=n-1; 8 | while(l<=r){ 9 | int mid=(l+r)/2; 10 | if(arr[mid]==1){ 11 | ans=mid; 12 | r=mid-1; 13 | } 14 | else l=mid+1; 15 | } 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Find Triplets with zero Sum GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | 4 | bool findTriplets(int arr[], int n) 5 | { 6 | sort(arr,arr+n); 7 | for(int i=0;i repeatedRows(vector> &matrix, int M, int N) 5 | { 6 | vector ans; 7 | set> st; 8 | for(int i=0;i>1; 13 | } 14 | return 0; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Find maximum volume of a cuboid.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double maxVolume(double P, double A) { 4 | double l=(P-sqrt(pow(P,2)-(24*A)))/12; 5 | double v= (P*l*l - 8*l*l*l)/4; 6 | 7 | return v; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Find the Closest Element in BST GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int find(Node* root, int k, int &ans){ 5 | if(!root) return ans; 6 | if(root->data==k) return 0; 7 | 8 | if(root) ans=min(ans,abs(root->data-k)); 9 | if(root->left) ans=min(ans, abs(root->left->data-k)); 10 | if(root->right) ans=min(ans, abs(root->right->data-k)); 11 | 12 | if(root->data >k) find(root->left,k,ans); 13 | else find(root->right,k,ans); 14 | } 15 | 16 | 17 | int minDiff(Node *root, int K) 18 | { 19 | int ans=INT_MAX; 20 | return find(root, K, ans); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Find the closest pair from two arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | vector printClosest(int arr[], int brr[], int n, int m, int x) { 4 | vector ans(2); 5 | int mn=INT_MAX; 6 | int i=0; 7 | int j=m-1; 8 | int a=-1,b=-1; 9 | while(i=0){ 10 | int sum=arr[i]+brr[j]; 11 | int diff=abs(sum-x); 12 | if(diff< mn){ 13 | mn=diff; 14 | ans[0]=arr[i]; 15 | ans[1]=brr[j]; 16 | } 17 | if(sum>x) j--; 18 | else i++; 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Flip bits gfg.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int maxOnes(int a[], int n) 4 | { 5 | int r=0; 6 | int count=0; 7 | int flip=0; 8 | while(r data == x) 10 | return x; 11 | int ans1 = -1, ans2 = -1, ans3 = -1; 12 | ans1 = floor(root -> left, x); 13 | ans2 = (x - root -> data >= 0) ? root -> data : -1; 14 | if(ans2 != -1) 15 | ans3 = floor(root -> right, x); 16 | if(ans3 != -1) 17 | return ans3; 18 | if(ans2 != -1) 19 | return ans2; 20 | if(ans1 != -1) 21 | return ans1; 22 | return -1; 23 | } 24 | -------------------------------------------------------------------------------- /Form a number divisible by 3 using array digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int digitsum(int n){ 4 | int sum=0; 5 | while(n){ 6 | sum+=n%10; 7 | n=n/10; 8 | } 9 | return sum; 10 | } 11 | int isPossible(int N, int arr[]) { 12 | int sum=0; 13 | for(int i=0;i> dp(n + 1,vector(n + 1, 0)); 8 | 9 | for (int i = 1; i <= n; ++i) { 10 | for (int j = 1; j <= n; ++j) { 11 | if (str[i - 1] == s[j - 1]) { 12 | dp[i][j] = 1 + dp[i - 1][j - 1]; 13 | } else { 14 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 15 | } 16 | } 17 | } 18 | 19 | return n - dp[n][n]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Frequency Game Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int LargButMinFreq(int arr[], int n) { 4 | unordered_map mp; 5 | int f=INT_MAX; 6 | int ans=INT_MIN; 7 | for(int i=0;i> M) 4 | { 5 | int ans=0; 6 | for(int j=1;j=0 and j-1>=0) mx=max(mx,M[i-1][j-1]); 10 | if(j-1>=0) mx=max(mx,M[i][j-1]); 11 | if(i+1=0) mx=max(mx,M[i+1][j-1]); 12 | M[i][j]+=mx; 13 | ans=max(ans,M[i][j]); 14 | } 15 | } 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Good Subtrees GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | unordered_set help(Node* root, int k, int &ans){ 4 | unordered_set st; 5 | if(!root) return st; 6 | unordered_set left=help(root->left,k,ans); 7 | unordered_set right=help(root->right,k,ans); 8 | st.insert(left.begin(),left.end()); 9 | st.insert(right.begin(),right.end()); 10 | 11 | st.insert(root->data); 12 | 13 | if(st.size()<=k) ans++; 14 | return st; 15 | } 16 | int goodSubtrees(Node *root,int k){ 17 | int ans=0; 18 | help(root, k, ans); 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /How Many X's?.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countX(int L, int R, int X) { 4 | int count=0; 5 | for(int a=L+1; a 0){ 8 | int digit = temp % 10; 9 | if(digit == X){ 10 | count++; 11 | } 12 | temp /=10; 13 | } 14 | } 15 | return count; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Identical Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | bool areIdentical(struct Node *head1, struct Node *head2) { 2 | struct Node *t1=head1; 3 | struct Node *t2=head2; 4 | 5 | while(t1!=NULL || t2!=NULL){ 6 | if(!t1 || !t2 || t1->data!= t2->data) return false; 7 | t1=t1->next; 8 | t2=t2->next; 9 | } 10 | return true; 11 | } 12 | -------------------------------------------------------------------------------- /Implement Atoi.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int atoi(string s) { 4 | int ans=0; 5 | int sign=1; 6 | int n=s.size(); 7 | for(int i=0;i='0' && s[i]<='9'){ 12 | ans=ans*10+(s[i]-'0'); 13 | } 14 | else return -1; 15 | } 16 | ans=ans*sign; 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Inorder Traversal and BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int isRepresentingBST(int arr[], int N) 4 | { 5 | for(int i=0;i=arr[i+1]) return false; 7 | } 8 | return true; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Insert a node in a BST.cpp: -------------------------------------------------------------------------------- 1 | Node* insert(Node* &node, int data) { 2 | if(!node) { 3 | node = new Node(data); 4 | return node; 5 | } 6 | if(node->data == data) return node; 7 | else if(node->data > data) insert(node->left, data); 8 | else insert(node->right, data); 9 | } 10 | -------------------------------------------------------------------------------- /Integral Points Inside Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long int InternalCount(long long int p[], long long int q[],long long int r[]) { 4 | long long int area = (abs(p[0]*(q[1] - r[1]) + q[0]*(r[1] - p[1]) + r[0]*(p[1] - q[1])))/2; 5 | 6 | long long int b1 = __gcd(abs(p[0] - q[0]), abs(p[1] - q[1])); 7 | long long int b2 = __gcd(abs(q[0] - r[0]), abs(q[1] - r[1])); 8 | long long int b3 = __gcd(abs(r[0] - p[0]), abs(r[1] - p[1])); 9 | 10 | long long int boundaryPoints = b1 + b2 + b3; 11 | 12 | long long int internalPoints = area - boundaryPoints/2 + 1; 13 | 14 | return internalPoints; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Intersection of two sorted Linked lists.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | Node* findIntersection(Node* head1, Node* head2) 5 | { 6 | Node * ans=new Node(0); 7 | Node* temp=ans; 8 | while(head1 and head2){ 9 | if(head1->data==head2->data){ 10 | temp->next= new Node(head1->data); 11 | head1=head1->next; 12 | head2=head2->next; 13 | temp=temp->next; 14 | } 15 | else if( head1->data < head2->data) head1=head1->next; 16 | else head2=head2->next; 17 | } 18 | return ans->next; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Isomorphic Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool areIsomorphic(string s1, string s2) 5 | { 6 | int n=s1.length(); 7 | if(s2.length()!=n) return false; 8 | vector f(26,-1); 9 | unordered_mapmp; 10 | for(int i=0;imp; 5 | 6 | void solve(Node* root, int k, int sum, int& ans) 7 | { 8 | if(!root) return; 9 | 10 | sum += root->data; 11 | ans += mp[sum-k]; 12 | mp[sum]++; 13 | 14 | solve(root->left, k, sum,ans); 15 | solve(root->right, k, sum,ans); 16 | 17 | mp[sum]--; 18 | } 19 | 20 | int sumK(Node *root,int k) 21 | { 22 | int ans=0; 23 | mp[0]=1; 24 | solve(root, k, 0,ans); 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /K-Pangrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool kPangram(string str, int k) { 5 | vectorarr(26,0); 6 | int n=str.size(); 7 | int ch=0; 8 | for(int i=0;ik) return false; 21 | return true; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Kth Smallest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // arr : given array 4 | // k : find kth smallest element and return using this function 5 | int kthSmallest(vector &arr, int k) { 6 | priority_queuepq; 7 | int n=arr.size(); 8 | for(int i=0;i&parents){ 2 | if(!root) return false; 3 | parents.push_back(root->data); 4 | 5 | if(root->data==target) return true; 6 | if(find(root->left,target,parents) || find(root->right,target,parents) ) 7 | return true; 8 | parents.pop_back(); 9 | 10 | return false; 11 | } 12 | 13 | 14 | int kthAncestor(Node *root, int k, int node) 15 | { 16 | vector parents; 17 | find(root,node,parents); 18 | int n=parents.size(); 19 | if(n>k) 20 | return parents[n-k-1]; 21 | 22 | return -1; 23 | } 24 | -------------------------------------------------------------------------------- /Kth distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool checkDuplicatesWithinK(vector& arr, int k) { 5 | unordered_map mp; 6 | int n=arr.size(); 7 | if(n==1) return false; 8 | for(int i=0;i<=k;i++){ 9 | mp[arr[i]]++; 10 | if(mp[arr[i]]>1) return true; 11 | } 12 | int last=0; 13 | for(int i=k+1;i1) return true; 17 | } 18 | return false; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Kth largest element in BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthLargest(Node *root, int K) { 4 | int kthLargestValue = -1; // Initialize the result. 5 | reverseInorder(root, K, kthLargestValue); 6 | return kthLargestValue; 7 | } 8 | void reverseInorder(Node* root, int& K, int& kthLargestValue) { 9 | if (!root || K <= 0) { 10 | return; 11 | } 12 | reverseInorder(root->right, K, kthLargestValue); 13 | K--; 14 | 15 | if (K == 0) { 16 | kthLargestValue = root->data; 17 | return; 18 | } 19 | reverseInorder(root->left, K, kthLargestValue); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Largest Number formed from an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | static bool compare(const string &a, const string &b) { 4 | return (a + b) > (b + a); 5 | } 6 | string printLargest(int n, vector &arr) { 7 | // code here 8 | sort(arr.begin(), arr.end(), compare); 9 | string result; 10 | for (int i = 0; i < n; ++i) { 11 | result += arr[i]; 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Largest number possible.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | string findLargest(int N, int S){ 4 | string ans=""; 5 | for(int i=0;i=9){ 7 | ans+="9"; 8 | S-=9; 9 | }else{ 10 | ans+= to_string(S); 11 | S-=S; 12 | } 13 | } 14 | if(N>1 and ans[0]=='0') return "-1"; 15 | if(S==0) return ans; 16 | return "-1"; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Largest prime factor GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | long long int largestPrimeFactor(int N){ 4 | long long int ans=0; 5 | long long int i; 6 | for( i=2;i*i<=N;i++){ 7 | while(N%i==0){ 8 | if(i>ans) ans=i; 9 | N=N/i; 10 | } 11 | } 12 | if(N>1){ 13 | if(N>ans) ans=N; 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Largest square formed in a matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSquare(int n, int m, vector> mat) { 4 | int res = 0; 5 | for(int i=0; i= 0 && j-1>=0){ 8 | mat[i][j] = min({mat[i-1][j],mat[i][j-1],mat[i-1][j-1]})+1; 9 | } 10 | res = max(res,mat[i][j]); 11 | } 12 | } 13 | return res; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leaders in an array gfg.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | //Function to find the leaders in the array. 3 | public: 4 | vector leaders(int a[], int n){ 5 | vector ans; 6 | int mx=INT_MIN; 7 | for(int i=n-1;i>=0;i--){ 8 | if(a[i]>=mx){ 9 | mx=a[i]; 10 | ans.push_back(mx); 11 | } 12 | } 13 | 14 | reverse(ans.begin(),ans.end()); 15 | return ans; 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Left Rotate Matrix K times.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> rotateMatrix(int k, vector> mat) { 4 | int n = mat.size(); 5 | int m = mat[0].size(); 6 | 7 | k = k % m; 8 | if (k == 0) return mat; 9 | vector> arr(n, vector(m)); 10 | 11 | for (int i = 0; i < m; i++) { 12 | for (int j = 0; j < n; j++) { 13 | arr[j][i] = mat[j][(i + k) % m]; 14 | } 15 | } 16 | 17 | return arr; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Left View of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | vector leftView(Node *root) 2 | { 3 | vector ans; 4 | if(!root) return ans; 5 | queue q; 6 | q.push(root); 7 | while(!q.empty()){ 8 | int size=q.size(); 9 | ans.push_back(q.front()->data); 10 | while(size--){ 11 | Node* t=q.front(); 12 | q.pop(); 13 | if(t->left) q.push(t->left); 14 | if(t->right) q.push(t->right); 15 | } 16 | } 17 | return ans; 18 | } 19 | -------------------------------------------------------------------------------- /Leftmost and rightmost nodes of binary tree.cpp: -------------------------------------------------------------------------------- 1 | void printCorner(Node *root){ 2 | queueq; 3 | q.push(root); 4 | while(!q.empty()){ 5 | int n=q.size(), flag=0; 6 | while(n--){ 7 | auto temp=q.front(); q.pop(); 8 | if(n==0||flag==0){ 9 | flag=1; cout<data<<" "; 10 | } 11 | if(temp->left) q.push(temp->left); 12 | if(temp->right) q.push(temp->right); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Level of Nodes.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int nodeLevel(int V, vector adj[], int X) 5 | { 6 | queue> q; 7 | vector vis(V,0); 8 | q.push({0,0}); 9 | vis[0]=1; 10 | while(!q.empty()){ 11 | int node=q.front().first; 12 | int level=q.front().second; 13 | q.pop(); 14 | if(node==X) return level; 15 | for(auto it: adj[node]){ 16 | if(!vis[it]){ 17 | q.push({it,level+1}); 18 | vis[it]=1; 19 | } 20 | } 21 | } 22 | return -1; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Longest Common Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubstr(string str1, string str2) { 4 | int n=str1.size(); 5 | int m=str2.size(); 6 | int dp[n+1][m+1]; 7 | for(int i=0;i<=n;i++){ 8 | for(int j=0;j<=m;j++){ 9 | if(i==0||j==0)dp[i][j]=0; 10 | } 11 | } 12 | int ans=0; 13 | for(int i=1;i<=n;i++){ 14 | for(int j=1;j<=m;j++){ 15 | if(str1[i-1]==str2[j-1]){ 16 | dp[i][j]=1+dp[i-1][j-1]; 17 | ans=max(ans,dp[i][j]); 18 | }else{ 19 | dp[i][j]=0; 20 | } 21 | } 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Longest K unique characters substring.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | public: 4 | int longestKSubstr(string s, int k) { 5 | 6 | int freq[26]={0},ans=-1,i=0,j=0,n=s.length(),sum; 7 | freq[s[0]-'a']++; 8 | while(jk) freq[s[i++]-'a']--; 12 | else{ 13 | if(sum==k) ans=max(ans,(j-i+1)); 14 | freq[s[++j]-'a']++; 15 | } 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Longest Repeating Subsequence Medium GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int LongestRepeatingSubsequence(string str){ 4 | int n=str.size(); 5 | int dp[n+1][n+1]; 6 | 7 | for(int i = 0; i <= n; i++) { 8 | for(int j = 0; j <= n; j++) { 9 | if(i == 0 || j == 0) dp[i][j] = 0; 10 | else if(str[i-1] == str[j-1] && i!=j) dp[i][j] = 1 + dp[i-1][j-1]; 11 | else dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 12 | } 13 | } 14 | return dp[n][n]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Longest increasing subsequence gfg.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int longestSubsequence(int n, int a[]) 5 | { 6 | vector ans; 7 | ans.push_back(a[0]); 8 | for(int i=1;ians.back()){ 10 | ans.push_back(a[i]); 11 | } 12 | else{ 13 | int ind= lower_bound(ans.begin(),ans.end(), a[i])- ans.begin(); 14 | ans[ind]=a[i]; 15 | } 16 | } 17 | return ans.size(); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Lowest common ancestorGFg.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | Node* LCA(Node *root, int n1, int n2) 4 | { 5 | if(root==NULL) return NULL; 6 | int curr=root->data; 7 | if(currright,n1,n2); 8 | if(curr>n1 && curr>n2) return LCA(root->left,n1,n2); 9 | return root; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Lucky Numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | bool isLucky(int n) { 4 | for(int i=2;i<=n;i++){ 5 | if(n%i==0)return false; 6 | n=n-n/i; 7 | } 8 | return true; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Make Binary Tree From Linked List.cpp: -------------------------------------------------------------------------------- 1 | void constructTree(int ind, vector &arr, TreeNode* &node) 2 | { 3 | node = new TreeNode(arr[ind]); 4 | int leftInd = 2*ind + 1, rightInd = 2*ind + 2; 5 | 6 | if(leftInd < arr.size()) 7 | constructTree(leftInd,arr,node->left); 8 | 9 | else 10 | return; 11 | 12 | if(rightInd < arr.size()) 13 | constructTree(rightInd,arr,node->right); 14 | 15 | } 16 | void convert(Node *head, TreeNode *&root) { 17 | vector arr; 18 | Node* temp = head; 19 | while(temp != NULL) 20 | { 21 | arr.push_back(temp->data); 22 | temp = temp->next; 23 | } 24 | constructTree(0,arr,root); 25 | } 26 | -------------------------------------------------------------------------------- /Make Palindrome Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | bool makePalindrome(int n,vector &arr){ 4 | map mp; 5 | for(auto it: arr) mp[it]++; 6 | 7 | for(auto str: arr){ 8 | string rev=str; 9 | reverse(rev.begin(),rev.end()); 10 | if(mp.find(rev)==mp.end()){ 11 | return false; 12 | } 13 | else { 14 | if(mp[rev]>1) mp[rev]--; 15 | else 16 | mp.erase(rev); 17 | } 18 | } 19 | return true; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Make array elements unique.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minIncrements(vector arr) { 4 | unordered_set st; 5 | int n=arr.size(); 6 | int sum=0,maxi=0; 7 | sort(arr.begin(),arr.end()); 8 | for(int i=0;i &Arr , int N){ 4 | long ans = 0, sum = 0; 5 | for(int i = 0; i < N; i++){ 6 | if(i < K) 7 | sum += Arr[i]; 8 | else{ 9 | ans = max(ans,sum); 10 | sum += Arr[i]; 11 | sum -= Arr[i-K]; 12 | } 13 | } 14 | ans = max(ans,sum); 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Maximize Array Value After Rearrangement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int Maximize(vector arr) { 4 | int Mod=1e9+7; 5 | int ans=0; 6 | sort(arr.begin(),arr.end()); 7 | for(int i=0;i &arr) { 4 | long long int sum=0; 5 | for(int i=0;i0;i--){ 10 | if(sum%i==0) return i; 11 | } 12 | 13 | return N; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Maximum Meetings in One Room.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | vector maxMeetings(int N,vector &S,vector &F){ 4 | vector,int>> v; 5 | for(int i=0;i ans; 10 | int end=v[0].first.first; 11 | ans.push_back(v[0].second); 12 | for(int i=1;i end){ 14 | ans.push_back(v[i].second); 15 | end=v[i].first.first; 16 | } 17 | } 18 | sort(ans.begin(),ans.end()); 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Maximum Product Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // Function to find maximum product subarray 4 | int maxProduct(vector &arr) { 5 | int n=arr.size(); 6 | long long ma=arr[0]; 7 | long long mi=arr[0]; 8 | long long ans=arr[0]; 9 | 10 | for(int i=1;i right(n); // max, j 5 | vector left(n); // min, i 6 | left[0] = arr[0]; 7 | right[n - 1] = arr[n - 1]; 8 | 9 | for (int i = 1; i < n; i++) left[i] = min(arr[i], left[i - 1]); 10 | 11 | 12 | for (int j = n - 2; j >= 0; j--) right[j] = max(arr[j], right[j + 1]); 13 | 14 | 15 | int i = 0, j = 0, ans = 0; 16 | while (j < n && i < n) { 17 | if (left[i] <= right[j]) { 18 | ans = max(ans, j - i); 19 | j++; 20 | } else { 21 | i++; 22 | } 23 | } 24 | 25 | return ans; 26 | } 27 | 28 | }; 29 | -------------------------------------------------------------------------------- /Maximum product subset of an array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long int findMaxProduct(vector& arr) { 4 | int n=arr.size(); 5 | long long ans = 1; 6 | int mod = 1e9+7; 7 | int mn = INT_MIN; 8 | for (int i=0;i &arr) { 4 | int n=arr.size(); 5 | if(n==1) return 0; 6 | sort(arr.begin(),arr.end()); 7 | int ans=arr[n-1]-arr[0]; 8 | for(int i=0;i2){ 10 | if(n%2==0){ 11 | n=n/2; 12 | } 13 | else{ 14 | n=n-1; 15 | } 16 | steps++; 17 | } 18 | return steps; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Modified Game of Nim.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int findWinner(int n, int A[]){ 4 | int num=0; 5 | for(int i=0;i&a, pair&b){ 4 | return (a.second> meet; 9 | for(int i=0;i& arr, int k) { 4 | priority_queue,greater> q; 5 | int n=arr.size(); 6 | for(int i=0;i<=k;i++) q.push(arr[i]); 7 | int i=0,j=k+1; 8 | while(j v,set&st, int k) { 4 | if(!root) return; 5 | v.push_back(root); 6 | if(!root->left && !root->right) { 7 | int indx = lvl - k; 8 | if(indx >= 0) 9 | st.insert(v[indx]); 10 | return; 11 | } 12 | solve(root->left, lvl+1, v,st, k); 13 | solve(root->right, lvl+1, v,st, k); 14 | } 15 | public: 16 | int printKDistantfromLeaf(Node* root, int k) 17 | { 18 | vector v; 19 | set st; 20 | solve(root, 0, v,st, k); 21 | return st.size(); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Non Repeating Character Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to find the first non-repeating character in a string. 5 | char nonrepeatingCharacter(string S) 6 | { 7 | //hello 8 | // h-1 9 | // e=1 10 | //l =2 11 | // o-1 12 | unordered_mapmp; 13 | for( auto c: S) mp[c]++; 14 | 15 | for(auto c: S){ 16 | if(mp[c]==1) return c; 17 | } 18 | 19 | return '$'; 20 | 21 | } 22 | 23 | }; 24 | -------------------------------------------------------------------------------- /Non repeating numbers gfg.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector singleNumber(vector nums) 5 | { 6 | int temp=0; 7 | for(int i=0;i ans(n+1,0); 10 | ans[0]=1; 11 | ans[1]=1; 12 | for(int i=2;i<=n;i++){ 13 | int l=0,r=i-1; 14 | while(l=0){ 15 | ans[i]= (ans[i]+ (ans[l]*ans[r])%mod)%mod; 16 | l++; 17 | r--; 18 | } 19 | 20 | } 21 | return ans[n]; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Nth fibonacci number gfg.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mod=1e9+7; 4 | int nthFibonacci(int n){ 5 | if(n==0) return 0; 6 | if(n==1) return 1; 7 | int a=0,b=1,c; 8 | for(int i=2;i<=n;i++){ 9 | c=(a+b)%mod; 10 | a=b; 11 | b=c; 12 | } 13 | return c; 14 | } 15 | }; 16 | 17 | // dp 18 | class Solution { 19 | public: 20 | int mod=1e9+7; 21 | int nthFibonacci(int n){ 22 | if(n==0) return 0; 23 | if(n==1) return 1; 24 | int l=1; 25 | int sl=0; 26 | for(int i=2;i<=n;i++){ 27 | dp[i]=(dp[i-1]+dp[i-2])%mod; 28 | } 29 | return dp[n]; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Nth node from end of linked list Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | Node* reverse(Node *head){ 2 | Node* prev=NULL; 3 | Node* current=head; 4 | Node* nextnode; 5 | while(current){ 6 | nextnode=current->next; 7 | current->next=prev; 8 | prev=current; 9 | current=nextnode; 10 | } 11 | return prev; 12 | } 13 | int getNthFromLast(Node *head, int n) 14 | { 15 | Node *temp=reverse(head); 16 | int k=1; 17 | while(temp){ 18 | if(k==n) return temp->data; 19 | temp=temp->next; 20 | k++; 21 | } 22 | return -1; 23 | } 24 | -------------------------------------------------------------------------------- /Number following a pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | string printMinNumberForPattern(string s){ 4 | string ans=""; 5 | stack st; 6 | int n=s.length(); 7 | for(int i=0;i<=n;i++){ 8 | st.push(i+1); 9 | if(s[i]=='I' || i==n){ 10 | while(!st.empty()){ 11 | ans+=to_string(st.top()); 12 | st.pop(); 13 | } 14 | } 15 | } 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Number of subarrays with maximum values in given range.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | long countSubarrays(int a[], int n, int L, int R) 4 | { 5 | long ans=0; 6 | long btwLR=0, lessL=0 ; 7 | for(int i=0;iR){ 13 | btwLR=lessL=0; 14 | } 15 | else{ 16 | ans+=btwLR+lessL+1; 17 | btwLR+=lessL+1; 18 | lessL=0; 19 | } 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Overlapping Intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> mergeOverlap(vector>& arr) { 4 | int n=arr.size(); 5 | sort(arr.begin(),arr.end()); 6 | vector> ans; 7 | int cnt=0; 8 | 9 | ans.push_back(arr[0]); 10 | for(int i=1;i=arr[i][0]){ 12 | ans[cnt][1]=max(ans[cnt][1],arr[i][1]); 13 | } 14 | else{ 15 | ans.push_back(arr[i]); 16 | cnt++; 17 | } 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Painting the Fence.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | long long countWays(int n, int k){ 4 | int mod=1e9+7; 5 | long long nonR=k; 6 | long long R=0; 7 | for(int i=0;inext) 7 | return head; 8 | Node *temp=head->next; 9 | head->next=pairWiseSwap(temp->next); 10 | temp->next=head; 11 | return temp; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Panagram Checking.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to check if a string is Pangram or not. 5 | bool checkPangram (string s) { 6 | vector arr(26,0); 7 | int n=s.length(); 8 | for(int i=0;i='a' && s[i]<='z' ) || (s[i]>='A' && s[i]<='Z' )){ 10 | char c=tolower(s[i]); 11 | arr[c-'a']=1; 12 | } 13 | } 14 | for(int i=0;i<26;i++){ 15 | if(arr[i]==0) return false; 16 | } 17 | return true; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Pascal Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | 4 | vector nthRowOfPascalTriangle(int n) { 5 | vector prev; 6 | int mod=1e9+7; 7 | for (int i = 0; i < n; i++) { 8 | vector curr(i + 1, 1); 9 | for (int j = 1; j < i; j++) { 10 | curr[j] = (prev[j - 1] + prev[j])%mod ; 11 | } 12 | prev = curr; 13 | } 14 | return prev ; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Paths from root with a specified sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void dfs(Node* root, int sum, int target,vector&temp,vector>&ans){ 5 | if(!root) return; 6 | temp.push_back(root->key); 7 | if(sum+root->key==target){ 8 | ans.push_back(temp); 9 | } 10 | dfs(root->left,sum+root->key,target,temp,ans); 11 | dfs(root->right,sum+root->key,target,temp,ans); 12 | temp.pop_back(); 13 | } 14 | vector> printPaths(Node *root, int sum) 15 | { 16 | vector> ans; 17 | vector temp; 18 | dfs(root,0,sum,temp,ans); 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Peak element.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int peakElement(int arr[], int n) 5 | { 6 | int l=0,h=n-1; 7 | while(lleft); 8 | if(prev){ 9 | prev->next = root; 10 | } 11 | prev = root; 12 | populateNext(root->right); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Power of 2.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // Function to check if given number n is a power of two. 4 | bool isPowerofTwo(long long n){ 5 | if(n==0) return false; 6 | if(n==1) return true; 7 | while(n>2){ 8 | if(1&n) return false; 9 | n=n>>1; 10 | } 11 | if(1&n) return false; 12 | return true; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Power of Numbers medium GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int m=1000000007; 4 | 5 | long long power(int N,int R) 6 | { 7 | // recursive 8 | // if(R==1)return N%m; 9 | // long long ans=power(N,R/2); 10 | // ans=ans*ans%m; 11 | // if(R%2==1)ans=(ans*N)%m; 12 | // return ans; 13 | 14 | // iterative 15 | long long ans = 1; 16 | long long base = N % m; 17 | 18 | while (R > 0) { 19 | if (R % 2 == 1) { 20 | ans = (ans * base) % m; 21 | } 22 | base = (base * base) % m; 23 | R /= 2; 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Powerfull Integer EASY GFG: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int powerfullInteger(int n,vector> &intervals,int k){ 4 | int mx=INT_MIN; 5 | 6 | for( auto it: intervals ) mx=max(mx,it[1]); 7 | 8 | vector mp(mx+2,0); 9 | 10 | for(auto it:intervals){ 11 | int start=it[0]; 12 | int end=it[1]+1; 13 | mp[start]++; 14 | mp[end]--; 15 | } 16 | 17 | 18 | int ans=-1; 19 | for(int i=0;i=k ) ans=max(ans,i); 22 | } 23 | return ans; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Predict the Column.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int columnWithMaxZeros(vector>arr,int N){ 4 | int ans=0; 5 | int col=-1; 6 | for(int i=0;ians){ 12 | ans=cnt; 13 | col=i; 14 | } 15 | } 16 | } 17 | } 18 | return col; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Print Bracket Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | vector bracketNumbers(string str) { 5 | vector ans; 6 | int open=0; 7 | stackst; 8 | int n=str.length(); 9 | int i=0; 10 | while(i snakePattern(vector > matrix) 6 | { 7 | vector ans; 8 | int n=matrix.size(); 9 | 10 | for(int i=0;i=0;j--){ 18 | ans.push_back(matrix[i][j]); 19 | } 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Print Pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | void printSeq(vector& ans,int N, int x){ 4 | if(N<=0 || N>x){ 5 | ans.push_back(N); 6 | return; 7 | } 8 | ans.push_back(N); 9 | printSeq(ans,N-5,x); 10 | if(N<=x){ 11 | ans.push_back(N); 12 | } 13 | } 14 | vector pattern(int N){ 15 | vector ans; 16 | printSeq(ans,N,N); 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Print adjacency list.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // Function to return the adjacency list for each vertex. 4 | vector> printGraph(int V, vector>edges) { 5 | vector> ans(V); 6 | for(int i=0;i printFibb(int n) 6 | { 7 | vectorans; 8 | ans.push_back(1); 9 | long long f=0; 10 | long long s=1; 11 | long long num; 12 | while(n>1){ 13 | num=f+s; 14 | ans.push_back(num); 15 | f=s; 16 | s=num; 17 | n--; 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Pythagorean Triplet.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | bool checkTriplet(int arr[], int n) { 4 | if(n<3) return false; 5 | sort(arr,arr+n); 6 | for(int i=0;i=2;k--){ 9 | int i=0,j=k-1; 10 | while(i &q, int k){ 5 | q.push(k); 6 | } 7 | 8 | // Function to find frequency of an element 9 | // return the frequency of k 10 | int findFrequency(queue &q, int k){ 11 | int size=q.size(); 12 | int f=0; 13 | while(size--){ 14 | int x=q.front(); 15 | if(x==k) f++; 16 | q.pop(); 17 | q.push(x); 18 | } 19 | return f; 20 | } 21 | 22 | }; 23 | -------------------------------------------------------------------------------- /Queue Reversal Basic GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | queue rev(queue q) 5 | { 6 | int size=q.size(); 7 | vector arr; 8 | while(size--){ 9 | int x=q.front(); 10 | q.pop(); 11 | arr.push_back(x); 12 | } 13 | size=arr.size(); 14 | for(int i=size-1;i>=0;i--){ 15 | q.push(arr[i]); 16 | } 17 | return q; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Reach the Nth point.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int nthPoint(int n){ 4 | int mod=1e9+7; 5 | if(n==1) return 1; 6 | if(n==2) return 2; 7 | int f=1; 8 | int s=2; 9 | for(int i=3;i<=n;i++){ 10 | int t=(f+s)%mod; 11 | f=s; 12 | s=t; 13 | } 14 | return s; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Rearrange an array with O(1) extra space GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | void arrange(long long arr[], int n) { 4 | 5 | for(int i=0;i recamanSequence(int n){ 4 | unordered_set st; 5 | vector ans(n); 6 | ans[0]=0; 7 | st.insert(0); 8 | for(int i=1;i arr(26,0); 6 | string ans; 7 | int n=str.size(); 8 | for(int i=0;inext!=NULL){ 6 | if(temp->data== temp->next->data){ 7 | temp->next=temp->next->next; 8 | } 9 | else 10 | temp=temp->next; 11 | } 12 | return head; 13 | } 14 | -------------------------------------------------------------------------------- /Reverse Coding Easy GFg.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mod=1000000007; 4 | int sumOfNaturals(int n) { 5 | int f=n%mod; 6 | int s=(n+1)%mod; 7 | 8 | long long ans= (f*1L*s)/2; 9 | return int (ans%mod); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Reverse First K elements of Queue.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | queue modifyQueue(queue q, int k) { 5 | stack st; 6 | int n=q.size(); 7 | while(k--){ 8 | st.push(q.front()); 9 | q.pop(); 10 | n--; 11 | } 12 | while(!st.empty()){ 13 | q.push(st.top()); 14 | st.pop(); 15 | } 16 | while(n--){ 17 | int x=q.front(); 18 | q.pop(); 19 | q.push(x); 20 | } 21 | return q; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Reverse a Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | Node* reverseDLL(Node * head) 5 | { 6 | Node* front=head; 7 | Node* last=head; 8 | int n=0; 9 | while(last->next!=NULL){ 10 | last=last->next; 11 | n++; 12 | } 13 | int i=0; 14 | while(i<=n/2){ 15 | int x=front->data; 16 | front->data=last->data; 17 | last->data=x; 18 | front=front->next; 19 | last=last->prev; 20 | i++; 21 | } 22 | return head; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Reverse a Linked List in groups of given size Medium GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | struct node *reverse (struct node *head, int k) 5 | { 6 | node* curr=head; 7 | node* prev=NULL; 8 | node* nextnode=head; 9 | int count=0; 10 | while(countnext; 12 | curr->next=prev; 13 | prev=curr; 14 | curr=nextnode; 15 | count++; 16 | } 17 | if(curr!=NULL){ 18 | head->next= reverse(curr,k); 19 | } 20 | return prev; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Reverse a String gfg.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string reverseWord(string str) 5 | { 6 | reverse(str.begin(),str.end()); 7 | return str; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Reverse a stack GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | void Reverse(stack &St){ 4 | if(St.empty()){ 5 | return; 6 | } 7 | int x=St.top(); 8 | St.pop(); 9 | Reverse(St); 10 | 11 | insert(St,x); 12 | } 13 | 14 | void insert(stack &St, int value){ 15 | if(St.empty()){ 16 | St.push(value); 17 | return; 18 | } 19 | int x=St.top(); 20 | St.pop(); 21 | insert(St,value); 22 | 23 | St.push(x); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Reversing the equation Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string reverseEqn (string s) 5 | { 6 | int i=0; 7 | int n=s.size(); 8 | while(i rightView(Node *root) 5 | { 6 | queue q; 7 | vector ans; 8 | q.push(root); 9 | while(!q.empty()){ 10 | int size=q.size(); 11 | while(size){ 12 | Node* node=q.front(); 13 | if(size==1) ans.push_back(q.front()->data); 14 | q.pop(); 15 | if(node->left) q.push(node->left); 16 | if(node->right) q.push(node->right); 17 | size--; 18 | } 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Rightmost different bit.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to find the first position with different bits. 5 | int posOfRightMostDiffBit(int m, int n) 6 | { 7 | if(m==n) return -1; 8 | int ans=1; 9 | // while( m >0 || n>0){ 10 | // if( (m&1) != (n&1) ){ 11 | // return ans; 12 | // } 13 | // m=m>>1; 14 | // n=n>>1; 15 | // ans++; 16 | // } 17 | 18 | int num=m^n; 19 | while(num>0){ 20 | if(num&1) return ans; 21 | num=num>>1; 22 | ans++; 23 | } 24 | 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Roman Number to Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToDecimal(string &s) { 4 | int ans=0; 5 | int n=s.length(); 6 | unordered_map mp{ 7 | {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}}; 8 | 9 | ans+=mp[s[n-1]]; 10 | int prev=mp[s[n-1]]; 11 | for(int i=n-2;i>=0;i--){ 12 | if(mp[s[i]]>= prev){ 13 | ans+=mp[s[i]]; 14 | } 15 | else{ 16 | ans-=mp[s[i]]; 17 | } 18 | prev=mp[s[i]]; 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Roof Top.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // Function to find maximum number of consecutive steps 4 | // to gain an increase in altitude with each step. 5 | int maxStep(vector& arr) { 6 | int cnt=0; 7 | int n=arr.size(); 8 | int ans=0; 9 | for(int i=1;iarr[i-1]){ 11 | cnt++; 12 | ans=max(ans,cnt); 13 | } 14 | else{ 15 | cnt=0; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Root to leaf path sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool dfs(Node* root,int sum, int target){ 4 | if(!root->left && !root->right){ 5 | if(sum+root->data==target) 6 | return true; 7 | return false; 8 | } 9 | bool left=false, right=false; 10 | 11 | if(root->left) 12 | left=dfs(root->left,sum+root->data, target); 13 | if(root->right) 14 | right=dfs(root->right,sum+root->data, target); 15 | 16 | return left||right; 17 | } 18 | bool hasPathSum(Node *root, int target) { 19 | return dfs(root,0,target); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Root to leaf paths sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void dfs(Node* root,int &ans, int x){ 4 | if(!root->left && !root->right){ 5 | ans+=x*10+root->data; 6 | return ; 7 | } 8 | if(root->left){ 9 | dfs(root->left,ans,x*10+root->data); 10 | } 11 | if(root->right){ 12 | dfs(root->right,ans,x*10 + root->data); 13 | } 14 | } 15 | int treePathsSum(Node *root) { 16 | int ans=0; 17 | int x=0; 18 | dfs(root,ans,x); 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Rotate a Linked List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | Node* rotate(Node* head, int k) { 4 | if (!head || !head->next || k == 0) return head; 5 | Node* temp = head; 6 | int n = 1; 7 | while (temp->next) { 8 | temp = temp->next; 9 | n++; 10 | } 11 | k = k % n; 12 | if (k == 0) return head; 13 | temp->next=head; 14 | Node* t=head; 15 | while(k>1){ 16 | t=t->next; 17 | k--; 18 | } 19 | head=t->next; 20 | t->next=NULL; 21 | return head; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Row with max 1s.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rowWithMax1s(vector > arr) { 4 | int ans=-1; 5 | int n=arr.size(); 6 | int m=arr[0].size(); 7 | int i=0; 8 | int j=m-1; 9 | while(i=0){ 10 | if(arr[i][j]==1){ 11 | ans=i; 12 | j--; 13 | } 14 | else{ 15 | i++; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Search Pattern (KMP-Algorithm).cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector search(string pat, string txt) 5 | { 6 | vectorans; 7 | int n=txt.size(); 8 | int m=pat.size(); 9 | if(n &arr) { 4 | int l=0,r=arr.size()-1; 5 | while(lm) return 0; 6 | 7 | int pick= help(i*2, j+1, n, m); 8 | int not_pick=help(i+1, j, n, m); 9 | 10 | return pick+not_pick; 11 | } 12 | int numberSequence(int m, int n){ 13 | return help(1,0,n,m); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Shortest Common Supersequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int shortestCommonSupersequence(string X, string Y, int m, int n) 5 | { 6 | int dp[m+1][n+1]; 7 | memset(dp,0,sizeof(dp)); 8 | for(int i=1;i<=m;i++){ 9 | for(int j=1;j<=n;j++){ 10 | if(X[i-1]==Y[j-1]) dp[i][j]=1+dp[i-1][j-1]; 11 | else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 12 | } 13 | } 14 | return (m+n-dp[m][n]); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Shortest path from 1 to n.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int minimumStep(int n){ 4 | //complete the function here 5 | int ans= 0 ; 6 | while(n > 0){ 7 | if(n % 3 == 0){ 8 | n = n /3; 9 | } 10 | else 11 | n--; 12 | ans++; 13 | } 14 | return ans-1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Shuffle Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | void solve(int arr[], int i, int j, int n) { 4 | if(n <= 0) 5 | return; 6 | 7 | int a = arr[i]; 8 | int b = arr[j]; 9 | 10 | solve(arr, i-1, j-1, n-2); 11 | 12 | arr[n-2] = a; 13 | arr[n-1] = b; 14 | } 15 | void shuffleArray(int arr[],int n) 16 | { 17 | int i=n/2-1; 18 | int j=n-1; 19 | solve(arr,i,j,n); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Smallest number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string smallestNumber(int s, int d) { 4 | if(d==1 && s>9) return "-1"; 5 | if(9*d0){ 9 | if(s>9){ 10 | ans="9"+ans; 11 | s=s-9; 12 | } 13 | else if(s<=9 && d>1){ 14 | ans=to_string(s)+ans; 15 | s=0; 16 | } 17 | else{ 18 | ans=to_string(s+1)+ans; 19 | } 20 | d--; 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Split array in three equal sum subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // Function to determine if array arr can be split into three equal sum sets. 4 | vector findSplit(vector& arr) { 5 | int n=arr.size(); 6 | vector ans; 7 | int sum=0; 8 | for(int i=0;i dp(n,0); 8 | if(n==1) return arr[0]; 9 | if(n==2) return max(arr[0],arr[1]); 10 | dp[0]=arr[0]; 11 | dp[1]=max(arr[0],arr[1]); 12 | for(int i=2;i &prices) { 4 | int n=prices.size(); 5 | int ans=0; 6 | if(n==1) return ans; 7 | int buy=prices[0]; 8 | for(int i=1;i &prices) { 4 | int ans=0; 5 | int buy=prices[0]; 6 | for(int i=0;istr[i]){ 11 | pre+=str[i]; 12 | } 13 | else if(pre.length()>1 && str[i-1]==str[i]){ 14 | pre+=str[i]; 15 | } 16 | else break; 17 | } 18 | string rev=pre; 19 | reverse(rev.begin(),rev.end()); 20 | return pre+rev; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /String's Count.cpp: -------------------------------------------------------------------------------- 1 | long long int countStr(long long int n){ 2 | return 1+n+n+n*(n-1)+n*(n-1)/2 + n*(n-1)*(n-2)/2; 3 | } 4 | -------------------------------------------------------------------------------- /Subarray with 0 sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | //Complete this function 4 | //Function to check whether there is a subarray present with 0-sum or not. 5 | bool subArrayExists(int arr[], int n) 6 | { 7 | unordered_sets; 8 | int sum=0; 9 | for(int i=0;i subarraySum(vector arr, int n, long long s) 5 | { 6 | int l = 0, r = 0; 7 | long long sum = 0; 8 | while (r < n) 9 | { 10 | sum += arr[r]; 11 | while (sum > s && lleft == NULL && root->right == NULL){ 8 | return root->data; 9 | } 10 | int leftans = solve(root->left , ans); 11 | int rightans = solve(root->right , ans); 12 | if(leftans + rightans != root->data){ 13 | ans = false; 14 | } 15 | return leftans + rightans +root->data; 16 | } 17 | bool isSumTree(Node* root) 18 | { 19 | bool ans = true; 20 | solve(root , ans); 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Sum of all divisors from 1 to n.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long sumOfDivisors(int N) 5 | { 6 | long long int ans = 0; 7 | for(int i=1;i<=N;i++){ 8 | ans+=(N/i)*i; 9 | } 10 | return ans; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Sum of bit differences.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | long long sumBitDifferences(int arr[], int n) { 4 | long long ans = 0; 5 | for(int i = 0;i<32;i++){ 6 | long long count = 0; 7 | for(int j = 0;j adj[], int V) { 4 | int ans=0; 5 | for(int i=0;i sumTriangles(const vector >& matrix, int n) 6 | { 7 | vector ans; 8 | int sum1=0,sum2=0; 9 | for(int i=0;i(2*n)) return 0; 5 | long long midPoint=n+1; 6 | long long ans=n- abs(midPoint-q); 7 | return ans; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Swap and Maximize.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxSum(vector& arr) { 4 | long long sum=0; 5 | int n=arr.size(); 6 | sort(arr.begin(),arr.end()); 7 | vector a; 8 | int i=0,j=n-1; 9 | while(i<=j){ 10 | if(i==j){ 11 | a.push_back(arr[i]); 12 | break; 13 | } 14 | a.push_back(arr[i++]); 15 | a.push_back(arr[j--]); 16 | } 17 | for(int i=1;idata != right->data) 8 | return false; 9 | 10 | return check(left->left, right->right) && check(left->right, right->left); 11 | } 12 | 13 | bool isSymmetric(Node* root) { 14 | if (root == NULL) 15 | return true; 16 | return check(root->left, root->right); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Techfest and the Queue.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int primes(int x){ 4 | int ans=0; 5 | for(int i=2;i*i<=x;i++){ 6 | while(x%i==0){ 7 | ans++; 8 | x=x/i; 9 | } 10 | } 11 | if(x!=1) ans++; 12 | return ans; 13 | } 14 | int sumOfPowers(int a, int b){ 15 | int ans=0; 16 | for(int i=a;i<=b;i++){ 17 | ans+=primes(i); 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Ticket Counter Easy GFG.cpp: -------------------------------------------------------------------------------- 1 | int distributeTicket(int n, int k) { 2 | 3 | deque dq; 4 | for(int i = 1; i <=n; i++){ 5 | dq.push_back(i); 6 | } 7 | bool front = true; 8 | int count = k; 9 | while(dq.size() > 1){ 10 | if(count > 0 and front == true){ 11 | dq.pop_front(); 12 | count--; 13 | } 14 | if(count == 0){ 15 | front = !front; 16 | count = k; 17 | } 18 | if(count > 0 and front == false){ 19 | dq.pop_back(); 20 | count--; 21 | } 22 | } 23 | return dq.front(); 24 | } 25 | -------------------------------------------------------------------------------- /Topological sort Medium GFG.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void topo(int node, vector&vis, stack& st, vector adj[]){ 5 | vis[node]=1; 6 | for(auto it: adj[node]){ 7 | if(!vis[it]){ 8 | topo(it,vis,st,adj); 9 | } 10 | } 11 | st.push(node); 12 | } 13 | 14 | vector topoSort(int N, vector adj[]) 15 | { 16 | stack st; 17 | vector vis(N,0); 18 | for(int i=0;i ans; 25 | while(!st.empty()){ 26 | ans.push_back(st.top()); 27 | st.pop(); 28 | } 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Total count.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int totalCount(int k, vector& arr) { 4 | int ans=0; 5 | int n=arr.size(); 6 | for(int i=0;i &A){ 4 | vector lmax(N,0); 5 | vector rmin(N,0); 6 | int lm=0; 7 | int rm=INT_MAX; 8 | 9 | if(N==1) return 0; 10 | 11 | for(int i=0;i=0;i--){ 16 | rm=min(rm,A[i]); 17 | rmin[i]=rm; 18 | } 19 | 20 | 21 | int ans=0; 22 | for(int i=1;i=K) ans++; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Transform to prime.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isPrime(int x){ 5 | if(x==1) return false; 6 | if(x==2) return true; 7 | for(int i=2;i<=sqrt(x);i++){ 8 | if(x%i==0) return false; 9 | } 10 | return true; 11 | } 12 | int minNumber(int arr[],int N) 13 | { 14 | int sum=0; 15 | for(int i=0;i> transitiveClosure(int N, vector> grid) 4 | { 5 | int n=N; 6 | for(int k=0; k >& matrix, int n) 6 | { 7 | for(int i=0;i mp; 7 | for(int i=0;i2, 2->1, 5->2 11 | set st; 12 | for(auto it: mp){ 13 | if(st.find(it.second)!=st.end()){ 14 | return false; 15 | } 16 | st.insert(it.second); 17 | } 18 | return true; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Vertical Width of a Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // Function to find the vertical width of a Binary Tree. 4 | void dfs(Node* root, int level,int &mn, int &mx){ 5 | if(!root) return; 6 | mn=min(mn,level); 7 | mx=max(mx,level); 8 | dfs(root->left,level-1,mn,mx); 9 | dfs(root->right,level+1,mn,mx); 10 | } 11 | int verticalWidth(Node* root) { 12 | int level=0; 13 | int mn=0; 14 | int mx=0; 15 | if(!root) return 0; 16 | dfs(root,level,mn,mx); 17 | return abs(mn)+mx+1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Wave array.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | void convertToWave(int n, vector& arr){ 4 | for(int i=0;idata; 8 | temp=temp->next; 9 | } 10 | string s=str; 11 | reverse(s.begin(),s.end()); 12 | return s==str?true:false; 13 | } 14 | }; 15 | --------------------------------------------------------------------------------