├── Phase - 1(60 questions) ├── Day-1 │ ├── 1. Two Sum.cpp │ ├── 2. Container With Most Water.cpp │ ├── 3. Permutations.cpp │ ├── 4. Binary Tree Inorder Traversal.cpp │ └── 5. Generate Parentheses.cpp ├── Day-10 │ ├── 37. Subarray with given sum.cpp │ └── 38. Maximum Index.cpp ├── Day-11 │ ├── 39. B. GCD Length.cpp │ ├── 40. C. Yet Another Card Deck.cpp │ ├── 41. D. Min Cost String.cpp │ ├── 42. A. Red and Blue Beans.cpp │ └── 43. B. The Cake Is a Lie.cpp ├── Day-12 │ ├── 44. C. The Sports Festival.cpp │ ├── 45. Mussadi lal and stairs.cpp │ ├── 46. Intersection of Two Linked Lists.cpp │ └── 47. Average of Levels in Binary Tree.cpp ├── Day-13 │ ├── 48. Maximum Intervals Overlap.cpp │ └── 49. Shortest Prime Path.cpp ├── Day-14 │ ├── 50. BFS of graph.cpp │ └── 51. DFS of Graph.cpp ├── Day-15 │ ├── 52. Optimal Strategy For A Game.cpp │ └── 53. Increasing Path in Matrix.cpp ├── Day-16 │ ├── 54. Longest Increasing Path in a Matrix.cpp │ ├── 55. Delete Operation for Two Strings.cpp │ ├── 56. Jump Game II.cpp │ ├── 57. Non-decreasing Array.cpp │ └── 58. Running Sum of 1d Array.cpp ├── Day-17 │ ├── 59. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp │ └── 60. Super Palindromes.cpp ├── Day-2 │ ├── 10. Kth Largest Element in an Array.cpp │ ├── 11. Palindrome Linked List.cpp │ ├── 6. Subsets.cpp │ ├── 7. Kth Smallest Element in a BST.cpp │ ├── 8. Top K Frequent Elements.cpp │ └── 9. Product of Array Except Self.cpp ├── Day-3 │ ├── 12. Remove loop in Linked List.cpp │ ├── 13. Non-decreasing Array.cpp │ └── 14. Count Primes.cpp ├── Day-4 │ ├── 15. Partition Equal Subset Sum.cpp │ ├── 16. Minimum Falling Path Sum.cpp │ ├── 17. Minimum Path Sum.cpp │ ├── 18. Longest Palindromic Substring.cpp │ ├── 19. Unique Paths.cpp │ └── 20. k-th Lexicographical String of All Happy Strings of Length n.cpp ├── Day-5 │ ├── 21. Partitioning Into Minimum Number Of Deci-Binary Numbers.cpp │ ├── 22. Find Center of Star Graph.cpp │ └── 23. 01 Matrix.cpp ├── Day-6 │ ├── 24. First Bad Version.cpp │ ├── 25. Jewels and Stones.cpp │ ├── 25. Ransom Note.cpp │ ├── 26. First Unique Character in a String.cpp │ └── 27. Number Complement.py ├── Day-7 │ ├── 29. Longest Increasing Subsequence.cpp │ └── 30. Maximum Sum Increasing Subsequence.cpp ├── Day-8 │ ├── 31. Longest Bitonic Subsequence.cpp │ ├── 32. Maximum Non-overlapping Bridges.cpp │ └── 33. Russian Doll Envelopes.cpp ├── Day-9 │ ├── 34. Round B 2021 - Kick Start-Increasing Substring.cpp │ ├── 35. Round B 2021 - Kick Start-Longest Progression.cpp │ └── 36. A. Review Site.cpp └── README.md ├── Phase - 2(60 questions) ├── Day-1 │ ├── 1. Permutations of a String.cpp │ ├── 2. Xor Equality.cpp │ └── 3. Remove K Digits.cpp ├── Day-10 │ ├── 24. Find All Duplicates in an Array.cpp │ ├── 25. Remove Duplicates from Sorted Array II.cpp │ ├── 26. Best Time to Buy and Sell Stock IV.cpp │ ├── 27. Longest Palindromic Substring.cpp │ └── 28. Longest Palindromic Subsequence.cpp ├── Day-11 │ ├── 29. Turn on the bulbs.cpp │ ├── 30. Edit Distance.cpp │ └── 31. Minimum Operations to Make Array Equal.cpp ├── Day-12 │ ├── 32. K-diff Pairs in an Array.cpp │ ├── 33. Majority Element.cpp │ ├── 34. Subarray Product Less Than K.cpp │ └── 35. Jump Game.cpp ├── Day-13 │ ├── 36. Jump Game III.cpp │ ├── 37. 0-1 Knapsack.cpp │ └── 38. Flip Array.cpp ├── Day-14 │ ├── 39. 3Sum.cpp │ ├── 40. Squares of a Sorted Array.cpp │ └── 41. Maximum Subarray.cpp ├── Day-15 │ ├── 42. Repeating Sub-Sequence.cpp │ ├── 43. Distinct Subsequences.cpp │ ├── 44. Scramble String.cpp │ └── 45. Tushar's Birthday Party.cpp ├── Day-16 │ ├── 46. Highest Product.cpp │ ├── 47. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts.cpp │ ├── 48. Rotate Image.cpp │ └── 49. Remove Covered Intervals.cpp ├── Day-17 │ ├── 50. Length of Longest Subsequence.cpp │ └── 51. Merge Intervals.cpp ├── Day-18 │ ├── 52. Search a 2D Matrix.cpp │ └── 53. Spiral Matrix.cpp ├── Day-19 │ ├── 54. Lowest Common Ancestor of a Binary Search Tree.cpp │ └── 55. Lowest Common Ancestor of a Binary Tree.cpp ├── Day-2 │ ├── 4. Kadane's Algorithm.cpp │ └── 5. Trie | (Insert and Search) .cpp ├── Day-20 │ ├── 56. Implement Trie (Prefix Tree).cpp │ ├── 57. Design Add and Search Words Data Structure.cpp │ └── 58. Min Cost Climbing Stairs.cpp ├── Day-21 │ ├── 59. Longest Word in Dictionary.cpp │ └── 60. Count Substrings That Differ by One Character.cpp ├── Day-3 │ ├── 6. Word Break.cpp │ ├── 7. Sort an array according to the other.cpp │ └── 8. Maximum XOR of Two Numbers in an Array.cpp ├── Day-4 │ ├── 10. Pow(x, n).cpp │ └── 9. Factorial Trailing Zeroes.cpp ├── Day-5 │ ├── 11. Reverse Words in a String.cpp │ └── 12. Longest Substring Without Repeating Characters.cpp ├── Day-6 │ ├── 13. Parenthesis Checker.cpp │ └── 14. Longest Valid Parentheses.cpp ├── Day-7 │ ├── 15. Countdown.cpp │ ├── 16. Decode String.cpp │ └── 17. Valid Anagram.cpp ├── Day-8 │ ├── 18. Repeated DNA Sequences.cpp │ ├── 19. Encoding Message .cpp │ └── 20. Asteroid Collision.cpp ├── Day-9 │ ├── 21. Best Time to Buy and Sell Stock II.cpp │ ├── 22. Jump Game II.cpp │ └── 23. Find All Numbers Disappeared in an Array.cpp └── README.md ├── Phase - 3(60 questions) ├── Day-1 │ ├── 1. Remove Islands.cpp │ ├── 2. N-Queens.cpp │ └── 3. Summation over all triplets.cpp ├── Day-10 │ ├── 30. Swap Nodes in Pairs.cpp │ ├── 31. Middle of the Linked List.cpp │ ├── 32. Linked List Cycle.cpp │ ├── 33. Palindrome Linked List.cpp │ └── 34. Maximum Score of a Good Subarray.cpp ├── Day-11 │ ├── 35. Redundant Connection.cpp │ ├── 36. Employee Importance.cpp │ └── 37. Find the Town Judge.cpp ├── Day-12 │ ├── 38. Split a String Into the Max Number of Unique Substrings.cpp │ ├── 39. C. Modified GDC.cpp │ ├── 40. Remove One Element to Make the Array Strictly Increasing.cpp │ ├── 41. Remove All Occurrences of a Substring.cpp │ └── 42. Maximum Alternating Subsequence Sum.cpp ├── Day-13 │ ├── 43. Minimum Remove to Make Valid Parentheses.cpp │ ├── 44. Cyclically Rotating a Grid.cpp │ ├── 45. Maximum Product Difference Between Two Pairs.cpp │ ├── 46. OOPS-classes.cpp │ └── 47. Trees: Is This a Binary Search Tree?.cpp ├── Day-14 │ ├── 48. Distinct Numbers in Window.cpp │ ├── 49. LRU Cache.cpp │ └── 50. N max pair combinations.cpp ├── Day-15 │ ├── 51. Collecting Coins Sequel.cpp │ ├── 52. Majority Vote.cpp │ ├── 53. Trailing Zeros.cpp │ ├── 54. Search for a Range.cpp │ ├── 55. First to Count to Target.cpp │ └── 56. Special Product List.cpp ├── Day-16 │ ├── 57. Largest Rectangle in Histogram.cpp │ ├── 58. Max Rectangle in Binary Matrix.cpp │ ├── 59. Largest square formed in a matrix.cpp │ └── 60. Maximal Rectangle.cpp ├── Day-2 │ ├── 4. Deletion Distance.cpp │ ├── 5. Time Planner.cpp │ └── 6. Flatten Binary Tree to Linked List.cpp ├── Day-3 │ ├── 10. D - Kth Excluded.cpp │ ├── 7. Shifted Array Search.cpp │ ├── 8. Add Two Numbers II.cpp │ └── 9. C - POW.cpp ├── Day-4 │ ├── 11. Grid Unique Paths.cpp │ ├── 12. Remove Duplicates from Sorted Array.cpp │ └── 13. Merge Two Sorted Lists II.cpp ├── Day-5 │ ├── 14. Odd Even Linked List.cpp │ ├── 15. Wildcard Matching.cpp │ └── 16. Rotate List.cpp ├── Day-6 │ ├── 17. All permutations.cpp │ ├── 18. All valid balanced parenthesis.cpp │ ├── 19. Average of Max Five Scores.cpp │ └── 20. Binary Sum.cpp ├── Day-7 │ ├── 21. Kth Smallest Element In Tree.cpp │ ├── 22. Two Sum II - Input array is sorted.cpp │ └── 23. 2-Sum Binary Tree.cpp ├── Day-8 │ ├── 24. Height of Binary Tree.cpp │ ├── 25. Count Leaves in Binary Tree.cpp │ └── 26. Mirror Tree.cpp ├── Day-9 │ ├── 27. Largest Odd Number in String.cpp │ ├── 28. The Number of Full Rounds You Have Played.cpp │ └── 29. Count Sub Islands.cpp └── README.md ├── Phase - 4(60 questions) ├── Day-01 │ ├── 1. D. Deleting Divisors.cpp │ ├── 2. Minimize elements to be added for Subsequence.cpp │ ├── 3. Count triplets smaller sum.cpp │ └── 4. Maximum sum increasing triplets.cpp ├── Day-02 │ ├── 5. Stone Game.cpp │ ├── 6. Subsets.cpp │ └── 7. Subsets II.cpp ├── Day-03 │ ├── 10. Count Good Numbers.cpp │ ├── 11. Sparse Arrays.cpp │ ├── 12. Maximum Subarray.cpp │ ├── 13. Climbing Stairs.cpp │ ├── 14. Divisor Game.cpp │ ├── 15. Stone Game II.cpp │ ├── 16. Counting Bits.cpp │ ├── 17. Decode Ways.cpp │ ├── 8. Build Array from Permutation.cpp │ └── 9. Eliminate Maximum Number of Monsters.cpp ├── Day-04 │ ├── 18. Divisible by 10.cpp │ ├── 19. Connected Components in a Graph.cpp │ └── 20. Is it a tree.cpp ├── Day-05 │ ├── 21. Clone Graph.cpp │ ├── 22. Job Scheduling - max number of jobs.cpp │ ├── 23. Peak Index in a Mountain Array.cpp │ ├── 24. Course Schedule.cpp │ ├── 25. Evaluate Division.cpp │ └── 26. Sqrt(x).cpp ├── Day-06 │ ├── 27. Symmetric Tree.cpp │ └── 28. Count Square Sum Triples.cpp ├── Day-07 │ ├── 29. Arithmetic Square.cpp │ ├── 30. Sliding Window Maximum.cpp │ ├── 31. Gas Station.cpp │ └── 32. Least Common Ancestor.cpp ├── Day-08 │ ├── 33. Median of Array.cpp │ ├── 34. House Robber III.cpp │ ├── 35. House Robber II.cpp │ ├── 36. Balanced Binary Tree.cpp │ └── 37. Container With Most Water.cpp ├── Day-09 │ ├── 38. Majority Element.cpp │ ├── 39. Rotated Sorted Array Search.cpp │ ├── 40. Repetitions.cpp │ ├── 41. Increasing Array.cpp │ ├── 42. Permutations.cpp │ ├── 43. Binary Tree Preorder Traversal.cpp │ ├── 44. Binary Tree Postorder Traversal.cpp │ ├── 45. Binary Tree Inorder Traversal.cpp │ ├── 46. Number Spiral.cpp │ └── 47. Two Knights.cpp ├── Day-10 │ ├── 48. Two Sets.cpp │ ├── 49. Bit Strings.cpp │ └── 50. Trailing Zeros.cpp ├── Day-11 │ ├── 51. Coin Piles.cpp │ └── 52. Palindrome Reorder.cpp ├── Day-12 │ ├── 53. Creating Strings.cpp │ └── 54. Apple Division.cpp ├── Day-13 │ ├── 55. Distinct Numbers.cpp │ ├── 56. Apartments.cpp │ ├── 57. Ferris Wheel.cpp │ ├── 58. Dice Combinations.cpp │ ├── 59. Book Shop.cpp │ └── 60. Course Schedule.cpp └── README.md ├── Phase - 5(60 questions) ├── Day-01 │ ├── 1. Sum of Digits of String After Convert.cpp │ ├── 2. Largest Number After Mutating Substring.cpp │ └── 3. Maximum Compatibility Score Sum.cpp ├── Day-02 │ ├── 10. A. Digits Sum.cpp │ ├── 4. RMQSQ - Range Minimum Query.cpp │ ├── 5. Help Ashu.cpp │ ├── 6. Multiples of 3.cpp │ ├── 7. A. Subsequence Permutation.cpp │ ├── 8. B. I Hate 1111.cpp │ └── 9. D. Buying Shovels.cpp ├── Day-03 │ ├── 11. C. Friends and Gifts.cpp │ ├── 12. B. Running for Gold.cpp │ ├── 13. A. Polycarp and Coins.cpp │ └── 14. Letter Combinations of a Phone Number.cpp ├── Day-04 │ ├── 15. Course Schedule II.cpp │ ├── 16. Number of Provinces.cpp │ ├── 17. Shortest Bridge.cpp │ ├── 18. All Paths From Source to Target.cpp │ ├── 19. Surrounded Regions.cpp │ ├── 20. Rotting Oranges.cpp │ └── 21. Word Ladder.cpp ├── Day-05 │ ├── 22. C. Boboniu and Bit Operations.cpp │ └── 23. Count primes using digits.cpp ├── Day-06 │ ├── 24. Next Greater Element I.cpp │ ├── 25. Next Greater Element II.cpp │ ├── 26. Nearest Smaller Element.cpp │ ├── 27. Stock span problem.cpp │ └── 28. Largest Rectangle in Histogram.cpp ├── Day-07 │ ├── 29. Maximal Rectangle.cpp │ ├── 30. Rain Water Trapped.cpp │ └── 31. Min Stack.cpp ├── Day-08 │ ├── 32. First non-repeating characters.cpp │ ├── 33. MAXSPPROD.cpp │ ├── 34. Simplify Path.cpp │ └── 35. Redundant Braces.cpp ├── Day-09 │ ├── 36. Reunion of 1's.cpp │ ├── 37. Minimum Cost of ropes.cpp │ ├── 38. D. Caesar's Legions.cpp │ ├── 39. Power of Two.cpp │ ├── 40. D. Flowers.cpp │ └── 41. C. Longest Regular Bracket Sequence.cpp ├── Day-10 │ ├── 42. E. Let's Go Rolling!.cpp │ ├── 43. Sum of Digits.cpp │ ├── 44. Digit Sum k.cpp │ └── 45. Digit Frequency k.cpp ├── Day-11 │ ├── 46. Olympics Ranking.cpp │ └── 47. Problem Difficulties.cpp ├── Day-12 │ ├── 48. Check If String Is a Prefix of Array.cpp │ └── 49. Remove Stones to Minimize the Total.cpp ├── Day-13 │ ├── 50. Set Matrix Zeroes.cpp │ ├── 51. Add Two Numbers.cpp │ ├── 52. Reverse Integer.cpp │ └── 53. String to Integer (atoi).cpp ├── Day-14 │ ├── 54. Deepest Leaves Sum.cpp │ ├── 55. Sum of Nodes with Even-Valued Grandparent.cpp │ ├── 56. Binary Search Tree to Greater Sum Tre.cpp │ ├── 57. Maximum Binary Tree.cpp │ ├── 58. Construct Binary Search Tree from Preorder Traversal.cpp │ ├── 59. All Elements in Two Binary Search Trees.cpp │ └── 60. Recover a Tree From Preorder Traversal.cpp └── README.md ├── Phase - 6(60 questions) ├── Day-01 │ ├── 1. Find Pivot Index.cpp │ ├── 2. Largest Number At Least Twice of Others.cpp │ └── 3. Plus One.cpp ├── Day-02 │ ├── 4. Longest Substring Without Repeat.cpp │ ├── 5. Square Root of Integer.cpp │ └── 6. First Index.cpp ├── Day-03 │ ├── 7. International Day of Yoga.cpp │ ├── 8. BUSYMAN - I AM VERY BUSY.cpp │ └── 9. Largest square formed in a matrix.cpp ├── Day-04 │ ├── 10. Maximum Nesting Depth of the Parentheses.cpp │ ├── 11. Remove Outermost Parentheses.cpp │ ├── 12. N-ary Tree Preorder Traversal.cpp │ ├── 13. N-ary Tree Postorder Traversal.cpp │ ├── 14. Increasing Order Search Tree.cpp │ ├── 15. Final Prices With a Special Discount in a Shop.cpp │ ├── 16. Remove All Adjacent Duplicates In String.cpp │ └── 17. Build an Array With Stack Operations.cpp ├── Day-05 │ ├── 18. Baseball Game.cpp │ ├── 19. The K Weakest Rows in a Matrix.cpp │ └── 20. Last Stone Weight.cpp ├── Day-06 │ ├── 21. Count Negative Numbers in a Sorted Matrix.cpp │ ├── 22. The K Weakest Rows in a Matrix.cpp │ ├── 23. Peak Index in a Mountain Array.cpp │ ├── 24. Intersection of Two Arrays.cpp │ ├── 25. Special Array With X Elements Greater Than or Equal X.cpp │ ├── 26. Fair Candy Swap.cpp │ ├── 27. Two Sum II - Input array is sorted.cpp │ ├── 28. Kth Missing Positive Number.cpp │ ├── 29. . Intersection of Two Arrays II.cpp │ └── 30. Guess Number Higher or Lower.cpp ├── Day-07 │ ├── 31. Range Sum of BST.cpp │ ├── 32. Merge Two Binary Trees.cpp │ ├── 33. Sum of Root To Leaf Binary Numbers.cpp │ ├── 34. Maximum Depth of N-ary Tree.cpp │ ├── 35. Maximum Depth of Binary Tree.cpp │ ├── 36. Invert Binary Tree.cpp │ ├── 37. Univalued Binary Tree.cpp │ ├── 38. Binary Tree Inorder Traversal.cpp │ └── 39. Average of Levels in Binary Tree.cpp ├── Day-08 │ ├── 40. Counting Bits.cpp │ ├── 41. Divisor Game.cpp │ ├── 42. Pascal's Triangle.cpp │ ├── 43. N-th Tribonacci Number.cpp │ ├── 44. Min Cost Climbing Stairs.cpp │ ├── 45. Pascal's Triangle II.cpp │ └── 46. Get Maximum in Generated Array.cpp ├── Day-09 │ ├── 47. Best Time to Buy and Sell Stock.cpp │ ├── 48. Best Time to Buy and Sell Stock II.cpp │ ├── 49. Flipping an Image.cpp │ └── 50. Sort Array By Parity.cpp ├── Day-10 │ ├── 51. Get Maximum in Generated Array.cpp │ ├── 52. Convert Binary Number in a Linked List to Integer.cpp │ ├── 53. Middle of the Linked List.cpp │ ├── 54. Delete Node in a Linked List.cpp │ ├── 55. Reverse Linked List.cpp │ ├── 56. Design HashSet.cpp │ ├── 57. Design HashMap.cpp │ ├── 58. Merge Two Sorted Lists.cpp │ ├── 59. Remove Duplicates from Sorted List.cpp │ └── 60. Intersection of Two Linked Lists.cpp └── README.md ├── Phase - 7(60 questions) ├── Day-01 │ ├── 1. Palindrome Linked List.cpp │ ├── 2. Remove Linked List Elements.cpp │ ├── 3. Merge In Between Linked Lists.cpp │ ├── 4. Design Browser History.cpp │ └── 5. Swapping Nodes in a Linked List.cpp ├── Day-02 │ ├── 10. Add Two Numbers II.cpp │ ├── 6. Next Greater Node In Linked List.cpp │ ├── 7. Odd Even Linked List.cpp │ ├── 8. Linked List Components.cpp │ └── 9. Flatten a Multilevel Doubly Linked List.cpp ├── Day-03 │ ├── 11. Leaf-Similar Trees.cpp │ ├── 12. Convert Sorted Array to Binary Search Tree.cpp │ ├── 13. Binary Tree Postorder Traversal.cpp │ ├── 14. Binary Tree Preorder Traversal.cpp │ ├── 15. Two Sum IV - Input is a BST.cpp │ ├── 16. Construct String from Binary Tree.cpp │ ├── 17. Binary Tree Paths.cpp │ └── 18. Minimum Absolute Difference in BST.cpp ├── Day-04 │ ├── 19. Global and Local Inversions.cpp │ ├── 20. How Many Numbers Are Smaller Than the Current Number.cpp │ ├── 21. Sorting the Sentence.cpp │ ├── 22. Maximum Product Difference Between Two Pairs.cpp │ ├── 23. Sort Array By Parity.cpp │ ├── 24. Height Checker.cpp │ ├── 25. Maximum Units on a Truck.cpp │ └── 26. The K Weakest Rows in a Matrix.cpp ├── Day-05 │ ├── 27. Carvans.cpp │ ├── 28. Count Substrings.cpp │ └── 29. Operations on Tree.cpp ├── Day-06 │ ├── 30. Lowest Common Ancestor of a Binary Search Tree.cpp │ ├── 31. Sum of Left Leaves.cpp │ ├── 32. Cousins in Binary Tree.cpp │ ├── 33. Kth Largest Element in a Stream.cpp │ ├── 34. Diameter of Binary Tree.cpp │ ├── 35. Symmetric Tree.cpp │ ├── 36. Balanced Binary Tree.cpp │ ├── 37. Subtree of Another Tree.cpp │ └── 38. Path Sum.cpp ├── Day-07 │ ├── 39. Swap Nodes in Pairs.cpp │ ├── 40. Flatten Binary Tree to Linked List.cpp │ └── 41. Partition List.cpp ├── Day-08 │ ├── 42. The k-th Lexicographical String of All Happy Strings of Length n.cpp │ ├── 43. Minimum Falling Path Sum.cpp │ ├── 44. Is Subsequence.cpp │ ├── 45. Climbing Stairs.cpp │ ├── 46. Maximum Subarray.cpp │ ├── 47. All Possible Full Binary Trees.cpp │ ├── 48. Count Sorted Vowel Strings.cpp │ ├── 49. Count Square Submatrices with All Ones.cpp │ └── 50. Count Substrings That Differ by One Character.cpp ├── Day-09 │ ├── 51. Count Number of Teams.cpp │ ├── 52. Sort Integers by The Power Value.cpp │ ├── 53. Number of Good Ways to Split a String.cpp │ ├── 54. Collecting Coins Sequel.cpp │ ├── 55. Special Product List.cpp │ ├── 56. Trailing Zeros.cpp │ ├── 57. Count Nodes in Complete Binary Tree.cpp │ ├── 58. Shortest Common Supersequence.cpp │ ├── 59. Count Substrings With All 1s.cpp │ └── 60. Partition String.cpp └── README.md └── README.md /Phase - 1(60 questions)/Day-1/1. Two Sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/two-sum 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector twoSum(vector& nums, int target) { 7 | vector ans; 8 | map mp; 9 | int i=0; 10 | for(auto a:nums) 11 | mp[a]=i++; 12 | i=0; 13 | for(auto a:nums){ 14 | if(mp.find(target-a)!=mp.end() && mp[target-a]!=i){ 15 | ans.push_back(i); 16 | ans.push_back(mp[target-a]); 17 | return ans; 18 | } 19 | i++; 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-1/2. Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/container-with-most-water/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxArea(vector& height) { 7 | int x, y, maxarea=INT_MIN; 8 | x = 0, y = height.size()-1; 9 | while(x> ans; 6 | 7 | void solve(vector&vec, int s){ 8 | int n=vec.size(); 9 | if(s==n){ 10 | ans.push_back(vec); 11 | } 12 | for(int i=s;i> permute(vector& nums) { 20 | // recursion 21 | ans.clear(); 22 | auto vec=nums; 23 | solve(nums, 0); 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-1/4. Binary Tree Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/binary-tree-inorder-traversal 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | vector ans; 17 | void inorder(TreeNode* root){ 18 | if(!root) 19 | return; 20 | if(root->left) 21 | inorder(root->left); 22 | ans.push_back(root->val); 23 | if(root->right) 24 | inorder(root->right); 25 | } 26 | public: 27 | vector inorderTraversal(TreeNode* root) { 28 | ans.clear(); 29 | inorder(root); 30 | return ans; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-1/5. Generate Parentheses.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/generate-parentheses 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector ans; 6 | public: 7 | vector generateParenthesis(int n) { 8 | ans.clear(); 9 | solve(n, "", 0, 0); 10 | return ans; 11 | } 12 | void solve(int n, string s, int open, int close) { 13 | if(s.length()==2*n) { 14 | ans.push_back(s); 15 | return; 16 | } 17 | if(open subarraySum(int arr[], int n, int sr) { 10 | 11 | // Your code here 12 | // window method 13 | int s = 0, e = 0, stn = 0; 14 | int ans_s, ans_e; 15 | bool found = 0; 16 | while (s <= e && e < n) { 17 | if (stn == sr) { 18 | ans_s = s, ans_e = e; 19 | found = 1; 20 | break; 21 | } 22 | stn += arr[e]; 23 | if (stn > sr) { 24 | while (stn > sr) 25 | stn -= arr[s++]; 26 | } 27 | if (stn == sr) { 28 | ans_s = s, ans_e = e; 29 | found = 1; 30 | break; 31 | } 32 | e++; 33 | } 34 | if (!found) { 35 | return { -1}; 36 | } 37 | vector ans; 38 | ans.push_back(ans_s + 1); 39 | ans.push_back(ans_e + 1); 40 | return ans; 41 | 42 | } 43 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-12/45. Mussadi lal and stairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.codechef.com/problems/JAM11 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> t; 36 | while (t--) { 37 | int n; 38 | cin >> n; 39 | cout << dp[n] << "\n"; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-12/46. Intersection of Two Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/featured/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3660/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode(int x) : val(x), next(NULL) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 15 | auto temp = headA; 16 | while (temp) { 17 | temp->val = temp->val * -1; 18 | temp = temp->next; 19 | } 20 | temp = headB; 21 | while (temp) { 22 | if (temp->val < 0) { 23 | goto over; 24 | } 25 | temp = temp->next; 26 | } 27 | over: 28 | auto t = headA; 29 | while (t) { 30 | t->val = t->val * -1; 31 | t = t->next; 32 | } 33 | return temp; 34 | } 35 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-13/48. Maximum Intervals Overlap.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/maximum-intervals-overlap5708/1 2 | // Author - Shumbul Arifa 3 | 4 | vector findMaxGuests(int Entry[], int Exit[], int N) 5 | { 6 | // Your code goes here 7 | int guests = 0, ansg = 0, anst = 0; 8 | sort(Entry, Entry + N); 9 | sort(Exit, Exit + N); 10 | // 2 pointers 11 | int i = 0, j = 0, time = 0; 12 | while (i < N && j < N) { 13 | if (Entry[i] <= Exit[j]) { 14 | time = Entry[i++], guests++; 15 | } 16 | else time = Exit[j++], guests--; 17 | if (ansg < guests) { 18 | ansg = guests; 19 | anst = time; 20 | } 21 | } 22 | vector ans = {ansg, anst}; 23 | return ans; 24 | } -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-14/50. BFS of graph.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1 2 | // Author - Shumbul Arifa 3 | 4 | class Solution 5 | { 6 | public: 7 | //Function to return Breadth First Traversal of given graph. 8 | vectorbfsOfGraph(int V, vector adj[]) 9 | { 10 | // Code here 11 | queue q; 12 | vector ans; 13 | bool vis[V]; 14 | q.push(0); 15 | while (!q.empty()) { 16 | int curr = q.front(); 17 | ans.push_back(curr); 18 | vis[curr] = 1; 19 | q.pop(); 20 | for (int ch : adj[curr]) { 21 | if (!vis[ch]) { 22 | q.push(ch); 23 | vis[ch] = 1; 24 | } 25 | } 26 | } 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-14/51. DFS of Graph.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1 2 | // Author - Shumbul Arifa 3 | 4 | class Solution 5 | { 6 | vector vis; 7 | vector ans; 8 | void dfs(int n, vector adj[]) { 9 | vis[n] = 1; 10 | ans.push_back(n); 11 | for (int i : adj[n]) { 12 | if (!vis[i]) 13 | dfs(i, adj); 14 | } 15 | } 16 | public: 17 | //Function to return a list containing the DFS traversal of the graph. 18 | vectordfsOfGraph(int V, vector adj[]) 19 | { 20 | // Code here 21 | vis.clear(); 22 | ans.clear(); 23 | vis.resize(V); 24 | dfs(0, adj); 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-15/52. Optimal Strategy For A Game.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/optimal-strategy-for-a-game-1587115620/1# 2 | // Author - Shumbul Arifa 3 | 4 | int F[1001][1001]; 5 | int f(int arr[], int i, int j, int sum) { 6 | if (F[i][j] != -1) 7 | return F[i][j]; 8 | if (i == j - 1) 9 | return F[i][j] = max(arr[i], arr[j]); 10 | 11 | return F[i][j] = max(sum - f(arr, i + 1, j, sum - arr[i]), sum - f(arr, i, j - 1, sum - arr[j])); 12 | } 13 | 14 | long long maximumAmount(int arr[], int n) 15 | { 16 | // Your code here 17 | memset(F, -1, sizeof(F)); 18 | return f(arr, 0, n - 1, accumulate(arr, arr + n, 0)); 19 | } -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-15/53. Increasing Path in Matrix.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/increasing-path-in-matrix/ 2 | // Author - Shumbul Arifa 3 | 4 | bool mainans = false; 5 | bool path(vector > &A, int i, int j, int n1, int n2) { 6 | // cout< A[i][j] && !mainans) 13 | ans = path(A, i + 1, j, n1, n2); 14 | if (j + 1 < n2 && A[i][j + 1] > A[i][j] && !mainans) 15 | ans = ans || path(A, i, j + 1, n1, n2); 16 | return ans || mainans; 17 | } 18 | 19 | int Solution::solve(vector > &A) { 20 | // if path doesn't exist, ans is -1 21 | // if path exists, based the the given conditions, ans is n1+n2-1 22 | // we just have to check if the path exists or not 23 | int n1 = A.size(), n2 = A[0].size(); 24 | mainans = false; 25 | if (path(A, 0, 0, n1, n2) || mainans) 26 | return n1 + n2 - 1; 27 | return -1; 28 | } -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-16/55. Delete Operation for Two Strings.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3734/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minDistance(string word1, string word2) { 7 | int n1 = word1.length(), n2 = word2.length(); 8 | if (!n1) 9 | return n2; 10 | if (!n2) 11 | return n1; 12 | int dp[n1 + 1][n2 + 1]; 13 | memset(dp, 0, sizeof(dp)); 14 | int maxl = 0; 15 | for (int i = 1; i <= n1; i++) { 16 | for (int j = 1; j <= n2; j++) { 17 | if (word1[i - 1] == word2[j - 1]) { 18 | dp[i][j] = dp[i - 1][j - 1] + 1; 19 | maxl = max(maxl, dp[i][j]); 20 | } 21 | else { 22 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 23 | } 24 | } 25 | } 26 | return (n1 - maxl + n2 - maxl); 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-16/56. Jump Game II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3732/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector dp; 6 | int minsans; 7 | int f(vector& nums, int curr) { 8 | dp[0] = 0; 9 | for (int i = 0; i < nums.size(); i++) { 10 | for (int j = 1; j <= nums[i] && i + j < nums.size(); j++) { 11 | dp[i + j] = min(dp[i + j], dp[i] + 1); 12 | } 13 | } 14 | return dp[nums.size() - 1]; 15 | } 16 | public: 17 | int jump(vector& nums) { 18 | dp.resize(nums.size()); 19 | for (int i = 0; i < nums.size(); i++) 20 | dp[i] = INT_MAX; 21 | return f(nums, 0); 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-16/57. Non-decreasing Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3731/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool checkPossibility(vector& nums) { 7 | bool f = 0; 8 | for (int i = 0; i < nums.size() - 1; i++) { 9 | if (nums[i] > nums[i + 1]) { 10 | if (i == 0) { 11 | f = 1; 12 | nums[i] = nums[i + 1]; 13 | continue; 14 | } 15 | if (f) 16 | return false; 17 | f = 1; 18 | if (nums[i + 1] >= nums[i - 1] && i > 0 && i < nums.size() - 1) 19 | nums[i] = nums[i + 1]; 20 | else if (nums[i + 1] < nums[i - 1] && i > 0 && i < nums.size() - 1) 21 | nums[i + 1] = nums[i]; 22 | } 23 | } 24 | return true; 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-16/58. Running Sum of 1d Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3730/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector runningSum(vector& nums) { 7 | int n = nums.size(); 8 | vector ans(n); 9 | ans[0] = nums[0]; 10 | for (int i = 1; i < n; i++) 11 | ans[i] = ans[i - 1] + nums[i]; 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-17/59. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minDifference(vector& nums) { 7 | sort(nums.begin(), nums.end()); 8 | int n = nums.size(); 9 | if (n <= 3) 10 | return 0; 11 | // min diff bw 1st three and last three 12 | int ans = nums[n - 1] - nums[0]; 13 | for (int i = 0, j = n - 4; j < n; j++, i++) { 14 | ans = min(ans, nums[j] - nums[i]); 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-2/10. Kth Largest Element in an Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/kth-largest-element-in-an-array/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int findKthLargest(vector& nums, int k) { 7 | priority_queue q; 8 | for (auto num : nums) { 9 | q.push(num); 10 | } 11 | while (k > 1) { 12 | q.pop(); 13 | k--; 14 | } 15 | return q.top(); 16 | } 17 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-2/11. Palindrome Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link -https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3693/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | ListNode* tmp; 16 | bool res; 17 | 18 | void rec(ListNode* head) { 19 | if (!head || res == 0) 20 | return; 21 | if (head->next && tmp != head->next) 22 | rec(head->next); 23 | if (tmp->val != head->val) 24 | res = 0; 25 | tmp = tmp->next; 26 | } 27 | public: 28 | bool isPalindrome(ListNode* head) { 29 | tmp = head; 30 | res = 1; 31 | rec(head); 32 | return res; 33 | } 34 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-2/7. Kth Smallest Element in a BST.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/kth-smallest-element-in-a-bst 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | int ans, i, k; 17 | 18 | void inorder(TreeNode* root) { 19 | if (!root || i > k) 20 | return; 21 | if (root->left) 22 | inorder(root->left); 23 | i++; 24 | if (i == k) { 25 | ans = root->val; 26 | return; 27 | } 28 | if (root->right) 29 | inorder(root->right); 30 | } 31 | public: 32 | int kthSmallest(TreeNode* root, int K) { 33 | i = 0, k = K; 34 | inorder(root); 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-2/8. Top K Frequent Elements.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/top-k-frequent-elements/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | map mp; 6 | public: 7 | vector topKFrequent(vector& nums, int k) { 8 | // sort based on their frequencies 9 | for (auto num : nums) { 10 | mp[num]++; 11 | } 12 | vector> q; 13 | for (auto a : mp) { 14 | q.push_back(make_pair(a.first, a.second)); 15 | } 16 | sort(q.begin(), q.end(), [](const pair &s1, const pair &s2) { 17 | return s1.second > s2.second; 18 | }); 19 | q.resize(k); 20 | vector v; 21 | for (int i = 0; i < k; i++) 22 | v.push_back(q[i].first); 23 | return v; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-2/9. Product of Array Except Self.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/product-of-array-except-self 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector productExceptSelf(vector& nums) { 7 | int n = nums.size(), l; 8 | vector ans(n, 1); 9 | l = nums[0]; 10 | for (int i = 1; i < n; i++) { 11 | ans[i] *= l; 12 | l *= nums[i]; 13 | } 14 | int r = nums[n - 1]; 15 | for (int i = n - 2; i >= 0; i--) { 16 | ans[i] *= r; 17 | r *= nums[i]; 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-3/12. Remove loop in Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/remove-loop-in-linked-list 2 | // Author - Shumbul Arifa 3 | 4 | class Solution 5 | { 6 | public: 7 | //Function to remove a loop in the linked list. 8 | void removeLoop(Node* head) 9 | { 10 | // code here 11 | // just remove the loop without losing any nodes 12 | bool loop = 0; 13 | auto fast = head, slow = head; 14 | while (fast && slow && fast->next) { 15 | fast = fast->next->next; 16 | slow = slow->next; 17 | if (fast == slow) { 18 | loop = 1; 19 | break; 20 | } 21 | } 22 | if (!loop) 23 | return; 24 | // otherwise, loop detected! 25 | 26 | auto another = head; 27 | while (slow != another) { 28 | slow = slow->next; 29 | another = another->next; 30 | } 31 | 32 | while (another->next != slow) { 33 | another = another->next; 34 | } 35 | another->next = NULL; 36 | 37 | } 38 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-3/13. Non-decreasing Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/non-decreasing-array 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool checkPossibility(vector& nums) { 7 | int dec = 0; 8 | bool decrease = 0; 9 | for (int i = 1; i < nums.size(); i++) { 10 | decrease = 0; 11 | if (nums[i - 1] > nums[i]) 12 | dec++, decrease = 1; 13 | if (decrease && i > 1 && nums[i - 2] > nums[i]) { 14 | if (i < nums.size() - 1 && nums[i - 1] <= nums[i + 1]) 15 | continue; 16 | if (i != nums.size() - 1) 17 | dec++; 18 | } 19 | } 20 | return dec < 2; 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-3/14. Count Primes.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/count-primes 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | bool prime[5000001]; 6 | 7 | void ALGO_sieve(int n) { 8 | memset(prime, true, n + 2); 9 | for (int p = 2; p * p <= n ; p++) { 10 | if (prime[p] == true) { 11 | for (int i = p * p; i <= n; i += p) 12 | prime[i] = false; 13 | } 14 | } 15 | } 16 | public: 17 | int countPrimes(int n) { 18 | if (n < 2) 19 | return 0; 20 | ALGO_sieve(n); 21 | int ans = 0; 22 | for (int i = 2; i < n; i++) { 23 | if (prime[i]) 24 | ans++; 25 | } 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-4/15. Partition Equal Subset Sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/partition-equal-subset-sum 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool canPartition(vector& nums) { 7 | // similar to subset problem 8 | int Sum = accumulate(nums.begin(), nums.end(), 0); 9 | if (Sum % 2) 10 | return 0; 11 | // finding a subset of sum Sum/2 : 12 | int k = Sum / 2; 13 | if (*max_element(nums.begin(), nums.end()) > k) 14 | return 0; 15 | int n = nums.size(), dp[k + 1]; 16 | memset(dp, 0, sizeof dp); 17 | dp[0] = 1; 18 | for (int i = 0; i < nums.size(); i++) { 19 | for (int j = k; j >= nums[i]; j--) { 20 | if (dp[j] == 0) { 21 | if (dp[j - nums[i]]) 22 | dp[j] = 1; 23 | } 24 | } 25 | } 26 | return dp[k]; 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-4/16. Minimum Falling Path Sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/minimum-falling-path-sum/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | class Solution { 6 | public: 7 | int minFallingPathSum(vector>& matrix) { 8 | int inf = 100001; 9 | int n = matrix.size(); 10 | int m = matrix[0].size(); 11 | if (n == 1) 12 | return matrix[0][0]; 13 | // vector> dp(n,vector(m)); 14 | int dp[n][m]; 15 | for (int i = 0; i < m; i++) { 16 | dp[n - 1][i] = matrix[n - 1][i]; 17 | } 18 | int mini = inf; 19 | for (int i = n - 2; i >= 0; i--) { 20 | for (int j = 0; j < m; j++) { 21 | 22 | dp[i][j] = dp[i + 1][j]; 23 | if (j - 1 >= 0) 24 | dp[i][j] = min(dp[i + 1][j - 1], dp[i][j]); 25 | if (j + 1 < n) 26 | dp[i][j] = min(dp[i + 1][j + 1], dp[i][j]); 27 | dp[i][j] += matrix[i][j]; 28 | if (i == 0) { 29 | mini = min(mini, dp[i][j]); 30 | } 31 | 32 | } 33 | } 34 | return mini; 35 | 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-4/17. Minimum Path Sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/minimum-path-sum/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minPathSum(vector>& grid) { 7 | int inf = 10001; 8 | int n = grid.size(), m = grid[0].size(); 9 | int dp[n][m + 2]; 10 | for (int i = 0; i < n; i++) { 11 | dp[i][0] = inf; 12 | dp[i][m + 1] = inf; 13 | } 14 | dp[n - 1][m] = grid[n - 1][m - 1]; 15 | 16 | for (int j = m - 1; j > 0; j--) 17 | dp[n - 1][j] = grid[n - 1][j - 1] + dp[n - 1][j + 1]; 18 | 19 | for (int i = n - 2; i >= 0; i--) { 20 | for (int j = m; j > 0; j--) { 21 | dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) + grid[i][j - 1]; 22 | } 23 | } 24 | for (int i = 0; i < n; i++) { 25 | for (int j = 0; j < m + 2; j++) { 26 | cout << dp[i][j] << " "; 27 | } 28 | cout << "\n"; 29 | } 30 | return dp[0][1]; 31 | } 32 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-4/18. Longest Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/longest-palindromic-substring 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string longestPalindrome(string s) { 7 | int l = s.length(); 8 | if (l < 2) 9 | return s; 10 | bool dp[l + 1][l + 1]; 11 | memset(dp, 0, sizeof dp); 12 | string ans = ""; 13 | for (int i = 0; i <= l; i++) { 14 | dp[0][i] = 1; 15 | if (i) 16 | dp[1][i] = 1; 17 | } 18 | ans = s.substr(0, 1); 19 | for (int i = 2; i <= l; i++) { 20 | for (int j = i; j <= l; j++) { 21 | if (dp[i - 2][j - 1] && s[j - 1] == s[j - 1 - i + 1]) { 22 | dp[i][j] = 1; 23 | if (i > ans.length()) 24 | ans = s.substr(j - i, i); 25 | } 26 | } 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-4/19. Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/unique-paths/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int uniquePaths(int m, int n) { 7 | int dp[m][n]; 8 | for (int i = 0; i < m; i++) { 9 | dp[i][0] = 1; 10 | } 11 | for (int i = 0; i < n; i++) { 12 | dp[0][i] = 1; 13 | } 14 | 15 | for (int i = 1; i < m; i++) { 16 | for (int j = 1; j < n; j++) { 17 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 18 | } 19 | } 20 | 21 | return dp[m - 1][n - 1]; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-4/20. k-th Lexicographical String of All Happy Strings of Length n.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector sets; 6 | string allowed; 7 | void fill(int n, string curr) { 8 | if (curr.length() == n) { 9 | sets.push_back(curr); 10 | return; 11 | } 12 | 13 | for (int i = 0; i < allowed.length(); i++) { 14 | if (curr == "") { 15 | curr += allowed[i]; 16 | fill(n, curr); 17 | curr.pop_back(); 18 | } 19 | else { 20 | if (curr[curr.length() - 1] != allowed[i]) { 21 | curr += allowed[i]; 22 | fill(n, curr); 23 | curr.pop_back(); 24 | } 25 | } 26 | } 27 | 28 | } 29 | public: 30 | string getHappyString(int n, int k) { 31 | allowed = "abc"; 32 | fill(n, ""); 33 | // generating sets 34 | sort(sets.begin(), sets.end()); 35 | if (sets.size() < k) 36 | return ""; 37 | return sets[k - 1]; 38 | } 39 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-5/21. Partitioning Into Minimum Number Of Deci-Binary Numbers.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minPartitions(string n) { 7 | // find the pattern taking values from 0 to 110 8 | return *max_element(n.begin(), n.end()) - '0'; 9 | } 10 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-5/22. Find Center of Star Graph.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/find-center-of-star-graph 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int findCenter(vector>& edges) { 7 | set st; 8 | vector v1 = edges[0], v2 = edges[1]; 9 | for (int i = 0; i < 2; i++) { 10 | st.insert(v1[i]); 11 | } 12 | for (int i = 0; i < 2; i++) { 13 | if (st.find(v2[i]) != st.end()) 14 | return v2[i]; 15 | } 16 | return -1; 17 | } 18 | }; 19 | 20 | // Method -2 21 | class Solution { 22 | public: 23 | int findCenter(vector>& e) { 24 | return e[0][0] == e[1][0] ? e[0][0] : (e[0][0] == e[1][1] ? e[0][0] : e[0][1]); 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-6/24. First Bad Version.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/first-bad-version 2 | // Author - Shumbul Arifa 3 | 4 | // The API isBadVersion is defined for you. 5 | // bool isBadVersion(int version); 6 | 7 | class Solution { 8 | public: 9 | int firstBadVersion(int n) { 10 | // using bin search 11 | int l, h, mid; 12 | l = 1, h = n; 13 | while (l <= h) { 14 | if (l == h) { 15 | return l; 16 | } 17 | mid = (h - l) / 2 + l; 18 | if (isBadVersion(mid)) { 19 | h = mid; 20 | } 21 | else { 22 | l = mid + 1; 23 | } 24 | } 25 | return -1; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-6/25. Jewels and Stones.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3317/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int numJewelsInStones(string jewels, string stones) { 7 | int ans = 0; 8 | for (char c : stones) { 9 | if (jewels.find(c) != jewels.npos) 10 | ans++; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-6/25. Ransom Note.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3318/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool canConstruct(string ransomNote, string magazine) { 7 | map mp; 8 | for (char c : magazine) { 9 | mp[c]++; 10 | } 11 | for (char c : ransomNote) { 12 | if (mp.find(c) == mp.end()) { 13 | return false; 14 | } 15 | mp[c]--; 16 | if (mp[c] == 0) 17 | mp.erase(c); 18 | } 19 | return true; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-6/26. First Unique Character in a String.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3320/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int firstUniqChar(string s) { 7 | string t = s, saw = ""; 8 | for (int i = 0; i < s.length(); i++) { 9 | t.erase(t.begin()); 10 | char c = s[i]; 11 | if (t.find(c) == t.npos && saw.find(c) == saw.npos) 12 | return i; 13 | saw += c; 14 | } 15 | return -1; 16 | } 17 | }; -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-6/27. Number Complement.py: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3319/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution: 5 | def findComplement(self, n: int) -> int: 6 | c = "" 7 | b = bin(n).replace("0b","") 8 | b = str(b) 9 | for i in range(0,len(b)): 10 | if b[i] == '0': 11 | c = c + "1" 12 | else: 13 | c = c + "0" 14 | # print(" b ", b, "\n c ", c) 15 | return int(c,2) -------------------------------------------------------------------------------- /Phase - 1(60 questions)/Day-9/36. A. Review Site.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://codeforces.com/contest/1511/problem/A 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> t; 23 | while (t--) { 24 | int n; cin >> n; 25 | int ar[n], ans = 0; 26 | for0(i, n) { 27 | cin >> ar[i]; 28 | if (ar[i] != 2) 29 | ans++; 30 | } 31 | cout << ans << "\n"; 32 | } 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-1/1. Permutations of a String.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/permutations-of-a-string/1/?track=md-backtracking&batchId=144 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector ans; 6 | void solve(string s, int b) { 7 | if (b == s.length() - 1) 8 | ans.push_back(s); 9 | for (int i = b; i < s.length(); i++) { 10 | swap(s[i], s[b]); 11 | solve(s, b + 1); 12 | swap(s[i], s[b]); 13 | } 14 | } 15 | public: 16 | vector permute(string s) { 17 | // code here 18 | ans.clear(); 19 | solve(s, 0); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-10/24. Find All Duplicates in an Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/find-all-duplicates-in-an-array/ 2 | // Author - Shumbul Arifa 3 | 4 | lass Solution { 5 | public: 6 | vector findDuplicates(vector& nums) { 7 | auto ar = nums; 8 | int n = ar.size(); 9 | vector ans; 10 | int m = -1; 11 | for (int i = 0; i < n; i++) { 12 | if (ar[abs(ar[i]) - 1] > 0) 13 | ar[abs(ar[i]) - 1] = ar[abs(ar[i]) - 1] * m; 14 | else { 15 | ans.push_back(abs(ar[i])); 16 | } 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-10/25. Remove Duplicates from Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 2 | 3 | class Solution { 4 | public: 5 | int removeDuplicates(vector& nums) { 6 | int i = 0; 7 | for (int n : nums) 8 | if (i < 2 || n > nums[i - 2]) 9 | nums[i++] = n; 10 | return i; 11 | } 12 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-10/26. Best Time to Buy and Sell Stock IV.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(int k, vector& prices) { 7 | int n = prices.size(); 8 | if (n < 2) 9 | return 0; 10 | k = min(n, k); 11 | int dp[k + 1][n]; 12 | for (int i = 0; i <= k; i++) 13 | for (int j = 0; j < n; j++) 14 | dp[i][j] = 0; 15 | 16 | for (int i = 1; i <= k; i++) { 17 | for (int j = 1; j < n; j++) { 18 | int sale = dp[i][j - 1]; 19 | for (int p = 0; p < j; p++) { 20 | int pr = prices[j] - prices[p]; 21 | sale = max(sale, pr + dp[i - 1][p]); 22 | } 23 | dp[i][j] = sale; 24 | } 25 | } 26 | 27 | for (int i = 0; i <= k; i++) { 28 | for (int j = 0; j < n; j++) 29 | cout << dp[i][j] << " "; 30 | cout << "\n"; 31 | } 32 | 33 | return dp[k][n - 1]; 34 | } 35 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-10/27. Longest Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/longest-palindromic-substring/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string longestPalindrome(string s) { 7 | int l = s.length(); 8 | if (l < 2) 9 | return s; 10 | bool dp[l + 1][l + 1]; 11 | memset(dp, 0, sizeof dp); 12 | string ans = ""; 13 | for (int i = 0; i <= l; i++) { 14 | dp[0][i] = 1; 15 | if (i) 16 | dp[1][i] = 1; 17 | } 18 | ans = s.substr(0, 1); 19 | for (int i = 2; i <= l; i++) { 20 | for (int j = i; j <= l; j++) { 21 | if (dp[i - 2][j - 1] && s[j - 1] == s[j - 1 - i + 1]) { 22 | dp[i][j] = 1; 23 | if (i > ans.length()) 24 | ans = s.substr(j - i, i); 25 | } 26 | } 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-10/28. Longest Palindromic Subsequence.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/longest-palindromic-subsequence/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int longestPalindromeSubseq(string s) { 7 | int n = s.length(), dp[n][n]; 8 | for (int i = 0; i < n; i++) 9 | for (int j = 0; j < n; j++) 10 | dp[i][j] = -1; 11 | 12 | for (int i = 0; i < n; i++) { 13 | dp[i][i] = 1; 14 | } 15 | for (int i = 0; i < n - 1; i++) { 16 | if (s[i] != s[i + 1]) 17 | dp[i][i + 1] = 1; 18 | else 19 | dp[i][i + 1] = 2; 20 | } 21 | for (int d = 3; d <= n; d++) { 22 | for (int i = 0; i <= n - d; i++) { 23 | int j = i + d - 1; 24 | if (s[i] == s[j]) { 25 | dp[i][j] = dp[i + 1][j - 1] + 2; 26 | } 27 | else 28 | dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]); 29 | } 30 | } 31 | // for(int i=0;i& nums) { 7 | map mp; 8 | for (int i = 0; i < nums.size(); i++) { 9 | mp[nums[i]]++; 10 | } 11 | int ans = 0; 12 | for (auto i : mp) { 13 | if (i.second > 1) 14 | ans++; 15 | } 16 | return ans; 17 | } 18 | int findPairs(vector& nums, int k) { 19 | if (k == 0) 20 | return handle0(nums); 21 | set st, ap; 22 | for (int i = 0; i < nums.size(); i++) { 23 | st.insert(nums[i]); 24 | } 25 | for (int i : st) { 26 | if (st.find(k + i) != st.end()) 27 | ap.insert(i); 28 | if (st.find(i - k) != st.end()) 29 | ap.insert(i); 30 | st.erase(i); 31 | } 32 | return ap.size(); 33 | -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-12/33. Majority Element.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/majority-element/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool isSubsequence(string s, string t) { 7 | int n = s.length(), m = t.length(); 8 | if (n > m) 9 | return 0; 10 | int i, j; 11 | for (i = 0, j = 0; i < n, j < m;) { 12 | if (s[i] == t[j]) { 13 | i++, j++; 14 | } 15 | else 16 | j++; 17 | } 18 | if (i == n) 19 | return 1; 20 | return 0; 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-12/34. Subarray Product Less Than K.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/subarray-product-less-than-k/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int numSubarrayProductLessThanK(vector& nums, int k) { 7 | if (k == 0) 8 | return 0; 9 | int p = 1, cnt = 0; 10 | for (int i = 0, j = 0; j < nums.size(); j++) { 11 | p *= nums[j]; 12 | while (p >= k && i <= j) 13 | p /= nums[i++]; 14 | cnt += (j - i + 1); 15 | } 16 | return cnt; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-12/35. Jump Game.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/jump-game/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool canJump(vector& nums) { 7 | int n = nums.size(), i, maxj; 8 | for (i = 0, maxj = 0; i < n && i <= maxj; i++) { 9 | maxj = max(maxj, i + nums[i]); 10 | } 11 | // i>maxj condition arises when reaching i isn't possible. 12 | return i == n; 13 | } 14 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-13/36. Jump Game III.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/jump-game-iii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool canReach(vector& nums, int start) { 7 | int n = nums.size(); 8 | if (start < 0 || start >= n || nums[start] < 0) 9 | return false; 10 | if (nums[start] == 0) 11 | return true; 12 | int i = start; 13 | nums[i] *= -1; 14 | return canReach(nums, i + nums[i]) || canReach(nums, i - nums[i]); 15 | return false; 16 | } 17 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-13/37. 0-1 Knapsack.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/0-1-knapsack/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::solve(vector &A, vector &B, int C) { 5 | int n = A.size(); 6 | if (n == 0 || C == 0) 7 | return 0; 8 | int dp[n + 1][C + 1]; 9 | for (int i = 0; i <= n; i++) 10 | dp[i][0] = 0; 11 | 12 | for (int i = 1; i <= C; i++) 13 | dp[0][i] = 0; 14 | 15 | for (int i = 1; i <= n; i++) { 16 | for (int j = 1; j <= C; j++) { 17 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 18 | if (B[i - 1] <= j) 19 | dp[i][j] = max(dp[i][j], dp[i - 1][j - B[i - 1]] + A[i - 1]); 20 | } 21 | } 22 | return dp[n][C]; 23 | } -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-14/39. 3Sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/3sum/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector> threeSum(vector& nums) { 7 | vector> ans; 8 | if (nums.size() < 3) return ans; 9 | map mp; 10 | int i = 0; 11 | sort(nums.begin(), nums.end()); 12 | for (auto v : nums) { 13 | mp[v] = i++; 14 | } 15 | vector v; 16 | for (int i = 0; i < nums.size(); i++) { 17 | if (i > 0 && nums[i - 1] == nums[i]) continue; 18 | for (int j = i + 1; j < nums.size(); j++) { 19 | int sum2 = nums[i] + nums[j]; 20 | if (mp.find(sum2 * -1) != mp.end()) { 21 | if (mp[sum2 * -1] > j) { 22 | v = {nums[i], nums[j], sum2 * -1}; 23 | ans.push_back(v); 24 | } 25 | } 26 | } 27 | } 28 | //delete dups from ans 29 | set> s; 30 | for (auto a : ans) { 31 | s.insert(a); 32 | } 33 | ans.clear(); 34 | for (auto a : s) { 35 | ans.push_back(a); 36 | } 37 | return ans; 38 | } 39 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-14/40. Squares of a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/squares-of-a-sorted-array/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector sortedSquares(vector& nums) { 7 | int i = 0, j = nums.size() - 1, k = j; 8 | vector ans(nums.size()); 9 | while (i <= j) { 10 | if (abs(nums[i]) >= abs(nums[j])) 11 | ans[k--] = nums[i] * nums[i++]; 12 | else 13 | ans[k--] = nums[j] * nums[j--]; 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-14/41. Maximum Subarray.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-subarray/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxSubArray(vector& nums) { 7 | int curr = -100001, maxi = curr; 8 | for (auto i : nums) { 9 | curr = max(i, curr + i); 10 | maxi = max(maxi, curr); 11 | } 12 | return maxi; 13 | } 14 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-15/42. Repeating Sub-Sequence.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/repeating-subsequence/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::anytwo(string A) { 5 | int n = A.length(), dp[n + 1][n + 1]; 6 | for (int i = 0; i <= n; i++) 7 | dp[i][0] = dp[0][i] = 0; 8 | for (int i = 1; i <= n; i++) { 9 | for (int j = 1; j <= n; j++) { 10 | if (i != j && A[i - 1] == A[j - 1]) 11 | dp[i][j] = dp[i - 1][j - 1] + 1; 12 | else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 13 | } 14 | } 15 | // for(int i=0;i<=n;i++){ 16 | // for(int j=0;j<=n;j++) 17 | // cout<= 2; 21 | } -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-15/43. Distinct Subsequences.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/distinct-subsequences/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::numDistinct(string A, string B) { 5 | int n = A.length(), m = B.length(); 6 | int dp[m + 1][n + 1]; 7 | for (int i = 0; i <= n; i++) 8 | dp[0][i] = 1; 9 | for (int j = 1; j <= m; j++) 10 | dp[j][0] = 0; 11 | 12 | for (int i = 1; i <= m; i++) { 13 | for (int j = 1; j <= n; j++) { 14 | if (B[i - 1] == A[j - 1]) 15 | dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1]; 16 | else 17 | dp[i][j] = dp[i][j - 1]; 18 | } 19 | } 20 | // for(int i=0;i<=m;i++){ 21 | // for(int j=0;j<=n;j++) 22 | // cout< &A, const vector &B, const vector &C) { 5 | int n1 = *max_element(A.begin(), A.end()), n2 = B.size(), n3 = C.size(); 6 | if (n2 != n3) 7 | return -1; 8 | int dp[n2 + 1][n1 + 1]; 9 | for (int i = 0; i <= n2; i++) 10 | dp[i][0] = 0; 11 | for (int i = 1; i <= n1; i++) 12 | dp[0][i] = 100000001; 13 | for (int i = 1; i <= n2; i++) { 14 | for (int j = 1; j <= n1; j++) { 15 | if (B[i - 1] > j) 16 | dp[i][j] = dp[i - 1][j]; 17 | else { 18 | dp[i][j] = min(dp[i][j - B[i - 1]] + C[i - 1], dp[i - 1][j]); 19 | } 20 | } 21 | } 22 | // for(int i=0;i<=n2;i++) { 23 | // for(int j=0;j<=n1;j++) 24 | // cout< &ar) { 6 | auto A = ar; 7 | int n = A.size(); 8 | if (n < 3) 9 | return -1; 10 | sort(A.begin(), A.end()); 11 | vector poss; 12 | poss.push_back(A[0]*A[1]*A[n - 1]); 13 | poss.push_back(A[n - 1]*A[n - 2]*A[n - 3]); 14 | return *max_element(poss.begin(), poss.end()); 15 | } -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-16/48. Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/rotate-image/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | void rotate(vector>& matrix) { 7 | // Step - 1 8 | // flip across diagonal - 2 9 | int n = matrix.size(); 10 | for (int i = 0; i < n - 1; i++) { 11 | for (int j = 0; i + j + 1 < n; j++) { 12 | swap(matrix[i][j], matrix[n - j - 1][n - i - 1]); 13 | } 14 | } 15 | for (int i = 0; i < n; i++) { 16 | for (int j = 0; j < n; j++) 17 | cout << matrix[i][j] << " "; 18 | cout << "\n"; 19 | } 20 | // Step - 2 21 | // flip across horizontal middle row (row -> if exists ) 22 | for (int i = 0; i < n / 2; i++) { 23 | for (int j = 0; j < n; j++) { 24 | swap(matrix[i][j], matrix[n - i - 1][j]); 25 | } 26 | } 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-16/49. Remove Covered Intervals.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/remove-covered-intervals/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int removeCoveredIntervals(vector>& intervals) { 7 | int removed = 0; 8 | sort(intervals.begin(), intervals.end(), [](const auto & s1, const auto & s2) { 9 | if (s1[0] != s2[0]) 10 | return s1[0] <= s2[0]; 11 | return s1[1] >= s2[1]; 12 | }); 13 | int last = -1; 14 | for (auto interval : intervals) { 15 | if (interval[1] <= last) 16 | removed ++; 17 | else 18 | last = interval[1]; 19 | } 20 | return intervals.size() - removed; 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-17/50. Length of Longest Subsequence.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/length-of-longest-subsequence/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::longestSubsequenceLength(const vector &ar) { 5 | int n = ar.size(); 6 | if (n == 0) 7 | return 0; 8 | int inc[n], dec[n]; 9 | int dp[n]; 10 | dp[0] = 1; 11 | for (int i = 1; i < n; i++) { 12 | dp[i] = 1; 13 | for (int j = 0; j < i; j++) 14 | if (ar[i] > ar[j] && dp[i] < dp[j] + 1) 15 | dp[i] = dp[j] + 1; 16 | } 17 | int dp2[n]; 18 | dp2[n - 1] = 1; 19 | for (int i = n - 2; i >= 0; i--) { 20 | dp2[i] = 1; 21 | for (int j = n - 1; j >= i; j--) 22 | if (ar[i] > ar[j] && dp2[i] < dp2[j] + 1) 23 | dp2[i] = dp2[j] + 1; 24 | } 25 | 26 | // for(int i=0;i>& mat, int target) { 7 | // step -1 8 | // traverse in the last column, and select min row value with val greater than target 9 | int row = 0; 10 | int n = mat.size(), m = mat[0].size(); 11 | for (int i = 0; i < n; i++) { 12 | if (mat[i][m - 1] >= target) { 13 | row = i; 14 | break; 15 | } 16 | } 17 | auto v = mat[row]; 18 | return binary_search(v.begin(), v.end(), target); 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-18/53. Spiral Matrix.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/spiral-matrix/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector spiralOrder(vector>& A) { 7 | int i, k = 0, l = 0; 8 | int m = A.size(), n = A[0].size(); 9 | vector temp; 10 | while (k < m && l < n) { 11 | for (i = l; i < n; ++i) { 12 | temp.push_back(A[k][i]); 13 | } 14 | k++; 15 | for (i = k; i < m; ++i) { 16 | temp.push_back(A[i][n - 1]); 17 | } 18 | n--; 19 | if (k < m) { 20 | for (i = n - 1; i >= l; --i) { 21 | temp.push_back(A[m - 1][i]); 22 | } 23 | m--; 24 | } 25 | if (l < n) { 26 | for (i = m - 1; i >= k; --i) { 27 | temp.push_back(A[i][l]); 28 | } 29 | l++; 30 | } 31 | } 32 | return temp; 33 | } 34 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-19/54. Lowest Common Ancestor of a Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | 14 | class Solution { 15 | public: 16 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 17 | if (root == NULL || root == p || root == q) 18 | return root; 19 | else if (root->val > max(p->val, q->val)) 20 | return lowestCommonAncestor(root->left, p, q); 21 | else if (root->val < min(p->val, q->val)) 22 | return lowestCommonAncestor(root->right, p, q); 23 | else return root; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-19/55. Lowest Common Ancestor of a Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 16 | if (root == NULL || root == p || root == q) 17 | return root; 18 | auto a = lowestCommonAncestor(root-> left, p, q); 19 | auto b = lowestCommonAncestor(root-> right, p, q); 20 | if (!(a == NULL || b == NULL)) 21 | return root; 22 | if (a == NULL && b == NULL) 23 | return NULL; 24 | 25 | return a != NULL ? a : b; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-2/4. Kadane's Algorithm.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/ 2 | // Author - Shumbul Arifa 3 | 4 | // arr: input array 5 | // n: size of array 6 | //Function to find the sum of contiguous subarray with maximum sum. 7 | int maxSubarraySum(int arr[], int n) { 8 | 9 | // Your code here 10 | int meh = 0, msf = INT_MIN; 11 | // max ending here, and max so far 12 | for (int i = 0; i < n; i++) { 13 | meh = meh + arr[i]; 14 | if (msf < meh) 15 | msf = meh; 16 | if (meh < 0) 17 | meh = 0; 18 | } 19 | return msf; 20 | } -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-20/58. Min Cost Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/603/week-1-june-1st-june-7th/3770/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minCostClimbingStairs(vector& cost) { 7 | int n = cost.size(), dp[n + 2]; 8 | if (n < 2) 9 | return 0; 10 | dp[0] = 0; 11 | dp[1] = 0; 12 | dp[2] = 0; 13 | for (int i = 2; i <= n; i++) { 14 | dp[i + 1] = min(dp[i] + cost[i - 1], dp[i - 1] + cost[i - 2]); 15 | } 16 | return dp[n + 1]; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-21/60. Count Substrings That Differ by One Character.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/count-substrings-that-differ-by-one-character/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int countSubstrings(string s, string t) { 7 | // Trie solution is also possible, but much more mode is required. This approach is time efficient. 8 | int res = 0; 9 | for (int i = 0; i < s.size(); ++i) { 10 | for (int j = 0; j < t.size(); ++j) { 11 | int miss = 0; 12 | for (int pos = 0; i + pos < s.size() && j + pos < t.size(); ++pos) { 13 | if (s[i + pos] != t[j + pos] && ++miss > 1) 14 | break; 15 | res += miss; 16 | } 17 | } 18 | } 19 | return res; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-3/6. Word Break.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/word-break/submissions/ 2 | // Link - https://practice.geeksforgeeks.org/problems/word-break1352/1# 3 | // Author - Shumbul Arifa 4 | 5 | class Solution { 6 | public: 7 | bool wordBreak(string s, vector& wd) { 8 | if (find(wd.begin(), wd.end(), s) != wd.end()) 9 | return true; 10 | int n = s.length(); 11 | bool dp[n + 1]; 12 | for (int i = 1; i <= n; i++) 13 | dp[i] = false; 14 | dp[0] = true; 15 | 16 | for (int l = 1; l <= n; l++) { 17 | for (int i = 0; i < l; i++) { 18 | string t = s.substr(i, l - i); 19 | if (dp[i] && (find(wd.begin(), wd.end(), t) != wd.end() || t == "")) { 20 | dp[l] = true; 21 | break; 22 | } 23 | } 24 | } 25 | 26 | return dp[n]; 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-4/10. Pow(x, n).cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/powx-n/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | #define ll double 7 | double myPow(double a, int n) { 8 | if (a == 1 || a == -1 && n % 2 == 0) 9 | return 1.00000; 10 | if (a == -1 && n % 2) 11 | return -1.00000; 12 | if (n < 0) { 13 | if (n == -2147483648) 14 | return 0; 15 | a = 1.0 / (a * 1.0); 16 | n *= -1; 17 | } 18 | ll p = 1; 19 | while (n > 0) { 20 | if (n % 2) { 21 | p = p * a; 22 | } 23 | n >>= 1; 24 | a *= a; 25 | } 26 | return p; 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-4/9. Factorial Trailing Zeroes.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/factorial-trailing-zeroes/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int trailingZeroes(int n) { 7 | int t5 = 0; 8 | while (n > 0) { 9 | n /= 5; 10 | t5 += n; 11 | } 12 | return t5; 13 | } 14 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-5/12. Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/longest-substring-without-repeating-characters/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int lengthOfLongestSubstring(string s) { 7 | // sliding window 8 | int start = 0, end = 0, n = s.length(), ans = 0; 9 | map mp; 10 | while (end < n) { 11 | if (mp.find(s[end]) != mp.end()) { 12 | start = max(start, mp[s[end]]); 13 | } 14 | ans = max(ans, end - start + 1); 15 | mp[s[end]] = end + 1; 16 | end++; 17 | // for(auto a:mp){ 18 | // cout< st; 10 | for (char c : s) { 11 | if (c == '(' || c == '[' || c == '{') 12 | st.push(c); 13 | else { 14 | if (st.empty()) 15 | goto end; 16 | char t = st.top(); 17 | if (t == '(' && c != ')' || t == '[' && c != ']' || t == '{' && c != '}') 18 | goto end; 19 | st.pop(); 20 | } 21 | // if(!st.empty()){ 22 | // cout< findRepeatedDnaSequences(string s) { 9 | set ans; 10 | set all; 11 | vector final; 12 | if (s.length() <= 10) 13 | return final; 14 | string s1; 15 | s1 = s.substr(0, 10); 16 | all.insert(s1); 17 | for (int i = 10; i < s.length(); i++) { 18 | s1.erase(s1.begin()); 19 | s1 += s[i]; 20 | if (all.find(s1) != all.end()) { 21 | ans.insert(s1); 22 | } 23 | else { 24 | all.insert(s1); 25 | } 26 | } 27 | for (auto a : ans) 28 | final.push_back(a); 29 | return final; 30 | } 31 | }; 32 | 33 | // method -2: using hashmap -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-8/19. Encoding Message .cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.codechef.com/WICP2002/problems/ENCOMS 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | // #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n >> s; 18 | for (unsigned int i = 1; i < s.length(); i += 2) { 19 | auto a = 122 - s[i] + 'a'; 20 | auto b = 122 - s[i - 1] + 'a'; 21 | s[i] = b, s[i - 1] = a; 22 | } 23 | cout << s << "\n"; 24 | } 25 | 26 | signed main() { 27 | fast; 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | 33 | int t; cin >> t; 34 | while (t--) { 35 | solve(); 36 | } 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-8/20. Asteroid Collision.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/asteroid-collision/submissions/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector asteroidCollision(vector& a) { 7 | stack st; 8 | vector ans; 9 | for (int i = 0; i < a.size(); i++) { 10 | if (a[i] > 0) { 11 | st.push(a[i]); 12 | } 13 | else { 14 | if (!st.empty()) { 15 | // we compare 16 | while (!st.empty() && st.top() < abs(a[i]) && st.top() > 0) { 17 | // powerful stays 18 | st.pop(); 19 | } 20 | if (!st.empty() && st.top() == abs(a[i]) && st.top() > 0) { 21 | st.pop(); 22 | continue; 23 | } 24 | if (!st.empty() && st.top() < 0 || st.empty()) 25 | st.push(a[i]); 26 | } 27 | else { 28 | st.push(a[i]); 29 | } 30 | } 31 | } 32 | while (!st.empty()) 33 | ans.push_back(st.top()), st.pop(); 34 | 35 | reverse(ans.begin(), ans.end()); 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-9/21. Best Time to Buy and Sell Stock II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector& prices) { 7 | int ans = 0; 8 | for (int i = 1; i < prices.size(); i++) { 9 | if (prices[i] > prices[i - 1]) 10 | ans += prices[i] - prices[i - 1]; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-9/22. Jump Game II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/jump-game-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int jump(vector& nums) { 7 | int n = nums.size(); 8 | int ar1[n]; 9 | ar1[0] = 0; 10 | for (int i = 1; i < n; i++) { // curr index 11 | for (int j = 0; j < i; j++) { // index where it may jump from 12 | if (nums[j] >= i - j) { 13 | ar1[i] = ar1[j] + 1; // j is just 1 jump ahead from min num of jumps to reach i 14 | break; 15 | } 16 | } 17 | } 18 | return ar1[n - 1]; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 2(60 questions)/Day-9/23. Find All Numbers Disappeared in an Array.cpp: -------------------------------------------------------------------------------- 1 | // Link -https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector findDisappearedNumbers(vector& nums) { 7 | auto ar = nums; 8 | int n = ar.size(); 9 | vector ans; 10 | int m = -1; 11 | for (int i = 0; i < n; i++) { 12 | if (ar[abs(ar[i]) - 1] > 0) 13 | ar[abs(ar[i]) - 1] = ar[abs(ar[i]) - 1] * m; 14 | } 15 | for (int i = 0; i < n; i++) { 16 | if (ar[i] > 0) { 17 | ans.push_back(i + 1); 18 | } 19 | cout << ar[i] << " "; 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-10/30. Swap Nodes in Pairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/swap-nodes-in-pairs/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* swapPairs(ListNode* head) { 17 | if (!head || !head->next) 18 | return head; 19 | auto tmp = head; 20 | int odd, even; 21 | while (tmp && tmp->next) { 22 | odd = tmp->val; 23 | even = tmp->next->val; 24 | tmp->val = even; 25 | tmp->next->val = odd; 26 | tmp = tmp->next->next; 27 | } 28 | return head; 29 | } 30 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-10/31. Middle of the Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/middle-of-the-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* middleNode(ListNode* head) { 17 | if (!head) return head; 18 | if (head->next == nullptr) return head; 19 | ListNode* fast, *slow; 20 | fast = slow = head; 21 | while (fast && fast->next) { 22 | fast = fast->next->next; 23 | slow = slow->next; 24 | } 25 | return slow; 26 | 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-10/32. Linked List Cycle.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/linked-list-cycle/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode(int x) : val(x), next(NULL) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool hasCycle(ListNode *head) { 15 | if (head == NULL) return 0; 16 | ListNode* fptr, * sptr; 17 | fptr = sptr = head; 18 | while (fptr && (sptr && fptr->next)) { 19 | sptr = sptr->next; 20 | fptr = fptr->next->next; 21 | if (fptr == sptr) { 22 | return 1; 23 | } 24 | } 25 | return 0; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-10/34. Maximum Score of a Good Subarray.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-score-of-a-good-subarray/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maximumScore(vector& nums, int k) { 7 | // greedy - O(n) time and O(1) space 8 | int i = k, j = i; 9 | int mini = nums[k], ans = mini; 10 | while (i > 0 || j < nums.size() - 1) { 11 | if (((i > 0) ? nums[i - 1] : 0) < ((j < (nums.size() - 1)) ? nums[j + 1] : 0)) 12 | mini = min(mini, nums[++j]); 13 | else 14 | mini = min(mini, nums[--i]); 15 | ans = max(ans, mini * (j - i + 1)); 16 | } 17 | return ans; 18 | } 19 | /* 20 | int maximumScore(vector& nums, int k) { 21 | // simple O(n^2) time and O(1) space solution 22 | long long ans = nums[k]; 23 | for(long long i=0;i<=k;i++){ 24 | for(long long j=k;j subordinates; 11 | }; 12 | */ 13 | 14 | class Solution { 15 | vector ar[2002]; 16 | map imp; 17 | 18 | public: 19 | int getImportance(vector emp, int id) { 20 | for (auto e : emp) { 21 | imp[e->id] = e->importance; 22 | for (auto s : e->subordinates) { 23 | ar[e->id].push_back(s); 24 | } 25 | } 26 | int ans = 0; 27 | deque q; 28 | q.push_front(id); 29 | while (!q.empty()) { 30 | int val = q.front(); 31 | ans += imp[val]; 32 | q.pop_front(); 33 | for (int i : ar[val]) { 34 | q.push_back(i); 35 | } 36 | } 37 | return ans; 38 | } 39 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-12/38. Split a String Into the Max Number of Unique Substrings.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/split-a-string-into-the-max-number-of-unique-substrings/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | // a rec func 6 | int solve(string s, int place, unordered_set st, string tmp) { 7 | if (place == s.size()) 8 | return 0; 9 | tmp += s[place]; 10 | int op1, op2; 11 | op1 = op2 = 0; 12 | if (st.find(tmp) == st.end()) { 13 | st.insert(tmp); 14 | op1 = 1 + solve(s, place + 1, st, ""); // tried including temp 15 | st.erase(tmp); // backtracking 16 | } 17 | op2 = solve(s, 1 + place, st, tmp); // a char is added to tmp, and function is called recursively 18 | return max(op1, op2); 19 | } 20 | public: 21 | int maxUniqueSplit(string s) { 22 | unordered_set st; 23 | return solve(s, 0, st, ""); 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-12/41. Remove All Occurrences of a Substring.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/biweekly-contest-55/problems/remove-all-occurrences-of-a-substring/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string removeOccurrences(string s, string part) { 7 | if (part == "" || s == "") 8 | return s; 9 | // using stack 10 | // string subs = ""; 11 | // stack st; 12 | // for(char c:s){ 13 | // subs+=c; 14 | // st.push(c); 15 | // if(subs.size()==part.size()){ 16 | // if(subs==part){ 17 | // for(int i=0;i& nums) { 7 | long long even = 0, odd = 0; 8 | for (auto a : nums) { 9 | long long new_even = max(even, odd - a); 10 | odd = max(odd, even + a); 11 | even = new_even; 12 | } 13 | return max(even, odd); 14 | } 15 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-13/43. Minimum Remove to Make Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string minRemoveToMakeValid(string s) { 7 | // trial -1, using stack 8 | stack st; 9 | unordered_set inx; // indices to remove 10 | int i; 11 | for (i = 0; i < s.length(); i++) { 12 | char c = s[i]; 13 | if (c == '(') { 14 | st.push(i); 15 | } 16 | else if (c == ')') { 17 | if (!st.empty()) 18 | st.pop(); 19 | else { 20 | inx.insert(i); 21 | } 22 | } 23 | } 24 | while (!st.empty()) { 25 | inx.insert(st.top()); 26 | st.pop(); 27 | } 28 | string t = s; 29 | for (i = 0; i < s.length(); i++) 30 | if (inx.find(i) == inx.end()) 31 | t += s[i]; 32 | return t; 33 | } 34 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-13/45. Maximum Product Difference Between Two Pairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/weekly-contest-247/problems/maximum-product-difference-between-two-pairs/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxProductDifference(vector& nums) { 7 | int x, y, z, w; 8 | auto a = max_element(nums.begin(), nums.end()); 9 | x = *a; 10 | *a = -1; 11 | auto b = max_element(nums.begin(), nums.end()); 12 | y = *b; 13 | *a = x; 14 | a = min_element(nums.begin(), nums.end()); 15 | z = *a; 16 | *a = 10003; 17 | b = min_element(nums.begin(), nums.end()); 18 | w = *b; 19 | *b = w; 20 | return (x * y - z * w); 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-13/46. OOPS-classes.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.javatpoint.com/cpp-this-pointer 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | class Employee { 7 | public: 8 | int id; //data member (also instance variable) 9 | string name; //data member(also instance variable) 10 | float salary; 11 | Employee(int i, string n, float s) 12 | { 13 | // this->id = i; 14 | // this->name = n; 15 | // this->salary = s; 16 | // // OR 17 | id = i; 18 | name = n; 19 | salary = s; 20 | } 21 | void display() 22 | { 23 | cout << id << " " << name << " " << salary << endl; 24 | } 25 | }; 26 | int main(void) { 27 | Employee e1 = Employee(101, "Sonoo", 890000); //creating an object of Employee 28 | Employee e2 = Employee(102, "Nakul", 59000); //creating an object of Employee 29 | e1.display(); 30 | e2.display(); 31 | e1.display(); 32 | e2.display(); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-15/52. Majority Vote.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://binarysearch.com/problems/Majority-Vote 2 | // Author - Shumbul Arifa 3 | 4 | int solve(vector& nums) { 5 | if (nums.size() < 1) 6 | return -1; 7 | if (nums.size() == 1) 8 | return nums[0]; 9 | int idx = 0, cnt = 1; 10 | for (int i = 1; i < nums.size(); i++) { 11 | if (nums[idx] == nums[i]) { 12 | cnt++; 13 | } 14 | else { 15 | cnt--; 16 | } 17 | if (cnt == 0) { 18 | idx = i; 19 | cnt = 1; 20 | } 21 | } 22 | // checking if element at index idx is a majority 23 | cnt = 0; 24 | for (int i = 0; i < nums.size(); i++) { 25 | if (nums[i] == nums[idx]) 26 | cnt++; 27 | } 28 | if (cnt <= nums.size() / 2) 29 | return -1; 30 | return nums[idx]; 31 | } 32 | /* 33 | // STL gives TLE 34 | for(int i=0; inums.size()/2) 36 | return nums[i]; 37 | } 38 | return -1; 39 | */ -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-15/55. First to Count to Target.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://binarysearch.com/problems/First-to-Count-to-Target 2 | // Author - Shumbul Arifa 3 | 4 | bool solve(int k, int target) { 5 | // To not let our opponent win, 6 | // 1<= X mod(k+1)<=k 7 | if (target % (k + 1) == 0) 8 | return false; 9 | return true; 10 | } -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-15/56. Special Product List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://binarysearch.com/problems/Special-Product-List 2 | // Author - Shumbul Arifa 3 | 4 | vector solve(vector& nums) { 5 | int n = nums.size(); 6 | vector l2r(n + 1, 1), r2l(n + 1, 1); 7 | for (int i = 1; i <= n; i++) { 8 | l2r[i] = l2r[i - 1] * nums[i - 1]; 9 | } 10 | for (int i = n - 1; i >= 0; i--) { 11 | r2l[i] = r2l[i + 1] * nums[i]; 12 | } 13 | vector ans(n); 14 | for (int i = 0; i < n; i++) 15 | ans[i] = l2r[i] * r2l[i + 1]; 16 | return ans; 17 | } -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-16/59. Largest square formed in a matrix.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/largest-square-formed-in-a-matrix0806/1 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxSquare(int n, int m, vector> mat) { 7 | // code here 8 | vector> dp(n, vector (m, 0)); 9 | int ans = 0; 10 | for (int i = 0; i < n; i++) 11 | dp[i][0] = mat[i][0], ans = ans || mat[i][0]; 12 | for (int j = 0; j < m; j++) 13 | dp[0][j] = mat[0][j], ans = ans || mat[0][j]; 14 | for (int i = 1; i < n; i++) { 15 | for (int j = 1; j < m; j++) { 16 | if (mat[i][j] == 0) { 17 | continue; 18 | } 19 | dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1; 20 | ans = max(ans, dp[i][j]); 21 | } 22 | } 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-2/6. Flatten Binary Tree to Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | TreeNode* prev = NULL; 17 | public: 18 | void flatten(TreeNode* root) { 19 | if (root == NULL) 20 | return; 21 | flatten(root->right); 22 | flatten(root->left); 23 | root->right = prev; 24 | root->left = NULL; 25 | prev = root; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-3/7. Shifted Array Search.cpp: -------------------------------------------------------------------------------- 1 | // Link -https://www.pramp.com/challenge/N5LYMbYzyOtbpovQoYPX 2 | // Author - Shumbul Arifa 3 | 4 | int shiftedArrSearch( const vector& A, int num ) 5 | { 6 | // your code goes here 7 | int l = 0, h = A.size() - 1, mid; 8 | while (l <= h) { 9 | mid = ((h - l) / 2 + l); 10 | if (A[mid] == num) return mid; 11 | if (A[l] == num) return l; 12 | if (A[h] == num) return h; 13 | if (A[mid] > A[h] && A[mid] > num) 14 | l = mid + 1; // shifted 15 | else if (A[mid] > A[h] && A[mid] < num) 16 | h = mid - 1; // shifted but val is in the left part 17 | else { 18 | // unshifted 19 | if (A[mid] < num) 20 | l = mid + 1; 21 | else 22 | h = mid - 1; 23 | } 24 | } 25 | return -1; 26 | } -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-4/11. Grid Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/grid-unique-paths/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::uniquePaths(int A, int B) { 5 | int dp[A][B]; 6 | for (int i = 0; i < A; i++) { 7 | dp[i][0] = 1; 8 | } 9 | for (int j = 0; j < B; j++) { 10 | dp[0][j] = 1; 11 | } 12 | 13 | for (int i = 1; i < A; i++) { 14 | for (int j = 1; j < B; j++) 15 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 16 | } 17 | return dp[A - 1][B - 1]; 18 | } 19 | -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-4/12. Remove Duplicates from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/remove-duplicates-from-sorted-array/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * @input A : Integer array 6 | * @input n1 : Integer array's ( A ) length 7 | * 8 | * @Output Integer 9 | */ 10 | int removeDuplicates(int* A, int n1) { 11 | if (n1 < 2) 12 | return n1; 13 | int i, count = 0, j; 14 | int k = n1; 15 | for (i = 1; i < n1; i++) 16 | { 17 | if (A[i] != A[count]) 18 | A[++count] = A[i]; 19 | } 20 | return count + 1; 21 | } 22 | -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-4/13. Merge Two Sorted Lists II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/merge-two-sorted-lists-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * @input A : Integer array 6 | * @input n1 : Integer array's ( A ) length 7 | * @input B : Integer array 8 | * @input n2 : Integer array's ( B ) length 9 | * 10 | * @Return Returns a array of size n1 + n2 with A and B merged. 11 | */ 12 | int* merge(int* A, int n1, int* B, int n2) { 13 | int i = 0, j = 0, index = 0, n = n1 + n2; 14 | int *res = (int*)malloc(sizeof(int) * (n1 + n2)); 15 | while (i < n1 && j < n2) 16 | { 17 | if (A[i] < B[j]) 18 | { 19 | res[index++] = A[i++]; 20 | } 21 | else 22 | { 23 | res[index++] = B[j++]; 24 | } 25 | } 26 | while (i < n1) 27 | { 28 | res[index++] = A[i++]; 29 | } 30 | while (j < n2) 31 | { 32 | res[index++] = B[j++]; 33 | } 34 | return res; 35 | } 36 | -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-5/14. Odd Even Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/odd-even-linked-list/submissions/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* oddEvenList(ListNode* head) { 17 | if (head == nullptr) return head; 18 | ListNode* oddHead = head; 19 | ListNode* evenHead = head->next, *even = evenHead; 20 | while (evenHead != NULL && evenHead->next != NULL) { 21 | oddHead->next = evenHead->next; 22 | oddHead = oddHead->next; 23 | evenHead->next = oddHead->next; 24 | evenHead = evenHead->next; 25 | } 26 | oddHead->next = even; 27 | return head; 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-7/21. Kth Smallest Element In Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/kth-smallest-element-in-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for binary tree 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | #include 14 | int ans; 15 | void solve_(TreeNode* A, int& B) { 16 | if (!A) 17 | return; 18 | if (B < 1) 19 | return; 20 | if (A->left) { 21 | solve_(A->left, B); 22 | B--; 23 | } 24 | if (B == 1) { 25 | ans = A->val; 26 | return; 27 | } 28 | if (A->right) { 29 | solve_(A->right, --B); 30 | } 31 | } 32 | int Solution::kthsmallest(TreeNode* A, int B) { 33 | solve_(A, B); 34 | return ans; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-7/22. Two Sum II - Input array is sorted.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector twoSum(vector& nums, int t) { 7 | vector ans; 8 | int i = 0, j = nums.size() - 1; 9 | while (i < j) { 10 | if (nums[i] + nums[j] == t) { 11 | ans.push_back(i + 1); 12 | ans.push_back(j + 1); 13 | } 14 | if (nums[i] + nums[j] < t) 15 | i++; 16 | else j--; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-7/23. 2-Sum Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/2sum-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for binary tree 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | bool search(TreeNode* root, TreeNode* curr, int B) { 14 | if (root == NULL) 15 | return false; 16 | return (root != curr && root->val == B) || 17 | (root->val <= B && search(root->right, curr, B)) || 18 | (root->val >= B && search(root->left, curr, B)); 19 | } 20 | bool dfs(TreeNode* root, TreeNode* curr, int B) { 21 | if (!curr) 22 | return false; 23 | return dfs(root, curr->left, B) || dfs(root, curr->right, B) || search(root, curr, B - curr->val); 24 | } 25 | int Solution::t2Sum(TreeNode* A, int B) { 26 | return dfs(A, A, B); 27 | } 28 | -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-8/24. Height of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int h; 6 | void solve(struct Node* node, int level) { 7 | if (!node) 8 | return; 9 | h = max(h, level); 10 | if (node->left) 11 | solve(node->left, level + 1); 12 | if (node->right) 13 | solve(node->right, level + 1); 14 | } 15 | public: 16 | //Function to find the height of a binary tree. 17 | int height(struct Node* node) { 18 | // code here 19 | h = 0; 20 | solve(node, 1); 21 | return h; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-8/25. Count Leaves in Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/count-leaves-in-binary-tree/1 2 | // Author - Shumbul Arifa 3 | 4 | int ans; 5 | void solve(Node* root) { 6 | if (!root) 7 | return; 8 | if (!root->left && !root->right) 9 | ans++; 10 | if (root->left) 11 | solve(root->left); 12 | if (root->right) 13 | solve(root->right); 14 | } 15 | int countLeaves(Node* root) 16 | { 17 | ans = 0; 18 | // Your code here 19 | solve(root); 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-8/26. Mirror Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/mirror-tree/1# 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | // Function to convert a binary tree into its mirror tree. 7 | void mirror(Node* node) { 8 | // code here 9 | if (!node) 10 | return; 11 | auto tmpl = node->left; 12 | auto tmpr = node->right; 13 | node->left = tmpr, node->right = tmpl; 14 | if (node->left) 15 | mirror(node->left); 16 | if (node->right) 17 | mirror(node->right); 18 | } 19 | }; -------------------------------------------------------------------------------- /Phase - 3(60 questions)/Day-9/27. Largest Odd Number in String.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/weekly-contest-246/problems/largest-odd-number-in-string/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string largestOddNumber(string num) { 7 | string ans = ""; 8 | for (int i = num.length() - 1; i >= 0; i--) { 9 | if ((num[i] - '0') % 2) { 10 | ans = num.substr(0, i + 1); 11 | break; 12 | } 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-01/1. D. Deleting Divisors.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://codeforces.com/contest/1537/problem/D 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | 9 | void solve(int n) { 10 | if (n & 1) 11 | cout << "Bob\n"; // n is odd 12 | else { 13 | if (n & (n - 1)) { 14 | cout << "Alice\n"; 15 | // not a power of 2 16 | } 17 | else { 18 | int c = 1; 19 | do { 20 | n = n / 2; 21 | c++; 22 | } while (n); 23 | if (c & 1) 24 | cout << "Bob\n"; 25 | else 26 | cout << "Alice\n"; 27 | } 28 | } 29 | } 30 | signed main() { 31 | int t; cin >> t; 32 | while (t--) { 33 | int n; cin >> n; 34 | solve(n); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-01/3. Count triplets smaller sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/count-triplets-with-sum-smaller-than-x5549/1 2 | // Author - Shumbul Arifa 3 | 4 | long long countTriplets(long long arr[], int n, long long sum) 5 | { 6 | // Your code goes here 7 | int ans = 0; 8 | sort(arr, arr + n); 9 | for (int i = 0; i < n - 2; i++) { 10 | int j = i + 1, k = n - 1; 11 | while (j < k) { 12 | if (arr[i] + arr[j] + arr[k] >= sum) 13 | k--; 14 | else { 15 | ans += (k - j); // important, reason: there can be (k-j) third elements 16 | j++; 17 | } 18 | } 19 | } 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-01/4. Maximum sum increasing triplets.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.geeksforgeeks.org/find-maximum-sum-triplets-array-j-k-ai-aj-ak/ 2 | // Author - Shumbul Arifa 3 | 4 | // return the triplet indices, follow lexicpgraphically smaller one for decision 5 | vector maximumSumTriplet(vector ar, int n) { 6 | int ans = 0, t1 = 0, t2 = 0; 7 | vector trips(3, -1); 8 | for (int i = 1; i < n - 1; i++) { 9 | int max_before = 0, max_after = 0; 10 | for (int j = 0; j < i; j++) { 11 | if (ar[i] > ar[j]) { 12 | if (max_before < ar[j]) { 13 | t1 = j; 14 | max_before = ar[j]; 15 | } 16 | } 17 | } 18 | for (int k = i + 1; k < n; k++) { 19 | if (ar[k] > ar[i]) { 20 | if (max_after < ar[k]) { 21 | t2 = k; 22 | max_after = ar[k]; 23 | } 24 | } 25 | } 26 | if (max_after && max_before) { 27 | if (ans < max_after + max_before + ar[i]) { 28 | trips[0] = i, trips[1] = t1, trips[2] = t2; 29 | ans = max_after + max_before + ar[i]; 30 | } 31 | } 32 | } 33 | return trips; 34 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-02/5. Stone Game.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/stone-game/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int memo[501][501]; 6 | int solve(vector piles, int s, int e) { 7 | if (s > e) 8 | return 0; 9 | if (memo[s][e] != -1) 10 | return memo[s][e]; 11 | if (s == e) 12 | return memo[s][e] = piles[s]; 13 | return memo[s][e] = max(piles[s] - solve(piles, s + 1, e), piles[e] - solve(piles, s, e - 1)); 14 | } 15 | public: 16 | bool stoneGame(vector& piles) { 17 | int n = piles.size(); 18 | memset(memo, -1, sizeof memo); 19 | return solve(piles, 0, n - 1) > 0; 20 | } 21 | }; 22 | 23 | // method - 2 24 | 25 | class Solution { 26 | public: 27 | bool stoneGame(vector& piles) { 28 | return true; 29 | } 30 | }; 31 | 32 | // because it's gven that piles length is always even -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-02/6. Subsets.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/subsets/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector> ans; // {{}, {1}, {2}, {3}, {1,2}, {1,3}} 6 | int k; 7 | void backtrack(vector &nums, int first, vector curr) { 8 | // base case 9 | if (curr.size() == k) { 10 | ans.push_back(curr); 11 | return; 12 | } 13 | // main algo 14 | for (int i = first; i < nums.size(); i++) { 15 | curr.push_back(nums[i]); 16 | backtrack(nums, i + 1, curr); 17 | // this 18 | curr.pop_back(); 19 | } 20 | } 21 | public: 22 | vector> subsets(vector& nums) { 23 | // M-2 Backtracking 24 | vector curr; 25 | for (k = 0; k <= nums.size(); k++) { 26 | curr = {}; 27 | backtrack(nums, 0, curr); 28 | } 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-02/7. Subsets II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/subsets-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector> ans; // {{}, {1}, {2}, {3}, {1,2}, {1,3}} 6 | set> st; 7 | int k; 8 | void backtrack(vector &nums, int first, vector curr) { 9 | // base case 10 | if (curr.size() == k) { 11 | sort(curr.begin(), curr.end()); 12 | st.insert(curr); 13 | return; 14 | } 15 | // main algo 16 | for (int i = first; i < nums.size(); i++) { 17 | curr.push_back(nums[i]); 18 | backtrack(nums, i + 1, curr); 19 | // this 20 | curr.pop_back(); 21 | } 22 | } 23 | public: 24 | vector> subsetsWithDup(vector& nums) { 25 | vector curr; 26 | st.clear(); 27 | ans.clear(); 28 | for (k = 0; k <= nums.size(); k++) { 29 | curr = {}; 30 | backtrack(nums, 0, curr); 31 | } 32 | for (auto a : st) { 33 | ans.push_back(a); 34 | } 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/10. Count Good Numbers.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/weekly-contest-248/problems/count-good-numbers/ 2 | // Author - Shumbul Arifa 3 | 4 | const int mod = 1e9 + 7; 5 | typedef long long ll; 6 | ll power(ll a, ll n, ll mod) {ll p = 1; while (n > 0) {if (n % 2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;} 7 | class Solution { 8 | public: 9 | int countGoodNumbers(long long n) { 10 | if (n == 1) 11 | return 5; 12 | long long ans = 1, five = 5, four = 4; 13 | // n=n%mod; -> never do this ! 14 | return ((power(five, n - n / 2, mod) % mod) * (power(four, n / 2, mod) % mod)) % mod; 15 | } 16 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/11. Sparse Arrays.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.hackerrank.com/challenges/sparse-arrays/problem 2 | // Author - Shumbul Arifa 3 | 4 | vector matchingStrings(vector strings, vector queries) { 5 | map mp; 6 | for (auto a : strings) 7 | mp[a]++; 8 | vector ans; 9 | for (auto b : queries) 10 | if (mp.find(b) != mp.end()) 11 | ans.push_back(mp[b]); 12 | else { 13 | ans.push_back(0); 14 | } 15 | return ans; 16 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/12. Maximum Subarray.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-subarray/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxSubArray(vector& nums) { 7 | // Kadane's algorithm 8 | int curr = -100001, maxi = curr; 9 | for (auto i : nums) { 10 | curr = max(i, curr + i); // whether to start fresh, or add in prev 11 | maxi = max(maxi, curr); 12 | } 13 | return maxi; 14 | } 15 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/13. Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/climbing-stairs/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int climbStairs(int n) { 7 | // basic dp question 8 | if (n < 3) 9 | return n; 10 | int dp[n + 1]; 11 | dp[0] = 0; 12 | dp[1] = 1; 13 | dp[2] = 2; 14 | for (int i = 3; i <= n; i++) { 15 | dp[i] = dp[i - 1] + dp[i - 2]; 16 | } 17 | return dp[n]; 18 | } 19 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/14. Divisor Game.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/divisor-game/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool divisorGame(int n) { 7 | // brute force 8 | int dp[n + 1]; 9 | dp[0] = true; 10 | dp[1] = false; 11 | for (int i = 2; i <= n; i++) { 12 | for (int j = 1; j * j <= i + 1; j++) { 13 | if (i % j == 0 && !dp[i - j]) 14 | dp[i] = true; 15 | } 16 | } 17 | return dp[n]; 18 | } 19 | }; 20 | 21 | // maths 22 | class Solution { 23 | public: 24 | bool divisorGame(int n) { 25 | return !(n % 2); 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/15. Stone Game II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/stone-game-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int dp[101][202]; 6 | int solve(int i, int M, vector piles) { 7 | if (i >= piles.size()) 8 | return 0; 9 | if (dp[i][M] != -1) 10 | return dp[i][M]; 11 | int tot = 0; 12 | int ans = INT_MIN; 13 | for (int j = 0; j < 2 * M; j++) { 14 | if ((i + j) < piles.size()) 15 | tot += piles[i + j]; 16 | ans = max(ans, tot - solve(i + j + 1, max(M, j + 1), piles)); 17 | 18 | } 19 | return dp[i][M] = ans; 20 | } 21 | public: 22 | int stoneGameII(vector& piles) { 23 | int sum = accumulate(piles.begin(), piles.end(), 0); 24 | memset(dp, -1, sizeof dp); 25 | return (sum + solve(0, 1, piles)) / 2; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/16. Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/counting-bits/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int countOnes (int n) 6 | { 7 | int count = 0; 8 | while (n != 0) 9 | { 10 | n = n & (n - 1); 11 | count++; 12 | } 13 | return count; 14 | } 15 | public: 16 | vector countBits(int n) { 17 | vector ans(n + 1, 0); 18 | for (int i = 0; i <= n; i++) { 19 | ans[i] = countOnes(i); 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/17. Decode Ways.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/decode-ways 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int numDecodings(string s) { 7 | // A char may be decoded alone or by pairing with the next char 8 | int n = s.length(); 9 | if (n == 0) 10 | return n; 11 | vector dp(n + 1, 0); 12 | dp[n] = 1; // base case 13 | // to handle the case of avoiding a number starting with '0' 14 | // we traverse back 15 | for (int i = n - 1; i >= 0; i--) { 16 | if (s[i] == '0') 17 | dp[i] = 0; 18 | else { 19 | dp[i] = dp[i + 1]; 20 | // two digit 21 | if (i < n - 1 && (s[i] == '1' || s[i] == '2' && s[i + 1] < '7')) dp[i] += dp[i + 2]; 22 | } 23 | } 24 | return dp[0]; 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/8. Build Array from Permutation.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/weekly-contest-248/problems/build-array-from-permutation/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector buildArray(vector& nums) { 7 | vector ans(nums.size(), 0); 8 | for (int i = 0; i < nums.size(); i++) { 9 | ans[i] = nums[nums[i]]; 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-03/9. Eliminate Maximum Number of Monsters.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/weekly-contest-248/problems/eliminate-maximum-number-of-monsters/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int eliminateMaximum(vector& dist, vector& speed) { 7 | int i, n = dist.size(); 8 | vector time; 9 | for (i = 0; i < n; i++) { 10 | time.push_back((double)dist[i] / (double)speed[i]); 11 | } 12 | sort(time.begin(), time.end()); 13 | int t = 0; 14 | int ans = 0; 15 | for (auto a : time) { 16 | cout << a << " "; 17 | if (a <= t) { 18 | break; 19 | } 20 | else { 21 | t++; 22 | ans++; 23 | } 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-04/18. Divisible by 10.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.hackerearth.com/challenges/competitive/google-interview-preparation/problems/9695be1c24944ca19c695c708976a403/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | 9 | signed main() { 10 | int n; cin >> n; 11 | int ar[n]; 12 | int ten = 0, five = 0, two = 0; 13 | for (int i = 0; i < n; i++) { 14 | cin >> ar[i]; 15 | if (ar[i] % 10 == 0) 16 | ten++; 17 | else if (ar[i] % 5 == 0) 18 | five++; 19 | else if (ar[i] % 2 == 0) 20 | two++; 21 | } 22 | cout << ten*(n - 1) - (ten * (ten - 1)) / 2 + five*two; 23 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-04/19. Connected Components in a Graph.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.hackerearth.com/problem/algorithm/connected-components-in-a-graph/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | vector ar[100001]; 8 | bool vis[100001]; 9 | void dfs(int i) { 10 | vis[i] = 1; 11 | for (auto a : ar[i]) { 12 | if (!vis[a]) 13 | dfs(a); 14 | } 15 | } 16 | 17 | signed main() { 18 | int n; cin >> n; 19 | int q; cin >> q; 20 | while (q--) { 21 | int a, b; cin >> a >> b; 22 | ar[a].push_back(b); 23 | ar[b].push_back(a); 24 | } 25 | int ans = 0; 26 | for (int i = 1; i <= n; i++) { 27 | if (!vis[i]) { 28 | dfs(i); 29 | ans++; 30 | } 31 | } 32 | cout << ans; 33 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-04/20. Is it a tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.spoj.com/problems/PT07Y/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | vector ar[100001]; 8 | bool vis[100001]; 9 | void dfs(int i) { 10 | vis[i] = 1; 11 | for (auto a : ar[i]) { 12 | if (!vis[a]) 13 | dfs(a); 14 | } 15 | } 16 | 17 | signed main() { 18 | int n; cin >> n; 19 | int q; cin >> q; 20 | if (q != n - 1) { 21 | cout << "NO"; 22 | return 0; 23 | } 24 | while (q--) { 25 | int a, b; cin >> a >> b; 26 | ar[a].push_back(b); 27 | ar[b].push_back(a); 28 | } 29 | int ans = 0; 30 | for (int i = 1; i <= n; i++) { 31 | if (!vis[i]) { 32 | dfs(i); 33 | ans++; 34 | if (ans == 2) { 35 | cout << "NO"; 36 | return 0; 37 | } 38 | } 39 | } 40 | cout << "YES"; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-05/26. Sqrt(x).cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/sqrtx/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int mySqrt(int x) { 7 | long long int l, h, mid; 8 | l = 0, h = x / 2 + 1; 9 | int ans = -1; 10 | while (l <= h) { 11 | mid = (h - l) / 2 + l; 12 | if (mid * mid <= x) { 13 | ans = mid; 14 | l = mid + 1; 15 | } 16 | else { 17 | h = mid - 1; 18 | } 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-06/27. Symmetric Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/symmetric-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | bool issym(TreeNode* n1, TreeNode* n2) { 17 | if (!n1 && !n2) 18 | return true; 19 | if (!n1 || !n2) 20 | return false; 21 | if (n1->val != n2->val) 22 | return false; 23 | return issym(n1->left, n2->right) && issym(n1->right, n2->left); 24 | } 25 | public: 26 | bool isSymmetric(TreeNode* root) { 27 | return issym(root, root); 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-06/28. Count Square Sum Triples.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/biweekly-contest-56/problems/count-square-sum-triples/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int countTriples(int n) { 7 | int cnt = 0; 8 | for (int i = 3; i <= n; i++) { 9 | for (int j = 3; j <= n; j++) { 10 | double d = sqrt(i * i + j * j); 11 | int dd = d; 12 | if (dd == d && d <= n) 13 | cnt++; 14 | } 15 | } 16 | return cnt; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-08/34. House Robber III.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/house-robber-iii/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | vector rob_(TreeNode* root) { 17 | vector res(2, 0); 18 | if (!root) 19 | return res; 20 | auto l = rob_(root->left); 21 | auto r = rob_(root->right); 22 | res[0] = max(l[0], l[1]) + max(r[0], r[1]); // exclude 23 | res[1] = root->val + l[0] + r[0]; 24 | return res; 25 | } 26 | public: 27 | int rob(TreeNode* root) { 28 | auto res = rob_(root); 29 | return max(res[0], res[1]); 30 | } 31 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-08/36. Balanced Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/balanced-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | int height(TreeNode* root) { 17 | if (!root) 18 | return 0; 19 | return max(height(root->left), height(root->right)) + 1; 20 | } 21 | public: 22 | bool isBalanced(TreeNode* root) { 23 | if (!root) 24 | return true; 25 | int leftHeight = height(root->left); 26 | int rightHeight = height(root->right); 27 | return (abs(leftHeight - rightHeight) < 2) && isBalanced(root->right) && isBalanced(root->left); 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-08/37. Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/container-with-most-water/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::maxArea(vector &A) { 5 | int ans = 0; 6 | int low = 0, high = A.size() - 1; 7 | while (low <= high) { 8 | ans = max(ans, (high - low) * min(A[low], A[high])); 9 | if (A[high] >= A[low]) { 10 | low++; 11 | } 12 | else { 13 | high--; 14 | } 15 | } 16 | return ans; 17 | } 18 | -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-09/38. Majority Element.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/majority-element/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::majorityElement(const vector &A) { 5 | int ans = 0, cnt = 0; 6 | for (int i = 0; i < A.size(); i++) { 7 | if (cnt == 0) { 8 | ans = A[i]; 9 | cnt++; 10 | } 11 | else { 12 | if (A[i] == ans) 13 | cnt++; 14 | else cnt--; 15 | } 16 | } 17 | return ans; 18 | } 19 | -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-09/39. Rotated Sorted Array Search.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/rotated-sorted-array-search/ 2 | // Author - Shumbul Arifa 3 | 4 | int bin_search(const vector &A, int B, int l, int h) { 5 | int n = A.size(); 6 | if (n == 0) 7 | return -1; 8 | int mid; 9 | while (l <= h) { 10 | mid = l + (h - l) / 2; 11 | if (A[mid] == B) 12 | return mid; 13 | if (A[mid] < B) { 14 | l = mid + 1; 15 | } 16 | else 17 | h = mid - 1; 18 | } 19 | return -1; 20 | } 21 | int Solution::search(const vector &A, int B) { 22 | int n = A.size(); 23 | if (n == 0) 24 | return -1; 25 | 26 | int l, h, mid; 27 | l = 0, h = n - 1; 28 | while (l < h) { 29 | mid = l + (h - l) / 2; 30 | if (A[mid] == B) 31 | return mid; 32 | if (A[mid] > A[h]) { 33 | l = mid + 1; 34 | } 35 | else 36 | h = mid; 37 | } 38 | if (B <= A[n - 1]) 39 | return bin_search(A, B, l, n - 1); 40 | return bin_search(A, B, 0, l - 1); 41 | } 42 | -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-09/40. Repetitions.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1069/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> s; 22 | int ans = 0, cnt = 0; 23 | int n = s.length(); 24 | for (int i = 0; i < n; i++) { 25 | if (i == 0) 26 | cnt = 1; 27 | else if (s[i] == s[i - 1]) { 28 | cnt++; 29 | } 30 | else { 31 | ans = max(ans, cnt); 32 | cnt = 1; 33 | } 34 | } 35 | cout << max(ans, cnt); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-09/41. Increasing Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1094 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | vector ar(n); 24 | for0(i, n) cin >> ar[i]; 25 | int ans = 0; 26 | for (int i = 1; i < n; i++) { 27 | if (ar[i] < ar[i - 1]) 28 | ans += ar[i - 1] - ar[i], ar[i] = ar[i - 1]; 29 | } 30 | cout << ans; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-09/42. Permutations.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1070 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 22 | if (n == 2 || n == 3) 23 | cout << "NO SOLUTION"; 24 | else if (n == 4) 25 | cout << "3 1 4 2"; 26 | else { 27 | vector v(n, 0); 28 | int k = 1; 29 | for (int i = 0; i < n; i += 2) 30 | v[i] = k++; 31 | for (int j = 1; j < n; j += 2) 32 | v[j] = k++; 33 | for (int i : v) 34 | cout << i << " "; 35 | } 36 | 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-09/47. Two Knights.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1072/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | for (int k = 1; k <= n; k++) { 24 | int a1 = k * k; 25 | int a2 = (a1 * (a1 - 1)) / 2; 26 | if (k > 2) 27 | a2 = a2 - 4 * (k - 1) * (k - 2); 28 | cout << a2 << "\n"; 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-10/49. Bit Strings.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1617 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | for (int i = 0; i < n; i++) { 24 | s = (s * 2) % mod; 25 | } 26 | cout << s; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-10/50. Trailing Zeros.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1618 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | for (int i = 5; i <= n; i *= 5) { 24 | s += n / i; 25 | } 26 | cout << s; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-12/53. Creating Strings.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1622 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> s; 23 | vector ans; 24 | sort(s.begin(), s.end()); 25 | do { 26 | ans.push_back(s); 27 | } while (next_permutation(s.begin(), s.end())); 28 | cout << ans.size() << "\n"; 29 | for (auto a : ans) { 30 | cout << a << "\n"; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-12/54. Apple Division.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1623 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | vector v(n); 24 | int s = 0; 25 | for (int i = 0; i < n; i++) { 26 | cin >> v[i]; 27 | s += v[i]; 28 | } 29 | int ans = 0; 30 | for (int i = 0; i < (1 << n); i++) { 31 | int cs = 0; 32 | for (int j = 0; j < n; j++) { 33 | if (i >> j & 1) 34 | cs += v[j]; 35 | } 36 | if (cs <= s / 2) 37 | ans = max(ans, cs); 38 | } 39 | cout << s - 2 * ans; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-13/55. Distinct Numbers.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1621 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | set s; 24 | for (int i = 0; i < n; i++) { 25 | cin >> tmp; 26 | s.insert(tmp); 27 | } 28 | cout << s.size(); 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-13/56. Apartments.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/result/2565500/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n >> m >> k; 23 | vector a(n), b(m); 24 | for0(i, n) 25 | cin >> a[i]; 26 | for0(i, m) 27 | cin >> b[i]; 28 | 29 | int ans = 0; 30 | sort(a.begin(), a.end()); 31 | sort(b.begin(), b.end()); 32 | for (int i = 0, j = 0; i < n; i++) { 33 | while (j < m && b[j] < a[i] - k) 34 | j++; 35 | if (j < m && b[j] <= a[i] + k) 36 | ans++, j++; 37 | } 38 | cout << ans; 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-13/57. Ferris Wheel.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1090 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n >> w; 23 | vector c(n); 24 | for0(i, n) 25 | cin >> c[i]; 26 | sort(c.begin(), c.end()); 27 | int ans = 0; 28 | for (int i = 0, j = n - 1; i < n;) { 29 | while (i < j && c[i] + c[j] > w) 30 | j--; 31 | if (i >= j) break; 32 | ans++, i++, j--; 33 | } 34 | cout << n - ans; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-13/58. Dice Combinations.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1633 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | vector dp(n + 1, 0); 24 | dp[0] = 1; 25 | int l = 6; 26 | for1(i, n) { 27 | for (int j = 1; j <= min(i, l); j++) { 28 | dp[i] = (dp[i] + dp[i - j]) % mod; 29 | } 30 | } 31 | cout << dp[n]; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Phase - 4(60 questions)/Day-13/59. Book Shop.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://cses.fi/problemset/task/1158/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n >> x; 24 | vector c(n, 0), p(n, 0); // cost, pages 25 | for0(i, n) 26 | cin >> c[i]; 27 | for0(i, n) 28 | cin >> p[i]; 29 | 30 | vector dp(x + 1, 0); 31 | for0(i, n) { 32 | for (int j = x; j >= c[i]; j--) { 33 | dp[j] = max(dp[j], dp[j - c[i]] + p[i]); 34 | } 35 | } 36 | 37 | cout << dp[x]; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-01/1. Sum of Digits of String After Convert.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/weekly-contest-251/problems/sum-of-digits-of-string-after-convert/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int getLucky(string s, int k) { 7 | vector all; 8 | for (int i = 0; i < s.length(); i++) { 9 | int dig = s[i] - 'a' + 1; 10 | while (dig) { 11 | all.insert(all.begin(), dig % 10); 12 | dig /= 10; 13 | } 14 | } 15 | for (auto a : all) 16 | cout << a << " "; 17 | // brute force 18 | while (k) { 19 | int sum = 0; 20 | for (int a : all) 21 | sum += a; 22 | all.clear(); 23 | while (sum) { 24 | all.insert(all.begin(), sum % 10); 25 | sum /= 10; 26 | } 27 | if (all.size() == 1) 28 | break; 29 | k--; 30 | } 31 | cout << endl; 32 | s = ""; 33 | for (int a : all) 34 | s += to_string(a); 35 | cout << s << "-<"; 36 | return stoi(s); 37 | } 38 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-01/2. Largest Number After Mutating Substring.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/contest/weekly-contest-251/problems/largest-number-after-mutating-substring/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string maximumNumber(string num, vector& change) { 7 | bool changed = 0; 8 | for (unsigned int i = 0; i < num.length(); i++) { 9 | int n1 = change[num[i] - '0']; 10 | int n2 = num[i] - '0'; 11 | // cout< 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 23 | while (n--) { 24 | int x; cin >> x; 25 | cout << (x + 1) / 10 << "\n"; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-03/13. A. Polycarp and Coins.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://codeforces.com/contest/1551/problem/A 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 17 | int rem = n % 3, n1, n2; 18 | if (rem == 0) 19 | n1 = n / 3, n2 = n - 2 * n1; 20 | else if (rem == 1) 21 | n1 = n / 3 + 1, n2 = n / 3; 22 | else 23 | n1 = n / 3, n2 = n / 3 + 1; 24 | cout << n1 << " " << n2 << "\n"; 25 | } 26 | 27 | signed main() { 28 | fast; 29 | #ifndef ONLINE_JUDGE 30 | freopen("input.txt", "r", stdin); 31 | freopen("output.txt", "w", stdout); 32 | #endif 33 | 34 | int t; cin >> t; 35 | while (t--) 36 | solve(); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-04/16. Number of Provinces.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/number-of-provinces/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int vis[10001]; 6 | int comp = 0; 7 | void dfs(int i, vector>& mat) { 8 | vis[i] = 1; 9 | for (int j = 0; j < mat[0].size(); j++) { 10 | if (mat[i][j] && !vis[j]) 11 | dfs(j, mat); 12 | } 13 | } 14 | public: 15 | int findCircleNum(vector>& mat) { 16 | // components in matrix 17 | comp = 0; 18 | for (int i = 0; i < mat.size(); i++) 19 | vis[i] = 0; 20 | for (int i = 0; i < mat.size(); i++) { 21 | if (!vis[i]) 22 | comp++, dfs(i, mat); 23 | } 24 | return comp; 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-04/18. All Paths From Source to Target.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/all-paths-from-source-to-target/ 2 | // Author - Shumbul Arifa 3 | 4 | #define pb push_back 5 | class Solution { 6 | vector ar[10001]; 7 | vector> ans; 8 | void dfs(int i, vector &res, int n, set &st) { 9 | if (i == n - 1) { 10 | ans.pb(res); 11 | return; 12 | } 13 | for (int a : ar[i]) { 14 | if (st.find(a) == st.end()) { 15 | res.pb(a); 16 | st.insert(a); 17 | dfs(a, res, n, st); 18 | res.pop_back(); 19 | st.erase(a); 20 | } 21 | } 22 | } 23 | public: 24 | vector> allPathsSourceTarget(vector>& graph) { 25 | int n = graph.size(); 26 | for (int i = 0; i < n; i++) { 27 | for (int b : graph[i]) 28 | ar[i].pb(b); 29 | } 30 | vector res; 31 | set st; 32 | st.insert(0); 33 | res.pb(0); 34 | dfs(0, res, n, st); 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-04/21. Word Ladder.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/word-ladder/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int ladderLength(string beginWord, string endWord, vector& word) { 7 | queue q; 8 | q.push(beginWord); 9 | int n = word.size(); 10 | unordered_set todo(word.begin(), word.end()); 11 | int ans = 1; 12 | while (!q.empty()) { 13 | int s = q.size(); 14 | while (s--) { 15 | string f = q.front(); 16 | todo.erase(f); 17 | if (f == endWord) 18 | return ans; 19 | q.pop(); 20 | for (int i = 0; i < f.size(); i++) { 21 | char c = f[i]; 22 | for (int j = 0; j < 26; j++) { 23 | if (j == c - 'a') 24 | continue; 25 | f[i] = j + 'a'; 26 | if (todo.find(f) != todo.end()) 27 | q.push(f); 28 | } 29 | f[i] = c; 30 | } 31 | } 32 | ans++; 33 | } 34 | return 0; 35 | } 36 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-06/24. Next Greater Element I.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/next-greater-element-i/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector nextGreaterElement(vector& nums1, vector& nums2) { 7 | stack st; 8 | int n = nums2.size(); 9 | map mp; 10 | for (int i = n - 1; i >= 0; i--) { 11 | if (st.empty()) { 12 | st.push(nums2[i]); 13 | mp[nums2[i]] = -1; 14 | } 15 | else if (st.top() > nums2[i]) { 16 | mp[nums2[i]] = st.top(); 17 | st.push(nums2[i]); 18 | } 19 | else { 20 | while (!st.empty() && st.top() <= nums2[i]) { 21 | st.pop(); 22 | } 23 | if (st.empty()) 24 | mp[nums2[i]] = -1; 25 | else 26 | mp[nums2[i]] = st.top(); 27 | st.push(nums2[i]); 28 | } 29 | } 30 | vector ans; 31 | for (int i : nums1) { 32 | ans.push_back(mp[i]); 33 | } 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-06/25. Next Greater Element II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/next-greater-element-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector nextGreaterElements(vector& nums) { 7 | if (nums.size() == 0) 8 | return nums; 9 | int m = *max_element(nums.begin(), nums.end()); 10 | stack st; 11 | int n = nums.size(); 12 | vector ans(n); 13 | for (int i = 2 * n - 1; i >= 0; i--) { 14 | if (st.empty()) { 15 | st.push(nums[i % n]); 16 | ans[i % n] = -1; 17 | } 18 | else if (st.top() > nums[i % n]) { 19 | ans[i % n] = st.top(); 20 | st.push(nums[i % n]); 21 | } 22 | else { 23 | while (!st.empty() && st.top() <= nums[i % n]) { 24 | st.pop(); 25 | } 26 | if (st.empty()) 27 | ans[i % n] = -1; 28 | else 29 | ans[i % n] = st.top(); 30 | st.push(nums[i % n]); 31 | } 32 | } 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-06/26. Nearest Smaller Element.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/nearest-smaller-element/ 2 | // Author - Shumbul Arifa 3 | 4 | vector Solution::prevSmaller(vector &a) { 5 | stack st; 6 | vector ans(a.size()); 7 | for (int i = 0; i < a.size(); i++) { 8 | if (st.empty()) { 9 | st.push(a[i]); 10 | ans[i] = -1; 11 | } 12 | else if (st.top() < a[i]) { 13 | ans[i] = st.top(); 14 | st.push(a[i]); 15 | } 16 | else { 17 | while (!st.empty() && st.top() >= a[i]) { 18 | st.pop(); 19 | } 20 | if (st.empty()) 21 | ans[i] = -1; 22 | else 23 | ans[i] = st.top(); 24 | st.push(a[i]); 25 | } 26 | } 27 | return ans; 28 | } 29 | -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-07/30. Rain Water Trapped.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/rain-water-trapped/ 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | int Solution::trap(const vector &A) { 6 | int n = A.size(); 7 | vector mxl(n), mxr(n), water(n); 8 | for (int i = 0; i < n; i++) 9 | if (i == 0) mxl[i] = A[i]; 10 | else mxl[i] = max(mxl[i - 1], A[i]); 11 | for (int i = n - 1; i >= 0; i--) 12 | if (i == n - 1) mxr[i] = A[i]; 13 | else mxr[i] = max(mxr[i + 1], A[i]); 14 | for (int i = 0; i < n; i++) 15 | water[i] = min(mxl[i], mxr[i]) - A[i]; 16 | return accumulate(water.begin(), water.end(), 0); 17 | } 18 | -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-08/32. First non-repeating characters.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/first-non-repeating-character-in-a-stream-of-characters/ 2 | // Author - Shumbul Arifa 3 | 4 | string Solution::solve(string A) { 5 | vector alpha(26, 0); 6 | queue q; 7 | string ans = ""; 8 | for (char c : A) { 9 | alpha[c - 'a']++; 10 | while (!q.empty() && alpha[q.front() - 'a'] > 1) 11 | q.pop(); 12 | if (q.empty()) { 13 | if (alpha[c - 'a'] == 1) 14 | ans += c, q.push(c); 15 | else 16 | ans += '#'; 17 | } 18 | else { 19 | ans += q.front(); 20 | if (alpha[c - 'a'] == 1) 21 | q.push(c); 22 | } 23 | } 24 | return ans; 25 | } 26 | -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-08/33. MAXSPPROD.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/problems/maxspprod/ 2 | // Author - Shumbul Arifa 3 | 4 | const long long mod = 1e9 + 7; 5 | #define ll long long 6 | int Solution::maxSpecialProduct(vector &A) { 7 | ll n = A.size(); 8 | vector nll(n, 0), nlr(n, 0); 9 | stack st; 10 | // nll 11 | for (ll i = 0; i < n; i++) { 12 | while (!st.empty() && A[st.top()] <= A[i]) 13 | st.pop(); 14 | if (!st.empty()) 15 | nll[i] = st.top(); 16 | st.push(i); 17 | } 18 | while (!st.empty()) 19 | st.pop(); 20 | // nlr 21 | 22 | for (ll i = n - 1; i >= 0; i--) { 23 | while (!st.empty() && A[st.top()] <= A[i]) 24 | st.pop(); 25 | if (!st.empty()) 26 | nlr[i] = st.top(); 27 | st.push(i); 28 | } 29 | ll ans = 0; 30 | // for(int i=0;i v; 9 | string ans; 10 | for (int i = 0; i < n; i++) { 11 | string dir = ""; 12 | while (i < n && path[i] != '/') { 13 | dir += path[i]; 14 | i++; 15 | } 16 | if (dir == "..") { 17 | if (!v.empty()) 18 | v.pop_back(); 19 | } 20 | else if (dir == "." || dir == "") {} 21 | else { 22 | v.push_back(dir); 23 | } 24 | // cout<> p; 6 | stack st; 7 | for (int i = 0; i < A.length(); i++) { 8 | if (A[i] == '(') 9 | st.push(i); 10 | else if (A[i] == ')') { 11 | assert(!st.empty()); 12 | int t = st.top(); 13 | st.pop(); 14 | if (p.find(make_pair(t + 1, i - 1)) != p.end() || t > i - 3) 15 | return 1; 16 | p.insert(make_pair(t, i)); 17 | } 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-09/37. Minimum Cost of ropes.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/minimum-cost-of-ropes-1587115620/1# 2 | // Author - Shumbul Arifa 3 | 4 | class Solution 5 | { 6 | public: 7 | //Function to return the minimum cost of connecting the ropes. 8 | long long minCost(long long ar[], long long n) { 9 | // Your code here 10 | priority_queue, greater> pq; 11 | long long ans = 0, i, t1, t2; 12 | for (i = 0; i < n; i++) pq.push(ar[i]); 13 | while (pq.size() > 1) { 14 | t1 = pq.top(), pq.pop(); 15 | t2 = pq.top(), pq.pop(); 16 | ans += t1 + t2; 17 | pq.push(t1 + t2); 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-09/39. Power of Two.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/power-of-two/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool isPowerOfTwo(int n) { 7 | // bit manipulation method 8 | // if no. of set bits = 1, return true, otherwise false 9 | if (n < 0) return false; 10 | int cnt = 0; 11 | while (n) { 12 | n = n & (n - 1); 13 | cnt++; 14 | } 15 | return cnt == 1; 16 | 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-11/46. Olympics Ranking.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.codechef.com/AUG21C/problems/OLYRANK 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> t; 23 | while (t--) { 24 | int g1, s1, b1, g2, s2, b2; 25 | cin >> g1 >> s1 >> b1 >> g2 >> s2 >> b2; 26 | if (g1 + s1 + b1 > g2 + s2 + b2) 27 | cout << "1\n"; 28 | else 29 | cout << "2\n"; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-12/48. Check If String Is a Prefix of Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool isPrefixString(string S, vector& words) { 7 | int k = 0, n = S.length(); 8 | for (auto s : words) { 9 | for (auto c : s) { 10 | if (k == n || c != S[k++]) 11 | return false; 12 | } 13 | if (k == n) 14 | return true; 15 | } 16 | return k == n; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-12/49. Remove Stones to Minimize the Total.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/remove-stones-to-minimize-the-total/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minStoneSum(vector& piles, int k) { 7 | priority_queue pq(piles.begin(), piles.end()); 8 | while (k--) { 9 | int top = pq.top(); 10 | // cout<>& mat) { 8 | // solving in 2 traversals using O(1) extra space 9 | int n = mat.size(), m = mat[0].size(); 10 | for (int i = 0; i < n; i++) { 11 | for (int j = 0; j < m; j++) { 12 | if (mat[i][j] == 0) { 13 | mat[i][j] = inf; 14 | } 15 | } 16 | } 17 | 18 | for (int i = 0; i < n; i++) { 19 | for (int j = 0; j < m; j++) { 20 | if (mat[i][j] == (int)inf) { 21 | for (int k = 0; k < m; k++) { 22 | if (!(k != j && mat[i][k] == (int)inf)) 23 | mat[i][k] = 0; 24 | } 25 | for (int k = 0; k < n; k++) { 26 | if (!(k != i && mat[k][j] == (int)inf)) 27 | mat[k][j] = 0; 28 | } 29 | } 30 | } 31 | } 32 | } 33 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-13/52. Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/reverse-integer/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int reverse(int x) { 7 | int rev = 0; 8 | // stack like logic 9 | while (x != 0) { 10 | int top = x % 10; 11 | x /= 10; // pop 12 | // checking overflow 13 | if (rev > INT_MAX / 10 || rev == INT_MAX && top > 7) 14 | return 0; 15 | if (rev < INT_MIN / 10 || rev == INT_MIN && top < -8) 16 | return 0; 17 | rev = rev * 10 + top; 18 | } 19 | return rev; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-13/53. String to Integer (atoi).cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/string-to-integer-atoi/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int myAtoi(string s) { 7 | if (s == "") 8 | return 0; 9 | int i = 0, sign = 1, base = 0; 10 | while (i < s.length() && s[i] == ' ') i++; 11 | if (i == s.length()) 12 | return 0; 13 | if (s[i] == '-' || s[i] == '+') { 14 | if (s[i] == '-') 15 | sign = -1; 16 | i++; 17 | } 18 | if (i == s.length()) { 19 | return 0; 20 | } 21 | while (s[i] >= '0' && s[i] <= '9') { 22 | if (base > INT_MAX / 10 || (base == INT_MAX / 10 && s[i] - '0' > 7)) { 23 | if (sign == 1) return INT_MAX; 24 | else return INT_MIN; 25 | } 26 | base = 10 * base + (s[i++] - '0'); 27 | } 28 | return base * sign; 29 | } 30 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-14/56. Binary Search Tree to Greater Sum Tre.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | int sum; 17 | void solve(TreeNode* root) { 18 | if (!root) 19 | return; 20 | solve(root->right); 21 | sum += root->val; 22 | root->val = sum; 23 | solve(root->left); 24 | } 25 | public: 26 | TreeNode* bstToGst(TreeNode* root) { 27 | sum = 0; 28 | solve(root); 29 | return root; 30 | } 31 | }; -------------------------------------------------------------------------------- /Phase - 5(60 questions)/Day-14/58. Construct Binary Search Tree from Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | int i = 0; 17 | public: 18 | TreeNode* bstFromPreorder(vector& preorder, int bound = INT_MAX) { 19 | if (i == preorder.size() || preorder[i] > bound) 20 | return NULL; 21 | TreeNode *root = new TreeNode(preorder[i++]); 22 | root->left = bstFromPreorder(preorder, root->val); 23 | root->right = bstFromPreorder(preorder, bound); 24 | return root; 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-01/1. Find Pivot Index.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/learn/card/array-and-string/201/introduction-to-array/1144/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int pivotIndex(vector& nums) { 7 | int n = nums.size(); 8 | vector left_sum(n), right_sum(n); 9 | for (int i = 1; i < n; i++) { 10 | left_sum[i] = left_sum[i - 1] + nums[i - 1]; 11 | } 12 | for (int i = n - 2; i >= 0; i--) { 13 | right_sum[i] = right_sum[i + 1] + nums[i + 1]; 14 | } 15 | for (int i = 0; i < n; i++) { 16 | if (left_sum[i] == right_sum[i]) 17 | return i; 18 | } 19 | return -1; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-01/2. Largest Number At Least Twice of Others.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/explore/learn/card/array-and-string/201/introduction-to-array/1147/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int dominantIndex(vector& nums) { 7 | auto it = max_element(nums.begin(), nums.end()); 8 | auto pos = it - nums.begin(); 9 | int largest = *it; 10 | for (int i = 0; i < nums.size(); i++) { 11 | if (i != pos && nums[i] * 2 > largest) 12 | return -1; 13 | } 14 | return pos; 15 | } 16 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-02/4. Longest Substring Without Repeat.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/longest-substring-without-repeat/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::lengthOfLongestSubstring(string A) { 5 | int l = 0, n = A.size(); 6 | if (n <= 1) 7 | return n; 8 | 9 | int i = 0, j = 1, curr = 1, max_l = 1; 10 | set st; 11 | st.insert(A[i]); 12 | while (j < n) { 13 | if (st.find(A[j]) == st.end()) { 14 | curr++; 15 | st.insert(A[j]); 16 | } 17 | else { 18 | max_l = max(max_l, curr); 19 | while (A[i] != A[j] && i < j) 20 | st.erase(A[i]), i++; 21 | i++; 22 | curr = j - i + 1; 23 | } 24 | j++; 25 | } 26 | max_l = max(max_l, curr); 27 | return max_l; 28 | } -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-02/5. Square Root of Integer.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/square-root-of-integer/ 2 | // Author - Shumbul Arifa 3 | 4 | int Solution::sqrt(int A) { 5 | if (A == 0) return 0; 6 | int start = 1, end = A, ans = 1, mid; 7 | while (start <= end) { 8 | mid = (end - start) / 2 + start; 9 | // cout< "; 10 | assert(mid != 0); 11 | if (mid == A / mid) 12 | return mid; 13 | if (mid < A / mid) 14 | ans = mid, start = mid + 1; 15 | else 16 | end = mid - 1; 17 | } 18 | return ans; 19 | } 20 | -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-02/6. First Index.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.interviewbit.com/old/problems/first-index/?ref=similar_problems 2 | // Author - Shumbul Arifa 3 | 4 | vector Solution::solve(vector &A, vector &B) { 5 | // brute force 6 | int n = B.size(); 7 | map mp; 8 | vector ans(n, -1); 9 | for (int q = 0; q < n; q++) { 10 | if (mp.find(B[q]) != mp.end()) { 11 | ans[q] = mp[B[q]]; 12 | continue; 13 | } 14 | for (int x = 0; x < A.size(); x++) { 15 | if (A[x] >= B[q]) { 16 | ans[q] = x; 17 | mp[B[q]] = x; 18 | break; 19 | } 20 | } 21 | } 22 | return ans; 23 | } 24 | -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-03/7. International Day of Yoga.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.codechef.com/problems/RAMDEV 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | long long l, b, N, L, B, ans = 0; 9 | cin >> l >> b >> N; 10 | for (long long i = 0; i < N; i++) { 11 | cin >> L >> B; 12 | ans += max((L / l) * (B / b), (L / b) * (B / l)); 13 | } 14 | cout << ans; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-03/9. Largest square formed in a matrix.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/largest-square-formed-in-a-matrix0806/1 2 | // Author - Shumbul Arifa 3 | 4 | int maxSquare(int n, int m, vector> mat) { 5 | // code here 6 | vector> dp(n, vector (m, 0)); 7 | int ans = 0; 8 | for (int i = 0; i < n; i++) 9 | dp[i][0] = mat[i][0], ans = ans || mat[i][0]; 10 | for (int j = 0; j < m; j++) 11 | dp[0][j] = mat[0][j], ans = ans || mat[0][j]; 12 | for (int i = 1; i < n; i++) { 13 | for (int j = 1; j < m; j++) { 14 | if (mat[i][j] == 0) { 15 | continue; 16 | } 17 | dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1; 18 | ans = max(ans, dp[i][j]); 19 | } 20 | } 21 | return ans; 22 | } -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-04/10. Maximum Nesting Depth of the Parentheses.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxDepth(string s) { 7 | stack st; 8 | int depth = 0, maxdepth = 0;; 9 | for (int i = 0; i < s.length(); i++) { 10 | if (s[i] == '(') { 11 | if (st.empty()) 12 | st.push(1); 13 | else 14 | st.push(st.top() + 1); 15 | } 16 | else if (s[i] == ')') { 17 | maxdepth = max(st.top(), maxdepth); 18 | st.pop(); 19 | } 20 | } 21 | return maxdepth; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-04/11. Remove Outermost Parentheses.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/remove-outermost-parentheses/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string removeOuterParentheses(string s) { 7 | string ans; 8 | stack st; 9 | for (int i = 0; i < s.length(); i++) { 10 | if (s[i] == '(') { 11 | if (st.empty()) 12 | st.push(1); 13 | else 14 | st.push(st.top() + 1), ans += s[i]; 15 | } 16 | else if (s[i] == ')') { 17 | if (st.top() != 1) { 18 | st.pop(), ans += ')'; 19 | } 20 | else { 21 | st.pop(); 22 | } 23 | } 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-04/12. N-ary Tree Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/n-ary-tree-preorder-traversal/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector ans; 6 | void solve(Node* root) { 7 | if (!root) 8 | return; 9 | ans.push_back(root->val); 10 | if (root->children.size() > 0) { 11 | for (auto a : root->children) 12 | solve(a); 13 | } 14 | } 15 | public: 16 | vector preorder(Node* root) { 17 | ans.clear(); 18 | solve(root); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-04/13. N-ary Tree Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/n-ary-tree-postorder-traversal/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector ans; 6 | void solve(Node* root) { 7 | if (!root) 8 | return; 9 | if (root->children.size() > 0) { 10 | for (auto a : root->children) 11 | solve(a), ans.push_back(a->val); 12 | } 13 | } 14 | public: 15 | vector postorder(Node* root) { 16 | ans.clear(); 17 | if (!root) 18 | return ans; 19 | solve(root); 20 | ans.push_back(root->val); 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-04/15. Final Prices With a Special Discount in a Shop.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector finalPrices(vector& p) { 7 | stack st; 8 | vector ans = p; 9 | for (int i = p.size() - 1; i >= 0; i--) { 10 | if (st.empty()) { 11 | st.push(i); 12 | } 13 | else { 14 | if (p[st.top()] > p[i]) { 15 | while (!st.empty() && p[st.top()] > p[i]) 16 | st.pop(); 17 | } 18 | if (!st.empty()) { 19 | ans[i] -= p[st.top()]; 20 | } 21 | st.push(i); 22 | } 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-04/16. Remove All Adjacent Duplicates In String.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string removeDuplicates(string s) { 7 | stack st; 8 | string ans; 9 | for (int i = 0; i < s.length(); i++) { 10 | if (st.empty()) 11 | st.push(s[i]); 12 | else { 13 | if (st.top() == s[i]) { 14 | while (!st.empty() && st.top() == s[i]) 15 | st.pop(); 16 | } 17 | else st.push(s[i]); 18 | } 19 | } 20 | while (!st.empty()) 21 | ans += st.top(), st.pop(); 22 | reverse(ans.begin(), ans.end()); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-04/17. Build an Array With Stack Operations.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/build-an-array-with-stack-operations/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector buildArray(vector& target, int n) { 7 | int k = 0; 8 | vector ans; 9 | for (int i = 1; i <= n; i++) { 10 | if (k >= target.size()) 11 | break; 12 | if (i < target[k]) { 13 | ans.push_back("Push"), ans.push_back("Pop"); 14 | } 15 | else 16 | ans.push_back("Push"), k++; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-05/18. Baseball Game.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/baseball-game/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int calPoints(vector& ops) { 7 | vector st; 8 | for (auto a : ops) { 9 | if (a == "C") { 10 | st.pop_back(); 11 | } 12 | else if (a == "D") { 13 | st.push_back(2 * st[st.size() - 1]); 14 | } 15 | else if (a == "+") { 16 | int i1 = st[st.size() - 1]; 17 | int i2 = st[st.size() - 2]; 18 | st.push_back(i1 + i2); 19 | } 20 | else { 21 | st.push_back(stoi(a)); 22 | } 23 | } 24 | int ans = accumulate(st.begin(), st.end(), 0); 25 | return ans; 26 | } 27 | }; https: //leetcode.com/problems/baseball-game/ -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-05/19. The K Weakest Rows in a Matrix.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int strength(vector r) { 6 | int i = 0; 7 | while (i < r.size() && r[i]) 8 | i++; 9 | return i; 10 | } 11 | public: 12 | vector kWeakestRows(vector>& mat, int k) { 13 | priority_queue, vector>, greater>> pq; 14 | int i = 0; 15 | for (auto r : mat) 16 | pq.push({strength(r), i++}); 17 | vector ans; 18 | while (!pq.empty() && k--) 19 | ans.push_back(pq.top().second), pq.pop(); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-05/20. Last Stone Weight.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/last-stone-weight/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int lastStoneWeight(vector& s) { 7 | priority_queue pq(s.begin(), s.end()); 8 | while (!pq.empty()) { 9 | if (pq.size() < 2) 10 | break; 11 | int i1 = pq.top(); pq.pop(); 12 | int i2 = pq.top(); pq.pop(); 13 | if (i1 != i2) 14 | pq.push(i1 - i2); 15 | } 16 | if (!pq.empty()) 17 | return pq.top(); 18 | return 0; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-06/23. Peak Index in a Mountain Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/peak-index-in-a-mountain-array/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int peakIndexInMountainArray(vector& arr) { 7 | int l, h, mid; 8 | l = 0, h = arr.size() - 1; 9 | while (l <= h) { 10 | mid = (h - l) / 2 + l; 11 | if (arr[mid] < arr[mid + 1]) { 12 | // everything is okay till mid+1 13 | l = mid + 1; 14 | } 15 | else 16 | h = mid - 1; 17 | } 18 | return l; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-06/25. Special Array With X Elements Greater Than or Equal X.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int specialArray(vector& nums) { 7 | sort(nums.begin(), nums.end(), greater()); 8 | int l = 0, h = nums.size(), mid; 9 | while (l < h) { // l!=h because h-> mid 10 | mid = (h - l) / 2 + l; 11 | if (mid < nums[mid]) 12 | l = mid + 1; 13 | else 14 | h = mid; 15 | } 16 | if (l < nums.size() && nums[l] == l) 17 | return -1; 18 | return l; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-06/26. Fair Candy Swap.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/fair-candy-swap/ 2 | // Author - Shumbul Arifa 3 | 4 | // using set 5 | class Solution { 6 | public: 7 | vector fairCandySwap(vector& aliceSizes, vector& bobSizes) { 8 | set b; 9 | int s1, s2; 10 | s1 = accumulate(aliceSizes.begin(), aliceSizes.end(), 0); 11 | s2 = accumulate(bobSizes.begin(), bobSizes.end(), 0); 12 | for (int i : bobSizes) 13 | b.insert(i); 14 | vector ans; 15 | for (int i : aliceSizes) { 16 | if (b.find((s2 - s1) / 2 + i) != b.end()) { 17 | ans.push_back(i), ans.push_back(i + (s2 - s1) / 2); 18 | return ans; 19 | } 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-06/27. Two Sum II - Input array is sorted.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int bin_search(vector& nums, int i, int target) { 6 | int l = i + 1, h = nums.size() - 1, mid; 7 | while (l <= h) { 8 | mid = (h - l) / 2 + l; 9 | if (nums[mid] == target) 10 | return mid; 11 | if (nums[mid] < target) 12 | l = mid + 1; 13 | else 14 | h = mid - 1; 15 | } 16 | return -1; 17 | } 18 | public: 19 | vector twoSum(vector& nums, int target) { 20 | vector ans; 21 | for (int i = 0; i < nums.size(); i++) { 22 | int bs = bin_search(nums, i, target - nums[i]); 23 | if (bs != -1) 24 | return {i + 1, bs + 1}; 25 | } 26 | return { -1, -1}; 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-06/28. Kth Missing Positive Number.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/kth-missing-positive-number/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int findKthPositive(vector& arr, int k) { 7 | int l = 0, h = arr.size(); 8 | int mid; 9 | while (l < h) { 10 | mid = (h - l) / 2 + l; 11 | if (arr[mid] - mid - 1 < k) 12 | l = mid + 1; 13 | else 14 | h = mid; 15 | } 16 | return l + k; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-06/29. . Intersection of Two Arrays II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/intersection-of-two-arrays-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | // METHOD - 1: using map and multiset 5 | class Solution { 6 | public: 7 | vector intersect(vector& nums1, vector& nums2) { 8 | map st; 9 | multiset tmp; 10 | vector ans; 11 | for (int i : nums1) 12 | st[i]++; 13 | for (int i : nums2) 14 | if (st.find(i) != st.end()) { 15 | tmp.insert(i); 16 | st[i]--; 17 | if (st[i] == 0) 18 | st.erase(i); 19 | } 20 | for (auto a : tmp) 21 | ans.push_back(a); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-06/30. Guess Number Higher or Lower.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/guess-number-higher-or-lower/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int guessNumber(int n) { 7 | int i = guess(n); 8 | if (i == 0) 9 | return n; 10 | int l = 0, h = n, mid; 11 | while (l <= h) { 12 | mid = (h - l) / 2 + l; 13 | int g = guess(mid); 14 | if (g == 0) 15 | return mid; 16 | if (g == 1) 17 | l = mid + 1; 18 | else h = mid - 1; 19 | } 20 | return -1; 21 | } 22 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-07/33. Sum of Root To Leaf Binary Numbers.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int ans; 6 | void solve(TreeNode* root, int num) { 7 | if (!root) 8 | return; 9 | num = (num << 1 | root->val); // left shift and bitwise OR 10 | // if leaf 11 | if (!root->left && !root->right) { 12 | ans += num; 13 | } 14 | else { 15 | solve(root->left, num); 16 | solve(root->right, num); 17 | } 18 | } 19 | public: 20 | int sumRootToLeaf(TreeNode* root) { 21 | ans = 0; 22 | solve(root, 0); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-07/34. Maximum Depth of N-ary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int depth; 6 | void dfs(Node* root, int d) { 7 | if (!root) 8 | return; 9 | depth = max(d, depth); 10 | for (auto a : root->children) 11 | dfs(a, d + 1); 12 | } 13 | public: 14 | int maxDepth(Node* root) { 15 | depth = 0; 16 | // dfs twice 17 | dfs(root, 1); 18 | return depth; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-07/35. Maximum Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-depth-of-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | int depth; 6 | void dfs(TreeNode* root, int d) { 7 | if (!root) 8 | return; 9 | depth = max(d, depth); 10 | if (root->left) 11 | dfs(root->left, d + 1); 12 | if (root->right) 13 | dfs(root->right, d + 1); 14 | } 15 | public: 16 | int maxDepth(TreeNode* root) { 17 | depth = 0; 18 | dfs(root, 1); 19 | return depth; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-07/36. Invert Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/invert-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | void invert(TreeNode* root) { 6 | if (!root) 7 | return; 8 | if (root->left && root->right) { 9 | swap(root->left, root->right); 10 | invert(root->right); 11 | invert(root->left); 12 | } 13 | else if (root->left) { 14 | root->right = root->left, root->left = NULL; 15 | invert(root->right); 16 | } 17 | else if (root->right) { 18 | root->left = root->right, root->right = NULL; 19 | invert(root->left); 20 | } 21 | } 22 | public: 23 | TreeNode* invertTree(TreeNode* root) { 24 | invert(root); 25 | return root; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-07/37. Univalued Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/univalued-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | bool ans = 1; 6 | int val; 7 | void solve(TreeNode* root) { 8 | if (!root || !ans) 9 | return; 10 | if (root->val != val) { 11 | ans = 0; return; 12 | } 13 | solve(root->left); 14 | solve(root->right); 15 | } 16 | public: 17 | bool isUnivalTree(TreeNode* root) { 18 | ans = true; 19 | if (!root) 20 | return ans; 21 | val = root->val; 22 | solve(root); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-07/38. Binary Tree Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector ans; 6 | void inorder(TreeNode* root) { 7 | if (!root) 8 | return; 9 | if (root->left) 10 | inorder(root->left); 11 | ans.push_back(root->val); 12 | if (root->right) 13 | inorder(root->right); 14 | } 15 | public: 16 | vector inorderTraversal(TreeNode* root) { 17 | ans.clear(); 18 | inorder(root); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-07/39. Average of Levels in Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/average-of-levels-in-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector averageOfLevels(TreeNode* root) { 7 | // bfs 8 | vector ans; 9 | vector lvl[10001]; 10 | if (!root) 11 | return ans; 12 | queue q; 13 | q.push(root); 14 | int l = 0; 15 | // lvl[l++].push_back(root->val); 16 | while (!q.empty()) { 17 | int s = q.size(); 18 | while (s--) { 19 | auto a = q.front(); 20 | q.pop(); 21 | if (a->left) 22 | q.push(a->left); 23 | if (a->right) 24 | q.push(a->right); 25 | lvl[l].push_back(a->val); 26 | } 27 | l++; 28 | } 29 | for (int i = 0; i < l; i++) { 30 | double d = 0; 31 | for (double j : lvl[i]) 32 | d += j; 33 | ans.push_back(d * 1.0 / lvl[i].size()); 34 | } 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-08/40. Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/counting-bits/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector countBits(int n) { 7 | vector ans(n + 1, 0); 8 | for (int i = 1; i <= n; i++) { 9 | ans[i] = ans[i >> 1] + (i & 1); 10 | } 11 | return ans; 12 | } 13 | }; 14 | 15 | // odd and even logic 16 | class Solution { 17 | public: 18 | vector countBits(int num) { 19 | vector res(num); 20 | res.push_back(0); // for num=0 21 | if (num == 0) return res; 22 | 23 | for (int i = 1; i <= num; i++) { 24 | if (i % 2 == 0) { 25 | res[i] = res[i / 2]; 26 | } else { 27 | res[i] = res[i - 1] + 1; 28 | } 29 | } 30 | return res; 31 | } 32 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-08/42. Pascal's Triangle.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/pascals-triangle/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector> generate(int numRows) { 7 | vector> ans(numRows); 8 | ans[0] = {1}; 9 | for (int i = 1; i < numRows; i++) { 10 | int prevs = ans[i - 1].size(); 11 | vector v(prevs + 1); 12 | v[0] = v[prevs] = 1; 13 | for (int j = 0; j < prevs - 1; j++) { 14 | v[j + 1] = ans[i - 1][j] + ans[i - 1][j + 1]; 15 | } 16 | ans[i] = v; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-08/43. N-th Tribonacci Number.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/n-th-tribonacci-number/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | map mp; 6 | public: 7 | int tribonacci(int n) { 8 | if (n < 2) 9 | return n; 10 | if (n == 2) 11 | return 1; 12 | if (mp.find(n) != mp.end()) 13 | return mp[n]; 14 | return mp[n] = tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3); 15 | } 16 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-08/44. Min Cost Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/min-cost-climbing-stairs/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minCostClimbingStairs(vector& cost) { 7 | cost.push_back(0); 8 | int n = cost.size(), dp[n]; 9 | dp[0] = cost[0], dp[1] = cost[1]; 10 | for (int i = 2; i < n; i++) { 11 | dp[i] = cost[i] + min(dp[i - 1], dp[i - 2]); 12 | } 13 | return dp[n - 1]; 14 | } 15 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-08/45. Pascal's Triangle II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/pascals-triangle-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector getRow(int rowIndex) { 7 | vector> ans(rowIndex + 1); 8 | ans[0] = {1}; 9 | for (int i = 1; i <= rowIndex; i++) { 10 | int prevs = ans[i - 1].size(); 11 | vector v(prevs + 1); 12 | v[0] = v[prevs] = 1; 13 | for (int j = 0; j < prevs - 1; j++) { 14 | v[j + 1] = ans[i - 1][j] + ans[i - 1][j + 1]; 15 | } 16 | ans[i] = v; 17 | } 18 | return ans[rowIndex]; 19 | } 20 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-08/46. Get Maximum in Generated Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/get-maximum-in-generated-array/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | map mp; 6 | vector ar; 7 | int N; 8 | int solve(int n) { 9 | if (n < 2) 10 | return n; 11 | if (mp.find(n) != mp.end()) 12 | return mp[n]; 13 | if (n >= 2 && n <= N) 14 | mp[n] = mp[n / 2]; 15 | return mp[n] = mp[n / 2] + mp[n / 2 + 1]; 16 | } 17 | public: 18 | int getMaximumGenerated(int n) { 19 | N = n; 20 | ar.resize(N); 21 | for (int i = 0; i < n; i++) 22 | solve(i); 23 | return *max(ar.begin(), ar.end()); 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-09/47. Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector& prices) { 7 | int pur = INT_MAX, profit = 0; 8 | for (int i = 0; i < prices.size(); i++) { 9 | pur = min(prices[i], pur); 10 | profit = max(profit, prices[i] - pur); 11 | } 12 | return profit; 13 | } 14 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-09/48. Best Time to Buy and Sell Stock II.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector& prices) { 7 | int ans = 0; 8 | for (int i = 1; i < prices.size(); i++) 9 | if (prices[i] > prices[i - 1]) 10 | ans += prices[i] - prices[i - 1]; 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-09/49. Flipping an Image.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/flipping-an-image/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector> flipAndInvertImage(vector>& mat) { 7 | int n = mat.size(), m = mat[0].size(); 8 | for (int i = 0; i < n; i++) { 9 | for (int j = 0; j < (m + 1) / 2; j++) { 10 | swap(mat[i][j], mat[i][m - j - 1]); 11 | mat[i][j] = !mat[i][j]; 12 | if (j != m - j - 1) 13 | mat[i][m - j - 1] = !mat[i][m - j - 1]; 14 | } 15 | } 16 | return mat; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-09/50. Sort Array By Parity.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/sort-array-by-parity/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector sortArrayByParity(vector& nums) { 7 | int i = 0, j = 0; 8 | for (int i = 0; i < nums.size(); i++) { 9 | if (nums[i] % 2 == 0) { 10 | swap(nums[i], nums[j]); 11 | j++; 12 | } 13 | } 14 | return nums; 15 | } 16 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-10/51. Get Maximum in Generated Array.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/get-maximum-in-generated-array/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | unordered_map mp; 6 | int N; 7 | int ans = 0; 8 | int solve(int n) { 9 | if (n < 2) 10 | return mp[n] = n; 11 | if (mp.find(n) != mp.end()) 12 | return mp[n]; 13 | if (n % 2 == 0) 14 | return mp[n] = solve(n / 2); 15 | return mp[n] = solve((n - 1) / 2) + solve((n + 1) / 2); 16 | } 17 | public: 18 | int getMaximumGenerated(int n) { 19 | N = n; 20 | ans = 0; 21 | for (int i = 1; i <= n; i++) 22 | solve(i), ans = max(mp[i], ans); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-10/52. Convert Binary Number in a Linked List to Integer.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | int ans; 16 | void solve(ListNode* head, int &pos) { 17 | if (!head) 18 | return; 19 | solve(head->next, pos); 20 | // cout<val); 22 | // cout<next && slow) { 20 | fast = fast->next->next; 21 | slow = slow->next; 22 | } 23 | return slow; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-10/54. Delete Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/delete-node-in-a-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode(int x) : val(x), next(NULL) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void deleteNode(ListNode* node) { 15 | // change val instead 16 | auto prev = node; 17 | while (node->next) { 18 | prev = node; 19 | node->val = node->next->val; 20 | node = node->next; 21 | } 22 | prev->next = nullptr; 23 | } 24 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-10/55. Reverse Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/reverse-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | ListNode* newhead; 16 | void solve(ListNode* head, ListNode* prev = nullptr) { 17 | if (!head) 18 | return; 19 | if (!head->next) 20 | newhead = head; 21 | solve(head->next, head); 22 | head->next = prev; 23 | } 24 | public: 25 | ListNode* reverseList(ListNode* head) { 26 | solve(head); 27 | return newhead; 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-10/58. Merge Two Sorted Lists.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/merge-two-sorted-lists/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 17 | // iteratively 18 | ListNode* nn = new ListNode(); 19 | ListNode* tmp = nn; 20 | while (l1 && l2) { 21 | if (l1->val <= l2->val) { 22 | tmp->next = l1; 23 | tmp = tmp->next; 24 | l1 = l1->next; 25 | } 26 | else { 27 | tmp->next = l2; 28 | tmp = tmp->next; 29 | l2 = l2->next; 30 | } 31 | } 32 | if (l1) 33 | tmp->next = l1; 34 | else if (l2) 35 | tmp->next = l2; 36 | return nn->next; 37 | } 38 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-10/59. Remove Duplicates from Sorted List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* deleteDuplicates(ListNode* head) { 17 | if (!head) 18 | return head; 19 | auto curr = head; 20 | while (curr && curr->next) { 21 | auto b = curr->next; 22 | while (b && curr->val == b->val) { 23 | b = b->next; 24 | } 25 | curr->next = b; 26 | curr = curr->next; 27 | } 28 | return head; 29 | } 30 | }; -------------------------------------------------------------------------------- /Phase - 6(60 questions)/Day-10/60. Intersection of Two Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/intersection-of-two-linked-lists/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode(int x) : val(x), next(NULL) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 15 | set st; 16 | auto t = headA; 17 | while (t) { 18 | st.insert(t); 19 | t = t->next; 20 | } 21 | t = headB; 22 | while (t) { 23 | if (st.find(t) != st.end()) 24 | return t; 25 | t = t->next; 26 | } 27 | return nullptr; 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-01/1. Palindrome Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/palindrome-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | ListNode* tmp; 16 | bool ans; 17 | bool solve(ListNode* head) { 18 | if (!head) { 19 | return 1; 20 | } 21 | if (!ans) 22 | return 0; 23 | // cout<val<val<<"\n"; 24 | if (head->next) 25 | ans = ans && solve(head->next); 26 | ans = ans && tmp->val == head->val; 27 | tmp = tmp->next; 28 | return ans; 29 | } 30 | public: 31 | bool isPalindrome(ListNode* head) { 32 | tmp = head; 33 | ans = 1; 34 | bool res = solve(head); 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-01/2. Remove Linked List Elements.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/remove-linked-list-elements/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | ListNode * newhead = new ListNode(); 16 | void solve(ListNode* head, ListNode* prev, int val) { 17 | if (!head) 18 | return; 19 | if (head->val == val) { 20 | prev->next = head->next; 21 | solve(head->next, prev, val); 22 | } 23 | else 24 | solve(head->next, head, val); 25 | } 26 | public: 27 | ListNode* removeElements(ListNode* head, int val) { 28 | newhead->next = head; 29 | solve(head, newhead, val); 30 | return newhead->next; 31 | } 32 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-01/3. Merge In Between Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/merge-in-between-linked-lists/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) { 17 | auto tmp = list1; 18 | ListNode *c, *d; 19 | b -= a; 20 | while (--a) { 21 | tmp = tmp->next; 22 | } 23 | c = tmp->next; 24 | tmp->next = list2; 25 | tmp = tmp->next; 26 | while (b--) { 27 | c = c->next; 28 | } 29 | while (tmp->next) 30 | tmp = tmp->next; 31 | tmp->next = c->next; 32 | c->next = NULL; 33 | return list1; 34 | } 35 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-01/5. Swapping Nodes in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/swapping-nodes-in-a-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | int K; 16 | ListNode* tmp; 17 | void solve(ListNode* head) { 18 | if (!head || !K) 19 | return; 20 | if (head->next) 21 | solve(head->next); 22 | --K; 23 | if (!K) { 24 | int t = head->val; 25 | head->val = tmp->val; 26 | tmp->val = t; 27 | } 28 | } 29 | public: 30 | ListNode* swapNodes(ListNode* head, int k) { 31 | K = k; 32 | tmp = head; 33 | while (--K) { 34 | tmp = tmp->next; 35 | } 36 | K = k; 37 | solve(head); 38 | return head; 39 | } 40 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-02/7. Odd Even Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/odd-even-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* oddEvenList(ListNode* head) { 17 | if (!head) 18 | return head; 19 | ListNode* odd = head, *even = head->next, *evenHead = even; 20 | while (even && even->next) { 21 | odd->next = odd->next->next; 22 | even->next = even->next->next; 23 | odd = odd->next; 24 | even = even->next; 25 | } 26 | odd->next = evenHead; 27 | return head; 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-02/9. Flatten a Multilevel Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /* 5 | // Definition for a Node. 6 | class Node { 7 | public: 8 | int val; 9 | Node* prev; 10 | Node* next; 11 | Node* child; 12 | }; 13 | */ 14 | 15 | class Solution { 16 | Node* solve(Node* head) { 17 | auto res = head; 18 | while (head) { 19 | if (head->child) { 20 | auto t = solve(head->child); 21 | auto nxt = head->next; 22 | head->next = head->child; 23 | head->child->prev = head; 24 | head->child = nullptr; 25 | t->next = nxt; 26 | if (nxt) 27 | nxt->prev = t; 28 | head = nxt; 29 | res = t; 30 | } 31 | else { 32 | res = head; 33 | head = head->next; 34 | } 35 | } 36 | return res; 37 | } 38 | public: 39 | Node* flatten(Node* head) { 40 | if (!head) 41 | return head; 42 | solve(head); 43 | return head; 44 | } 45 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-03/13. Binary Tree Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/binary-tree-postorder-traversal/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | vector postorderTraversal(TreeNode* root) { 18 | vector ans; 19 | if (!root) 20 | return ans; 21 | stack st; 22 | st.push(root); 23 | while (!st.empty()) { 24 | auto a = st.top(); 25 | st.pop(); 26 | ans.push_back(a->val); 27 | if (a->left) 28 | st.push(a->left); 29 | if (a->right) 30 | st.push(a->right); 31 | } 32 | reverse(ans.begin(), ans.end()); 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-03/18. Minimum Absolute Difference in BST.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/minimum-absolute-difference-in-bst/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | int val = -1, diff = INT_MAX; 17 | public: 18 | int getMinimumDifference(TreeNode* root) { 19 | if (!root) 20 | return diff; 21 | if (root->left) 22 | getMinimumDifference(root->left); 23 | if (val >= 0) { 24 | diff = min(diff, abs(root->val - val)); 25 | } 26 | val = root->val; 27 | if (root->right) 28 | getMinimumDifference(root->right); 29 | return diff; 30 | } 31 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-04/20. How Many Numbers Are Smaller Than the Current Number.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector smallerNumbersThanCurrent(vector& nums) { 7 | unordered_map mp; 8 | auto a = nums; 9 | sort(a.begin(), a.end()); 10 | int k = 0; 11 | for (int i : a) { 12 | if (mp.find(i) == mp.end()) 13 | mp[i] = k; 14 | k++; 15 | } 16 | vector ans; 17 | for (int i : nums) 18 | ans.push_back(mp[i]); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-04/21. Sorting the Sentence.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/sorting-the-sentence/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | string sortSentence(string s) { 7 | vector v(201); 8 | int m = 0, k; 9 | string t; 10 | for (int i = 0; i < s.length() - 1; i++) { 11 | t = ""; 12 | while (i + 2 == s.length() || (i + 1 < s.length() && s[i + 1] != ' ')) { 13 | t += s[i++]; 14 | } 15 | if (i < s.length()) { 16 | k = s[i] - '0'; 17 | i++; 18 | v[k - 1] = t; 19 | m = max(m, k - 1); 20 | } 21 | // cout< "; 22 | } 23 | t = ""; 24 | for (int i = 0; i <= m; i++) { 25 | t += v[i]; 26 | t += " "; 27 | } 28 | t.pop_back(); 29 | return t; 30 | } 31 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-04/22. Maximum Product Difference Between Two Pairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-product-difference-between-two-pairs/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxProductDifference(vector& nums) { 7 | auto a = nums; 8 | sort(a.begin(), a.end()); 9 | int n = nums.size(); 10 | return (a[n - 2] * a[n - 1] - a[0] * a[1]); 11 | } 12 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-04/23. Sort Array By Parity.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/sort-array-by-parity/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | vector sortArrayByParity(vector& nums) { 7 | int n = nums.size(); 8 | auto a = nums; 9 | int j = 0; 10 | for (int i = 0; i < n; i++) { 11 | if (a[i] % 2 == 0) { 12 | swap(a[i], a[j]); 13 | j++; 14 | } 15 | } 16 | return a; 17 | } 18 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-04/24. Height Checker.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/height-checker/ 2 | // Author - Shumbul Arifa 3 | 4 | // Brute Force 5 | class Solution { 6 | public: 7 | int heightChecker(vector& heights) { 8 | auto a = heights; 9 | sort(a.begin(), a.end()); 10 | int n = a.size(), cnt = 0; 11 | for (int i = 0; i < n; i++) { 12 | if (a[i] != heights[i]) 13 | cnt++; 14 | } 15 | return cnt; 16 | } 17 | }; 18 | 19 | // when we consider the constraints: 20 | int heightChecker(vector& heights) { 21 | int mismatch = 0; //count the number of mismatches 22 | vector count(101, 0); // store the count of heights 23 | 24 | for (int i = 0; i < heights.size(); i++) count[heights[i]]++; 25 | 26 | int i = 1, j = 0; 27 | while (i < 101) { 28 | // check value of count 29 | if (count[i] == 0) { 30 | i++; 31 | } 32 | else { 33 | // compare the index of count with value of height 34 | if (i != heights[j]) mismatch++; 35 | j++; count[i]--; 36 | } 37 | } 38 | 39 | return mismatch; 40 | } -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-04/25. Maximum Units on a Truck.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/maximum-units-on-a-truck/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maximumUnits(vector>& boxTypes, int t) { 7 | // greedy 8 | auto b = boxTypes; 9 | int ans = 0; 10 | sort(b.begin(), b.end(), [](const auto & s1, const auto & s2) { 11 | if (s1[1] != s2[1]) 12 | return s1[1] > s2[1]; 13 | return s1[0] < s1[0]; 14 | }); 15 | // for(auto a:b){ 16 | // for(auto c:a){ 17 | // cout< kWeakestRows(vector> mat, int K) { 7 | vector> v; 8 | int c = 0, k = 0; 9 | for (auto a : mat) { 10 | c = 0; 11 | for (int i = 0; i < a.size() && a[i]; i++) { 12 | c++; 13 | } 14 | v.push_back({c, k++}); 15 | } 16 | vector ans; 17 | sort(v.begin(), v.end()); 18 | k = 0; 19 | while (K--) { 20 | ans.push_back(v[k++].second); 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-05/27. Carvans.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.codechef.com/problems/CARVANS 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 17 | if (!n) 18 | return n; 19 | vi a(n); 20 | for0(i, n) cin >> a[i]; 21 | int m = a[0]; 22 | int ans; 23 | ans = 1; 24 | for1(i, n - 1) { 25 | if (a[i] <= m) { 26 | ans++; 27 | } 28 | m = min(m, a[i]); 29 | } 30 | return ans; 31 | } 32 | 33 | signed main() { 34 | fast; 35 | #ifndef ONLINE_JUDGE 36 | freopen("input.txt", "r", stdin); 37 | freopen("output.txt", "w", stdout); 38 | #endif 39 | 40 | int t; cin >> t; 41 | while (t--) { 42 | cout << solve() << "\n"; 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-05/28. Count Substrings.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://www.codechef.com/problems/CSUB 2 | // Author - Shumbul Arifa 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define int long long 8 | #define vi vector 9 | #define pb push_back 10 | #define for0(i,n) for(int i=0; i> n; 18 | string s; cin >> s; 19 | for (char c : s) 20 | if (c == '1') 21 | ans++; 22 | return (ans * (ans + 1)) / 2; 23 | } 24 | 25 | signed main() { 26 | fast; 27 | #ifndef ONLINE_JUDGE 28 | freopen("input.txt", "r", stdin); 29 | freopen("output.txt", "w", stdout); 30 | #endif 31 | 32 | int t; cin >> t; 33 | while (t--) { 34 | cout << solve() << "\n"; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-06/30. Lowest Common Ancestor of a Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | 14 | class Solution { 15 | public: 16 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 17 | if (!root) 18 | return root; 19 | if (root->val == p->val || root->val == q->val) 20 | return root; 21 | if (root->val > max(p->val, q->val)) 22 | return lowestCommonAncestor(root->left, p, q); 23 | else if (root->val < min(p->val, q->val)) 24 | return lowestCommonAncestor(root->right, p, q); 25 | return root; 26 | } 27 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-06/31. Sum of Left Leaves.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/sum-of-left-leaves/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | int sumi; 17 | void solve(TreeNode* root, bool isleft) { 18 | if (!root) 19 | return; 20 | if (isleft && !root->left && !root->right) { 21 | sumi += root->val; 22 | return; 23 | } 24 | if (root->left) 25 | solve(root->left, true); 26 | if (root->right) 27 | solve(root->right, false); 28 | } 29 | public: 30 | int sumOfLeftLeaves(TreeNode* root) { 31 | if (!root) 32 | return 0; 33 | sumi = 0; 34 | solve(root, false); 35 | return sumi; 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-06/33. Kth Largest Element in a Stream.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/kth-largest-element-in-a-stream/ 2 | // Author - Shumbul Arifa 3 | 4 | class KthLargest { 5 | priority_queue, greater> pq; 6 | int K; 7 | public: 8 | KthLargest(int k, vector& nums) { 9 | K = k; 10 | for (int i : nums) { 11 | pq.push(i); 12 | } 13 | while (pq.size() > k) 14 | pq.pop(); 15 | } 16 | 17 | int add(int val) { 18 | pq.push(val); 19 | if (pq.size() > K) 20 | pq.pop(); 21 | return pq.top(); 22 | } 23 | }; 24 | 25 | /** 26 | * Your KthLargest object will be instantiated and called as such: 27 | * KthLargest* obj = new KthLargest(k, nums); 28 | * int param_1 = obj->add(val); 29 | */ -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-06/35. Symmetric Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/symmetric-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | bool issym(TreeNode* n1, TreeNode* n2) { 17 | if (!n1 && !n2) 18 | return true; 19 | if (!n1 || !n2) 20 | return false; 21 | if (n1->val != n2->val) 22 | return false; 23 | return issym(n1->left, n2->right) && issym(n1->right, n2->left); 24 | } 25 | public: 26 | bool isSymmetric(TreeNode* root) { 27 | return issym(root, root); 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-06/36. Balanced Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/balanced-binary-tree/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | int height(TreeNode* root) { 17 | if (!root) 18 | return 0; 19 | return 1 + max(height(root->left), height(root->right)); 20 | } 21 | public: 22 | bool isBalanced(TreeNode* root) { 23 | if (!root) 24 | return true; 25 | int leftHeight = height(root->left); 26 | int rightHeight = height(root->right); 27 | return (abs(leftHeight - rightHeight) <= 1) && isBalanced(root->right) && isBalanced(root->left); 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-06/38. Path Sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/path-sum/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | bool ans; 17 | void solve(TreeNode* root, int t, int s) { 18 | if (!root || ans) 19 | return; 20 | s += root->val; 21 | if (root->left) 22 | solve(root->left, t, s); 23 | if (root->right) 24 | solve(root->right, t, s); 25 | if (!root->left && !root->right) { 26 | ans = ans || (s == t); 27 | return; 28 | } 29 | } 30 | public: 31 | bool hasPathSum(TreeNode* root, int targetSum) { 32 | 33 | ans = false; 34 | solve(root, targetSum, 0); 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-07/39. Swap Nodes in Pairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/swap-nodes-in-pairs/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* swapPairs(ListNode* head) { 17 | auto tmp = head; 18 | while (tmp && tmp->next) { 19 | int t = tmp->val; 20 | tmp->val = tmp->next->val; 21 | tmp->next->val = t; 22 | tmp = tmp->next->next; 23 | } 24 | return head; 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-07/40. Flatten Binary Tree to Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 13 | * }; 14 | */ 15 | class Solution { 16 | TreeNode* prev = NULL; 17 | public: 18 | void flatten(TreeNode* root) { 19 | if (!root) 20 | return; 21 | if (root->right) 22 | flatten(root->right); 23 | if (root->left) 24 | flatten(root->left); 25 | root->right = prev; 26 | root->left = NULL; 27 | prev = root; 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-08/42. The k-th Lexicographical String of All Happy Strings of Length n.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | string ans = ""; 6 | vector res; 7 | void solve(int n, int k, string s) { 8 | if (res.size() == k) { 9 | return; 10 | } 11 | if (s.size() == n) { 12 | res.push_back(s); 13 | return; 14 | } 15 | for (char ch : {'a', 'b', 'c'}) { 16 | int a = s.size(); 17 | if (!a || s[a - 1] != ch) 18 | solve(n, k, s + ch); 19 | } 20 | } 21 | public: 22 | string getHappyString(int n, int k) { 23 | solve(n, k, ""); 24 | if (res.size() == k) 25 | ans = res[k - 1]; 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-08/43. Minimum Falling Path Sum.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/minimum-falling-path-sum/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int minFallingPathSum(vector>& mat) { 7 | int n = mat.size(), m = mat[0].size(); 8 | int dp[n][m]; 9 | int ans = 102; 10 | for (int j = 0; j < m; j++) dp[n - 1][j] = mat[n - 1][j], ans = min(ans, dp[n - 1][j]); 11 | if (m == 1) 12 | return ans; 13 | ans = INT_MAX; 14 | for (int i = n - 2; i >= 0; --i) { 15 | for (int j = 0; j < m; ++j) { 16 | dp[i][j] = dp[i + 1][j]; 17 | if (j - 1 >= 0 && dp[i + 1][j - 1] < dp[i][j]) dp[i][j] = dp[i + 1][j - 1]; 18 | if (j + 1 < m && dp[i + 1][j + 1] < dp[i][j]) dp[i][j] = dp[i + 1][j + 1]; 19 | dp[i][j] += mat[i][j]; 20 | if (!i) 21 | ans = min(ans, dp[i][j]); 22 | } 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-08/44. Is Subsequence.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/is-subsequence/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | bool isSubsequence(string s, string t) { 7 | int l1 = s.length(), l2 = t.length(); 8 | int i = 0, j = 0; 9 | while (i < s.length() && j < t.length()) { 10 | if (s[i] == t[j]) { 11 | i++, j++; 12 | } 13 | else { 14 | j++; 15 | } 16 | } 17 | return i == l1; 18 | } 19 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-08/45. Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/climbing-stairs/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int climbStairs(int n) { 7 | // basic dp question 8 | if (n < 3) 9 | return n; 10 | int dp[n + 1]; 11 | dp[0] = 0; 12 | dp[1] = 1; 13 | dp[2] = 2; 14 | for (int i = 3; i <= n; i++) { 15 | dp[i] = dp[i - 1] + dp[i - 2]; 16 | } 17 | return dp[n]; 18 | } 19 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-08/46. Maximum Subarray.cpp: -------------------------------------------------------------------------------- 1 | // Link -https://leetcode.com/problems/maximum-subarray/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int maxSubArray(vector& nums) { 7 | int mtn = INT_MIN, max_cont = 0, i, n = nums.size(); 8 | bool flag = true; 9 | for (int i = 0; i < n; i++) { 10 | if (nums[i] > 0) 11 | flag = false; 12 | mtn = max(mtn, nums[i]); 13 | } 14 | if (flag) { 15 | return mtn; 16 | } 17 | for (int i = 0; i < n; i++) { 18 | max_cont = max_cont + nums[i]; 19 | mtn = max(max_cont, mtn); 20 | if (max_cont < 0) 21 | max_cont = 0; 22 | } 23 | return mtn; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-08/48. Count Sorted Vowel Strings.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/count-sorted-vowel-strings/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int countVowelStrings(int n) { 7 | int ans = 0; 8 | int dp[n + 1][6]; 9 | for (int i = 0; i <= n; i++) dp[i][0] = 0; 10 | for (int i = 0; i <= 5; i++) dp[0][i] = 0; 11 | for (int i = 0; i <= 5; i++) dp[1][i] = 1; 12 | for (int i = 2; i <= n; i++) { 13 | for (int j = 1; j <= 5; j++) { 14 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 15 | // cout<>& mat) { 7 | int n = mat.size(), m = mat[0].size(); 8 | 9 | auto v = mat; 10 | 11 | for (int i = 1; i < n; i++) 12 | 13 | for (int j = 1; j < m; j++) 14 | 15 | if (v[i][j]) 16 | 17 | v[i][j] = min(v[i - 1][j], min(v[i - 1][j - 1], v[i][j - 1])) + 1; 18 | 19 | int ans = 0; 20 | 21 | for (int i = 0; i < n; i++) 22 | 23 | for (int j = 0; j < m; j++) 24 | 25 | ans += v[i][j]; 26 | 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-08/50. Count Substrings That Differ by One Character.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/count-substrings-that-differ-by-one-character/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int countSubstrings(string s, string t) { 7 | int n1 = s.length(), n2 = t.length(); 8 | int ans = 0; 9 | for (int i = 0; i < n1; i++) { 10 | for (int j = 0; j < n2; j++) { 11 | int d = 0; 12 | int I = i, J = j; 13 | while (I < n1 && J < n2 && d < 2) { 14 | if (s[I] != t[J]) 15 | d++; 16 | if (d == 1) 17 | ans++; 18 | I++, J++; 19 | } 20 | } 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-09/52. Sort Integers by The Power Value.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/sort-integers-by-the-power-value/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | vector> v; 6 | int get_val(int i) { 7 | int ret = 0; 8 | while (i != 1) { 9 | if (i % 2) 10 | i = (3 * i + 1); 11 | else 12 | i = (i / 2); 13 | ret++; 14 | } 15 | return ret; 16 | } 17 | public: 18 | int getKth(int lo, int hi, int k) { 19 | for (int i = lo; i <= hi; i++) { 20 | v.push_back({get_val(i), i}); 21 | } 22 | sort(v.begin(), v.end()); 23 | return v[k - 1].second; 24 | } 25 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-09/53. Number of Good Ways to Split a String.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://leetcode.com/problems/number-of-good-ways-to-split-a-string/ 2 | // Author - Shumbul Arifa 3 | 4 | class Solution { 5 | public: 6 | int numSplits(string s) { 7 | // using maps 8 | map m1, m2; 9 | if (s.size() < 2) 10 | return 0; 11 | int cnt = 0; 12 | for (int i = 0; i < s.size(); i++) m2[s[i]]++; 13 | for (int i = 0; i < s.size(); i++) { 14 | m2[s[i]]--; 15 | if (m2[s[i]] == 0) 16 | m2.erase(s[i]); 17 | m1[s[i]]++; 18 | if (m1.size() == m2.size()) 19 | cnt++; 20 | } 21 | return cnt; 22 | } 23 | }; -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-09/55. Special Product List.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://binarysearch.com/problems/Special-Product-List 2 | // Author - Shumbul Arifa 3 | 4 | vector solve(vector& nums) { 5 | int n = nums.size(); 6 | vector l2r(n + 1, 1), r2l(n + 1, 1); 7 | for (int i = 1; i <= n; i++) { 8 | l2r[i] = l2r[i - 1] * nums[i - 1]; 9 | } 10 | for (int i = n - 1; i >= 0; i--) { 11 | r2l[i] = r2l[i + 1] * nums[i]; 12 | } 13 | // l2r.erase(l2r.begin()); 14 | // r2l.erase(r2l.end()-1); 15 | vector ans(n); 16 | for (int i = 0; i < n; i++) 17 | ans[i] = l2r[i] * r2l[i + 1]; 18 | return ans; 19 | } -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-09/57. Count Nodes in Complete Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://binarysearch.com/problems/Count-Nodes-in-Complete-Binary-Tree 2 | // Author - Shumbul Arifa 3 | 4 | /** 5 | * class Tree { 6 | * public: 7 | * int val; 8 | * Tree *left; 9 | * Tree *right; 10 | * }; 11 | */ 12 | int d; 13 | void SOL(Tree* root) { 14 | if (!root) 15 | return; 16 | d++; 17 | if (root && !root->left) { 18 | return; 19 | } 20 | SOL(root->left); 21 | SOL(root->right); 22 | } 23 | int solve(Tree* root) { 24 | d = 0; 25 | SOL(root); 26 | return d; 27 | } -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-09/58. Shortest Common Supersequence.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://binarysearch.com/problems/Shortest-Common-Supersequence 2 | // Author - Shumbul Arifa 3 | 4 | int solve(string a, string b) { 5 | int l1 = a.length(), l2 = b.length(); 6 | int dp[l1 + 1][l2 + 1]; 7 | for (int i = 0; i <= l1; i++) dp[i][0] = 0; 8 | for (int i = 0; i <= l2; i++) dp[0][i] = 0; 9 | for (int i = 1; i <= l1; i++) { 10 | for (int j = 1; j <= l2; j++) { 11 | if (a[i - 1] == b[j - 1]) 12 | dp[i][j] = dp[i - 1][j - 1] + 1; 13 | else 14 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 15 | } 16 | } 17 | int k = dp[l1][l2]; 18 | cout << k; 19 | return l1 + l2 - k; 20 | } -------------------------------------------------------------------------------- /Phase - 7(60 questions)/Day-09/59. Count Substrings With All 1s.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://binarysearch.com/problems/Count-Substrings-With-All-1s 2 | // Author - Shumbul Arifa 3 | 4 | int solve(string s) { 5 | int i = 0, t = 0, ans = 0, k = s.length(); 6 | while (i <= k) { 7 | if (i == k) { 8 | ans += (t * (t + 1) / 2); 9 | } 10 | else if (s[i] == '1') 11 | t++; 12 | else if (t) { 13 | ans += (t * (t + 1) / 2); 14 | t = 0; 15 | } 16 | i++; 17 | } 18 | return ans; 19 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive-programming-everyday 2 | ### Phase - 1: 40 days, 60 programs 3 | ### Phase - 2: 29 days, 60 programs 4 | ### Phase - 3: 21 days, 60 programs 5 | ### Phase - 4: 23 days, 60 programs 6 | ### Phase - 5: 21 days, 60 programs 7 | ### Phase - 6: 14 days, 60 programs 8 | ### Phase - 7: 14 days, 60 programs 9 | 10 | --------------------------------------------------------------------------------