├── .gitignore ├── Bit Manipulation Cheat Sheet.md ├── Deepak Kumar ├── Day 1 │ └── Algorithm_(Greedy) │ │ ├── 1. Minimize the sum of product.cpp │ │ ├── 2. Ishaan Loves Chocolates.cpp │ │ ├── 3. Smallest number.cpp │ │ ├── 4. Message Spreading.cpp │ │ └── 5. Swap and Maximize.cpp ├── Day 10 │ ├── 1. Count number of hops.cpp │ ├── 2. Gold Mine Problem.cpp │ ├── 3. Nth catalan number.cpp │ └── 4. Reach the Nth point.cpp ├── Day 11 │ ├── 1. Longest Palindromic Subsequence.cpp │ ├── 2. Maximize Dot Product.cpp │ ├── 3. Number of Unique Paths.cpp │ ├── 4. Reach a given score.cpp │ └── 5. Friends Pairing Problem.cpp ├── Day 12 │ ├── 1. Rod Cutting.cpp │ ├── 2. Knapsack with Duplicate Items.cpp │ └── 4. Paths to reach origin.cpp ├── Day 13 │ ├── 5. Divisor Game.cpp │ ├── 7. Min Cost Climbing Stairs.cpp │ ├── 8. Is Subsequence.cpp │ └── 9. Climbing Stairs.cpp ├── Day 14 │ ├── 1. Maximum Subarray.cpp │ ├── 11.climbing Stairs.cpp │ ├── 2. Range Sum Query - Immutable.cpp │ ├── 3. House Robber.cpp │ ├── 5. Count Square Submatrices with All Ones.cpp │ └── 6. Counting Bits.cpp ├── Day 15 │ ├── 1. Painting the Fence.cpp │ ├── 10. Maximum Sum Bitonic Subsequence.cpp │ ├── 2. Repetitive Addition Of Digits.cpp │ ├── 3. Count even length.cpp │ ├── 4. Sequence of Sequence.cpp │ ├── 5. Longest Common Subsequence.cpp │ ├── 6. Longest Repeating Subsequence.cpp │ ├── 7. Longest Increasing Subsequence.cpp │ ├── 8. Maximum sum increasing subsequence.cpp │ └── 9. LCS of three strings.cpp ├── Day 16 │ ├── 1. Max length chain.cpp │ ├── 10. Minimum steps to minimize n as per given condition.cpp │ ├── 2. Longest subsequence-1.cpp │ ├── 3. Maximum sum Problem.cpp │ ├── 4. Largest square formed in a matrix.cpp │ ├── 5. Pairs with specific difference.cpp │ ├── 6. Minimum cost to fill given weight in a bag.cpp │ ├── 7. Minimum number of jumps.cpp │ ├── 8. Path in Matrix.cpp │ └── 9. Adjacents are not allowed.cpp ├── Day 17 │ ├── 1. Edit Distance.cpp │ ├── 10. Count all possible paths from top left to bottom right.cpp │ ├── 11. Number of ways.cpp │ ├── 12. Kadane's Algorithm.cpp │ ├── 14. Convert to Strictly increasing array.cpp │ ├── 15. Ways to sum to N.cpp │ ├── 2. Minimum Time.cpp │ ├── 3. Longest Common Substring.cpp │ ├── 4. Sum of all substrings of a number.cpp │ ├── 5. Max Sum without Adjacents.cpp │ ├── 6. Reach a given score.cpp │ └── 8. Paths to reach origin.cpp ├── Day 18 │ ├── 1. Delete without head pointer.cpp │ ├── 11. Ugly Numbers.cpp │ ├── 12. k largest elements.cpp │ ├── 13. Print Diagonally.cpp │ ├── 14. N meetings in one room.cpp │ ├── 17. Check Mirror in N-ary tree.cpp │ ├── 18. Majority Element.cpp │ ├── 2. Finding the numbers.cpp │ ├── 3. Implement two stacks in an array.cpp │ ├── 4. Generate binary string.cpp │ ├── 5. Rat in a Maze Problem - I.cpp │ ├── 6. Group Anagrams Together.cpp │ ├── 8. Binary Tree to BST.cpp │ └── 9. Clone a Binary Tree.cpp ├── Day 19 │ ├── 1. Find Missing And Repeating.cpp │ ├── 10. Count Number of SubTrees having given Sum.cpp │ ├── 11. Solve the Sudoku.cpp │ ├── 12. Max Circular Subarray Sum.cpp │ ├── 13. Nth catalan number.cpp │ ├── 14. Set Bits.cpp │ ├── 15. Convert Level Order Traversal to BST.cpp │ ├── 2. Job Sequencing Problem.cpp │ ├── 3. Next larger element.cpp │ ├── 4. Smallest distinct window.cpp │ ├── 5. Sorted matrix.cpp │ ├── 6. Phone directory.cpp │ ├── 8. Infix to Postfix.cpp │ └── 9. Largest square formed in a matrix.cpp ├── Day 2 │ └── Algorithm_(greedy) │ │ ├── 1. Largest number possible.cpp │ │ ├── 2. Minimum Operations.cpp │ │ ├── 3. N meetings in one room.cpp │ │ ├── 4. Maximize Toys.cpp │ │ └── 5. Shop in Candy Store.cpp ├── Day 3 │ ├── 1. Fractional Knapsack.cpp │ ├── 2. Largest number with given sum.cpp │ ├── 3. Huffman Decoding-1.cpp │ ├── 4. Fact Digit Sum.cpp │ └── 5. Check if it is possible to survive on Island.cpp ├── Day 4 │ ├── 1. Smallest number with sum of digits as N and divisible by 10^N.cpp │ ├── 2. Huffman Decoding.cpp │ ├── 3. Maximum sum of increasing order elements from n arrays.cpp │ ├── 4. Minimum Swaps for Bracket Balancing.cpp │ └── 5. Raju and coins.cpp ├── Day 5 │ ├── 1. Hungry Pizza Lovers.cpp │ ├── 3. Choose and Swap.cpp │ ├── 4. Largest Permutation.cpp │ └── 5. Minimize the heights.cpp ├── Day 6 │ ├── 1. Minimum Spanning Tree.cpp │ ├── 2. Implementing Dijkstra Set 1.cpp │ ├── 3. Job Sequencing Problem.cpp │ ├── 4. Page Faults in LRU.cpp │ └── 5. Geek collects the balls.cpp ├── Day 7 │ └── 1. Coin Piles.cpp ├── Day 8 │ ├── 1. Print first n Fibonacci Numbers.cpp │ ├── 2. Padovan Sequence.cpp │ ├── 3. Count numbers containing 4.cpp │ └── 4. 0-1 Knapsack Problem.cpp ├── Day 9 │ ├── 1. Maximum sum increasing subsequence.cpp │ ├── 3. Count all possible paths from top left to bottom right.cpp │ └── 4. Stickler Thief.cpp └── README.md ├── Kushagra Shekhawat ├── Day 1 │ ├── 1. Minimize the sum of product.cpp │ ├── 2. Ishaan Loves Chocolates.cpp │ ├── 3. Smallest number.cpp │ ├── 4. Message Spreading.cpp │ └── 5. Swap and Maximize.cpp ├── Day 10 │ ├── 42. Count number of hops.cpp │ ├── 43. Gold Mine Problem.cpp │ ├── 44. Nth catalan number.cpp │ ├── 45. Reach the Nth point .cpp │ └── Do these Aptitude Questions Again!.md ├── Day 11 │ ├── 46. Longest Palindromic Subsequence.cpp │ ├── 47. Maximize Dot Product.cpp │ ├── 48. Number of Unique Paths.cpp │ ├── 49. Friends Pairing Problem.cpp │ └── Do these Aptitude Questions Again!.md ├── Day 12 │ ├── 50. Rod Cutting.cpp │ ├── 51. Knapsack with Duplicate Items.cpp │ ├── 52. Paths to reach origin.cpp │ ├── 53. Count of strings that can be formed using a, b and c under given constraints.cpp │ └── Do these Aptitude Questions Again!.md ├── Day 13 │ ├── 54. Coin Change.cpp │ ├── 55. Subset Sum Problem.cpp │ ├── 56. Subset with sum divisible by m.cpp │ ├── 57. Perfect Sum Problem.cpp │ ├── 58. Divisor Game.cpp │ ├── 59. Best Time to Buy and Sell Stock.cpp │ ├── 60. Min Cost Climbing Stairs.cpp │ ├── 61. Is Subsequence.cpp │ └── 62. Climbing Stairs.cpp ├── Day 14 │ ├── 63. Maximum Subarray.cpp │ ├── 64. Range Sum Query - Immutable.cpp │ ├── 65. Matrix Block Sum.cpp │ ├── 66. Count Square Submatrices with All Ones.cpp │ ├── 67. Counting Bits.cpp │ ├── 68. Stone Game.cpp │ ├── 69. Minimum Falling Path Sum.cpp │ └── 70. Climbing Stairs.cpp ├── Day 15 │ ├── 71. Painting the Fence.cpp │ ├── 72. Repetitive Addition Of Digits.cpp │ ├── 73. Count even length.cpp │ ├── 74. Sequence of Sequence.cpp │ ├── 75. Longest Common Subsequence.cpp │ ├── 76. Longest Repeating Subsequence.cpp │ ├── 77. Longest Increasing Subsequence.cpp │ ├── 78. Maximum sum increasing subsequence.cpp │ ├── 79. LCS of three strings.cpp │ └── 80. Maximum Sum Bitonic Subsequence.cpp ├── Day 16 │ ├── 81. Max length chain.cpp │ ├── 82. Longest subsequence-1.cpp │ ├── 83. Maximum sum Problem.cpp │ ├── 84. Largest square formed in a matrix.cpp │ ├── 85. Pairs with specific difference.cpp │ ├── 86. Minimum cost to fill given weight in a bag.cpp │ ├── 87. Minimum number of jumps.cpp │ ├── 88. Path in Matrix.cpp │ ├── 89. Adjacents are not allowed.cpp │ └── 90. Minimum steps to minimize n as per given condition.cpp ├── Day 17 │ ├── 100. Count all possible paths from top left to bottom right.cpp │ ├── 101. Number of ways.cpp │ ├── 102. Kadane's Algorithm.cpp │ ├── 103. Convert to Strictly increasing array.cpp │ ├── 104. Ways to sum to N.cpp │ ├── 91. Edit Distance.cpp │ ├── 92. Minimum Time.cpp │ ├── 93. Longest Common Substring.cpp │ ├── 94. Sum of all substrings of a number.cpp │ ├── 95. Max Sum without Adjacents.cpp │ ├── 96. Reach a given score.cpp │ ├── 97. BBT counter.cpp │ ├── 98. Paths to reach origin.cpp │ └── 99. Count the number of ways to tile the floor of size n x m using 1 x m size tiles.cpp ├── Day 18 │ ├── 105.Delete without head pointer.cpp │ ├── 106.Finding the numbers.cpp │ ├── 107.Implement two stacks in an array.cpp │ ├── 108.Generate binary string.cpp │ ├── 109.Rat in a Maze Problem - I.cpp │ ├── 110.Group Anagrams Together.cpp │ ├── 111.Equal.cpp │ ├── 112.Binary Tree to BST.cpp │ ├── 113.Clone a Binary Tree.cpp │ ├── 114.K-Concatenation.cpp │ ├── 115.Ugly Numbers.cpp │ ├── 116.k largest elements.cpp │ ├── 117.Print Diagonally.cpp │ ├── 118.N meetings in one room.cpp │ ├── 119.Roads and Libraries.cpp │ ├── 120.Check If two Line segments Intersect.cpp │ ├── 121.Check Mirror in N-ary tree.cpp │ ├── 122.Majority Element.cpp │ └── 123.Generalised Fibonacci numbers.cpp ├── Day 19 │ ├── 124.Find Missing And Repeating.cpp │ ├── 125.Job Sequencing Problem.cpp │ ├── 126.Next larger element.cpp │ ├── 127.Smallest distinct window.cpp │ ├── 128.Sorted matrix.cpp │ ├── 129.Phone directory.cpp │ ├── 130.First non-repeating character in a stream.cpp │ ├── 131.Infix to Postfix.cpp │ ├── 132.Largest square formed in a matrix.cpp │ ├── 133.Count Number of SubTrees having given Sum.cpp │ ├── 134.Solve the Sudoku.cpp │ ├── 135.Max Circular Subarray Sum.cpp │ ├── 136.Nth catalan number.cpp │ ├── 137.Set Bits.cpp │ └── 138.Convert Level Order Traversal to BST.cpp ├── Day 2 │ ├── 10. Shop in Candy Store.cpp │ ├── 6. Largest number possible.cpp │ ├── 7. Minimum Operations.cpp │ ├── 8. N meetings in one room.cpp │ └── 9. Maximize Toys.cpp ├── Day 20 │ ├── 139.Print adjacency list.cpp │ ├── 140.Graph representations using set and hash.cpp │ ├── 141.Find a Mother Vertex in a Graph.cpp │ ├── 142.Transitive closure of a Graph.cpp │ ├── 143.Transitive Closure of a Graph using DFS.cpp │ ├── 144.Find k-cores of an undirected graph.cpp │ ├── 145.Iterative Depth First Traversal of Graph.cpp │ ├── 146.Count the number of nodes at given level in a tree using BFS..cpp │ ├── 147.Count all possible paths between two vertices.cpp │ ├── 148.Minimum initial vertices to traverse whole matrix with given conditions.cpp │ ├── 149.Shortest path to reach one prime to other by changing single digit at a time.cpp │ ├── 150.Water Jug problem using BFS.cpp │ ├── 151.Count number of trees in a forest.cpp │ ├── 152.Level of Each node in a Tree from source node (using BFS).cpp │ ├── 153.Construct binary palindrome by repeated appending and trimming.cpp │ ├── 154.Transpose graph.cpp │ ├── 155.Path in a Rectangle with Circles.cpp │ └── k-cores-undirected-graph test case.txt ├── Day 21 │ ├── 156. Detect cycle in an undirected graph.cpp │ ├── 157. Detect cycle in a directed graph.cpp │ ├── 158. Detect a negative cycle in a Graph(Bellman Ford).cpp │ ├── 159. Detecting negative cycle using Floyd Warshall.cpp │ ├── 160. Check whether a given graph is Bipartite or not.cpp │ └── 161. Magical Indices in an array.cpp ├── Day 22 │ ├── 162. Topological Sorting.cpp │ ├── 163. All Topological Sorts of a Directed Acyclic Graph.cpp │ ├── 164. Kahn's algorithm for Topological Sorting.cpp │ ├── 165. Maximum edges that can be added to DAG so that remains DAG.cpp │ ├── 166. Longest path between any pair of vertices in DAG.cpp │ ├── 167. Longest Path in a Directed Acyclic Graph.cpp │ ├── 168. Topological Sort of a graph using departure time of vertex.cpp │ ├── 169. Given a sorted dictionary of an alien language, find order of characters.cpp │ ├── 170. Prims Minimum Spanning Tree (MST) Greedy Algo.cpp │ ├── 171. Bridge Edge in Graph.cpp │ ├── 172. Floyd Warshall.cpp │ ├── Kruskals-Minimum-Spanning-Tree-Algorithm-Greedy-Algo.cpp │ ├── Kruskal’s-Minimum-Spanning-Tree-Algorithm-Greedy-Algo test cases.txt │ ├── Longest-Path-between-Pair-in-a-Directed-Acyclic-Graph Test Case.txt │ └── Longest-Path-in-a-Directed-Acyclic-Graph Test case.txt ├── Day 23 │ ├── 173.Find-the-element-that-occurs-only-once-in-a-array-where-other-elements-occurs-thrice.cpp │ ├── 174.Detect-if-two-integers-have-opposite-signs.cpp │ ├── 175.Add-1-to-a-given-number.cpp │ ├── 176.Add-1-to-a-given-number(alternate better way).cpp │ ├── 177.Multiply a given Integer with 3.5.cpp │ ├── 178.Turn off the rightmost set bit.cpp │ ├── 179.Find whether a number is power of four.cpp │ ├── 180.Compute modulus division by a power-of-2-number.cpp │ ├── 181.Rotate Bits.cpp │ ├── 182.Find the Number Occurring Odd Number of Times.cpp │ ├── 183.Count set bits in an integer.cpp │ ├── 184.Count number of bits to be flipped to convert A to B.cpp │ ├── 185.Program to find whether a no is power of two.cpp │ ├── 186.Position of rightmost set bit.cpp │ ├── 187.Binary representation of a given number.cpp │ ├── 188.Find position of the only set bit.cpp │ ├── 189.Swap two nibbles in a byte.cpp │ └── How to turn off a particular bit in a number.cpp ├── Day 24 │ ├── 190. Russian Peasant Multiplication.cpp │ ├── 191. Add two bit strings.cpp │ ├── 192. Write your own strcmp that ignores cases.cpp │ ├── 193. XOR counts of 0s and 1s in binary representation.cpp │ ├── 194. Calculate XOR from 1 to n..cpp │ ├── 195. Equal Sum and XOR.cpp │ ├── 196. 2's Complement.cpp │ ├── 197. Toggle case of a string using Bitwise Operators.cpp │ ├── 198. Toggling k-th bit of a number.cpp │ ├── 199. Sum of numbers with exactly 2 bits set.cpp │ └── 200. Numbers with alternative 1's.cpp ├── Day 25 │ ├── 201. Number of Steps to Reduce a Number to Zero.cpp │ ├── 202. XOR Operation in an Array.cpp │ ├── 203. Convert Binary Number in a Linked List to Integer.cpp │ ├── 204. Hamming Distance.cpp │ ├── 205. Sort Integers by The Number of 1 Bits.cpp │ ├── 206. Single Number.cpp │ ├── 207. Number Complement.cpp │ ├── 208. Prime Number of Set Bits in Binary Representation.cpp │ ├── 209. Binary Number with Alternating Bits.cpp │ ├── 210. Majority Element.cpp │ ├── 211. Find the Difference.cpp │ ├── 212. Missing Number.cpp │ ├── 213. Number of 1 Bits.cpp │ ├── 214. Convert a Number to Hexadecimal.cpp │ ├── 215. Power of Two.cpp │ ├── 216. Power of Four.cpp │ ├── 217. Reverse Bits.cpp │ ├── 218. Count Triplets That Can Form Two Arrays of Equal XOR.cpp │ └── 219. Counting Bits.cpp ├── Day 26 │ ├── 220. XOR Queries of a Subarray.cpp │ ├── 221. Number of Good Ways to Split a String.cpp │ ├── 222. Pseudo-Palindromic Paths in a Binary Tree.cpp │ ├── 223. Letter Case Permutation.cpp │ ├── 224. Single Number III.cpp │ ├── 225. Minimum Flips to Make a OR b Equal to c.cpp │ ├── 226. Subsets.cpp │ ├── 227. Maximum XOR of Two Numbers in an Array.cpp │ ├── 228. Maximum of Absolute Value Expression.cpp │ ├── 229. Single Number II.cpp │ ├── 230. Maximum Product of Word Lengths.cpp │ ├── 231. Sum of Two Integers.cpp │ ├── 232. Total Hamming Distance.cpp │ └── 233. Maximum Length of a Concatenated String with Unique Characters.cpp ├── Day 27 │ ├── 234. Number of Steps to Reduce a Number in Binary Representation to One.cpp │ ├── 235. Check If a String Contains All Binary Codes of Size K.cpp │ ├── 236. Bitwise AND of Numbers Range.cpp │ ├── 237. Repeated DNA Sequences.cpp │ ├── 238. Bitwise ORs of Subarrays.cpp │ ├── 239. Integer Replacement.cpp │ ├── 240. Maximum Score Words Formed by Letters.cpp │ ├── 241. Smallest Sufficient Team.cpp │ ├── 242. Find a Value of a Mysterious Function Closest to Target.cpp │ ├── 243. Number of Ways to Wear Different Hats to Each Other.cpp │ ├── 244. Number of Valid Words for Each Puzzle.cpp │ └── 245. Find Longest Awesome Substring.cpp ├── Day 3 │ ├── 11. Fractional Knapsack.cpp │ ├── 12. Largest number with given sum.cpp │ ├── 13. Huffman Decoding-1.cpp │ ├── 14. Fact Digit Sum.cpp │ └── 15. Check if it is possible to survive on Island.cpp ├── Day 4 │ ├── 16. Smallest number with sum of digits as N and divisible by 10^N.cpp │ ├── 17. Huffman Decoding.cpp │ ├── 18. Maximum sum of increasing order elements from n arrays.cpp │ ├── 19. Minimum Swaps for Bracket Balancing.cpp │ └── 20. Raju and coins.cpp ├── Day 5 │ ├── 21. Hungry Pizza Lovers.cpp │ ├── 22. Theft at World Bank .cpp │ ├── 23. Choose and Swap.cpp │ ├── 24. Largest Permutation.cpp │ └── 25. Minimize the heights.cpp ├── Day 6 │ ├── 26. Minimum Spanning Tree.cpp │ ├── 27. Implementing Dijkstra Set 1 (Adjacency Matrix).cpp │ ├── 28. Job Sequencing Problem.cpp │ ├── 29. Page Faults in LRU.cpp │ └── 30. Geek collects the balls.cpp ├── Day 7 │ ├── 31. Coin Piles.cpp │ ├── 32. 7 Segment Display.cpp │ └── Do these Aptitude Questions Again!.md ├── Day 8 │ ├── 33. Print first n Fibonacci Numbers.cpp │ ├── 34. Padovan Sequence.cpp │ ├── 35. Count numbers containing 4.cpp │ ├── 36. 0 - 1 Knapsack Problem.cpp │ └── Do these Aptitude Questions Again!.md ├── Day 9 │ ├── 37. Maximum sum increasing subsequence.cpp │ ├── 38. Maximize The Cut Segments.cpp │ ├── 39. Count all possible paths from top left to bottom right.cpp │ ├── 40. Stickler Thief.cpp │ ├── 41. Minimum number of Coins.cpp │ └── Do these Aptitude Questions Again!.md ├── Do Me Again!.md ├── README.md └── never_give_up.jpg ├── README.md ├── Script to create the folder and code files.py ├── Shashwat ├── Day 1 │ └── Greedy │ │ ├── Ishaan Loves Chocolates.cpp │ │ ├── Message Spreading.cpp │ │ ├── Minimize the Sum of Products.cpp │ │ ├── Smallest number.cpp │ │ └── Swap and Maximize.cpp ├── Day 2 │ └── Greedy Algo │ │ ├── Largest number possible.cpp │ │ ├── Maximize Toys.cpp │ │ ├── Minimum Operations.cpp │ │ └── Shop in Candy Store.cpp ├── Day 3 │ ├── Check if it is possible to survive on Island.cpp │ ├── Fact Digit Sum.cpp │ ├── Fractional Knapsack.cpp │ ├── Huffman Decoding-1.cpp │ └── Largest number with given sum.cpp ├── Day 4 │ ├── Huffman Decoding.cpp │ ├── Maximum sum of increasing order elements from n arrays.cpp │ ├── Minimum Swaps for Bracket Balancing.cpp │ ├── Raju and coins.cpp │ └── Smallest number with sum of digits as N and divisible by 10^N.cpp └── Day 5 │ ├── Choose and Swap.cpp │ ├── Hungry Pizza Lovers.cpp │ ├── Largest Permutation.cpp │ └── Minimize the heights.cpp └── images └── IMpossible.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | list.txt -------------------------------------------------------------------------------- /Deepak Kumar/Day 1/Algorithm_(Greedy)/1. Minimize the sum of product.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | 3 | int main() { 4 | int T; 5 | cin>>T; 6 | while(T--){ 7 | int N; 8 | cin>>N; 9 | int A[N],B[N]; 10 | for(int i=0;i>A[i]; 12 | } 13 | for(int i=0;i>B[i]; 15 | } 16 | long long unsigned int sum=0; 17 | sort(A,A+N); 18 | sort(B,B+N,greater()); 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N]; 11 | for(int i=0;i>A[i]; 13 | } 14 | int ans=A[0]; 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | void solution(vector& ans, int sum, int no_of_digit){ 5 | if(sum==0 && no_of_digit==1){ 6 | ans.push_back(0); 7 | return; 8 | } 9 | else if(sum==0 && no_of_digit!=1){ 10 | ans.push_back(-1); 11 | return ; 12 | } 13 | else if(sum > 9*no_of_digit){ 14 | ans.push_back(-1); 15 | return ; 16 | } 17 | 18 | sum=sum-1; 19 | for(int i=no_of_digit-1;i>=1;i--){ 20 | if(sum>9){ 21 | ans.push_back(9); 22 | sum=sum-9; 23 | } 24 | else{ 25 | ans.push_back(sum); 26 | sum=0; 27 | } 28 | } 29 | ans.push_back(sum+1); 30 | return; 31 | } 32 | 33 | int main() { 34 | int T; 35 | cin>>T; 36 | while(T--){ 37 | int sum=0,no_of_digit=0; 38 | cin>>sum>>no_of_digit; 39 | vector ans; 40 | solution(ans,sum,no_of_digit); 41 | for(int i=ans.size()-1;i>=0;i--){ 42 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | cout<<2*(N-1)< 2 | using namespace std; 3 | 4 | int solution(int A[],int N){ 5 | int i=0,j=N-1; 6 | sort(A,A+N); 7 | int sum=0; 8 | for(int i=0;i>T; 18 | while(T--){ 19 | int N; 20 | cin>>N; 21 | int A[N]; 22 | for(int i=0;i>A[i]; 24 | } 25 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int dp[N+1]; 11 | dp[0]=1; 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]+dp[i-3]; 16 | } 17 | cout< 2 | #include 3 | using namespace std; 4 | using boost::multiprecision::cpp_int; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int n; 11 | cin>>n; 12 | cpp_int dp[n+1]; 13 | dp[0]=1; 14 | dp[1]=1; 15 | for(int i=2;i<=n;i++){ 16 | dp[i]=0; 17 | for(int j=0;j 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | cin>>n; 10 | unsigned long long 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 | cout< 2 | using namespace std; 3 | int dp[1001][1001]; 4 | 5 | int solution(const string& str,int i,int j){ 6 | if(i>j){ 7 | return 0; 8 | } 9 | if(i==j){ 10 | return 1; 11 | } 12 | if(dp[i][j]){ 13 | return dp[i][j]; 14 | } 15 | if(str[i]==str[j]){ 16 | return dp[i][j]=2+solution(str,i+1,j-1); 17 | } 18 | else{ 19 | return dp[i][j]=max(solution(str,i,j-1),solution(str,i+1,j)); 20 | } 21 | } 22 | 23 | int main() { 24 | int T; 25 | cin>>T; 26 | while(T--){ 27 | string str; 28 | cin>>str; 29 | int n=str.length(); 30 | memset(dp,0,sizeof(dp)); 31 | cout< 2 | using namespace std; 3 | 4 | void solution(long long int A[],long long int B[],long long int m,long long int n){ 5 | long long int dp[n+1][m+1]; 6 | memset(dp,0,sizeof(dp)); 7 | for(int i=1;i<=n;i++){ 8 | for(int j=i;j<=m;j++){ 9 | dp[i][j]=(max(dp[i-1][j-1]+(A[j-1]*B[i-1]),dp[i][j-1])); 10 | } 11 | } 12 | cout<>T; 19 | while(T--){ 20 | int m,n; 21 | cin>>m>>n; 22 | long long int A[m],B[n]; 23 | for(int i=0;i>A[i]; 25 | } 26 | for(int i=0;i>B[i]; 28 | } 29 | solution(A,B,m,n); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 11/3. Number of Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int M,N; 9 | cin>>M>>N; 10 | int dp[M][N]; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N+1]={0}; 11 | A[0]=1; 12 | for(int i=3;i<=N;i++){ 13 | A[i]+=A[i-3]; 14 | } 15 | for(int i=5;i<=N;i++){ 16 | A[i]+=A[i-5]; 17 | } 18 | for(int i=10;i<=N;i++){ 19 | A[i]+=A[i-10]; 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | cin>>n; 10 | unsigned long long 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]=0; 16 | } 17 | for(int i=3;i<=n;i++){ 18 | dp[i]=(dp[i-1]+((i-1)*dp[i-2]))%1000000007; 19 | } 20 | cout< 2 | using namespace std; 3 | 4 | int solution(int value[],int weight[],int no_of_item,int knapsack){ 5 | int dp[no_of_item+1][knapsack+1]; 6 | for(int i=0;i<=no_of_item;i++){ 7 | for(int j=0;j<=knapsack;j++){ 8 | if(i==0 || j==0){ 9 | dp[i][j]=0; 10 | } 11 | else if(weight[i-1]>j){ 12 | dp[i][j]=dp[i-1][j]; 13 | } 14 | else{ 15 | dp[i][j]=max(value[i-1]+dp[i][j-weight[i-1]],dp[i-1][j]); 16 | } 17 | } 18 | } 19 | return dp[no_of_item][knapsack]; 20 | } 21 | 22 | int main() { 23 | int T; 24 | cin>>T; 25 | while(T--){ 26 | int no_of_item,knapsack; 27 | cin>>no_of_item>>knapsack; 28 | int value[no_of_item]; 29 | int weight[no_of_item]; 30 | for(int i=0;i>value[i]; 32 | } 33 | for(int i=0;i>weight[i]; 35 | } 36 | cout< 2 | using namespace std; 3 | int dp[26][26]; 4 | 5 | int solution(int no_of_row,int no_of_column){ 6 | if(no_of_row<0 || no_of_column<0){ 7 | return 0; 8 | } 9 | else if(no_of_row==0 || no_of_column==0){ 10 | return 1; 11 | } 12 | else if(dp[no_of_row][no_of_column]!=-1){ 13 | return dp[no_of_row][no_of_column]; 14 | } 15 | else{ 16 | return dp[no_of_row][no_of_column]=solution(no_of_row-1,no_of_column)+solution(no_of_row,no_of_column-1); 17 | } 18 | } 19 | 20 | int main() { 21 | int T; 22 | cin>>T; 23 | while(T--){ 24 | int no_of_row,no_of_column; 25 | cin>>no_of_row>>no_of_column; 26 | dp[no_of_row][no_of_column]; 27 | memset(dp,-1,sizeof(dp)); 28 | cout<& cost) { 4 | int len=cost.size(); 5 | if(len==0){ 6 | return 0; 7 | } 8 | if(len==1){ 9 | return cost[0]; 10 | } 11 | if(len==2){ 12 | return min(cost[1],cost[0]); 13 | } 14 | int dp[len]; 15 | dp[0]=cost[0]; 16 | dp[1]=cost[1]; 17 | for(int i=2;i=s.length()){ 13 | return true; 14 | } 15 | } 16 | return false; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 13/9. Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | int dp[n+1]; 5 | dp[0]=1; 6 | dp[1]=1; 7 | for(int i=2;i<=n;i++){ 8 | dp[i]=dp[i-1]+dp[i-2]; 9 | } 10 | return dp[n]; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 14/1. Maximum Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int n=nums.size(); 5 | if(n<=0){ 6 | return 0; 7 | } 8 | for(int i=1;i0){ 10 | nums[i]=nums[i]+nums[i-1]; 11 | } 12 | } 13 | int ans=INT_MIN; 14 | for(int i=0;i arr; 4 | NumArray(vector& nums) { 5 | arr=nums; 6 | } 7 | 8 | int sumRange(int i, int j) { 9 | int sum = 0; 10 | for(int k = i; k <= j; k++){ 11 | sum = sum + arr[k]; 12 | } 13 | return sum; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 14/3. House Robber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int rob(vector& nums) { 5 | int len=nums.size(); 6 | if(len==0){ 7 | return 0; 8 | } 9 | if(len==1){ 10 | return nums[0]; 11 | } 12 | if(len==2){ 13 | return max(nums[0],nums[1]); 14 | } 15 | int dp[len]; 16 | dp[0]=nums[0]; 17 | dp[1]=max(nums[0],nums[1]); 18 | for(int i=2;i>& matrix) { 4 | int ans=0; 5 | int row=matrix.size(); 6 | int col=matrix[0].size(); 7 | if(row==0 || col==0){ 8 | return 0; 9 | } 10 | for(int i=0;i0 && i>0 && j>0){ 13 | matrix[i][j]=min(matrix[i-1][j-1],min(matrix[i][j-1],matrix[i-1][j]))+1; 14 | } 15 | ans+=matrix[i][j]; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 14/6. Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int num) { 4 | vector dp; 5 | dp.push_back(0); 6 | for(int i=1;i<=num;i++){ 7 | int temp=i/2; 8 | if(i%2==0){ 9 | dp.push_back(dp[temp]); 10 | } 11 | else{ 12 | dp.push_back(dp[temp]+1); 13 | } 14 | } 15 | return dp; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 15/1. Painting the Fence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | long long dp[100001]; 4 | 5 | int solution(int fence,int paint){ 6 | dp[1]=paint; 7 | dp[2]=paint*(paint-1)+paint; 8 | for(int i=3;i<=fence;i++){ 9 | dp[i]=((dp[i-1]+dp[i-2])*(paint-1))%(long long)(1e9 + 7); 10 | } 11 | return dp[fence]; 12 | } 13 | 14 | int main() { 15 | int T; 16 | cin>>T; 17 | while(T--){ 18 | int fence,paint; 19 | cin>>fence>>paint; 20 | memset(dp,0,sizeof(dp)); 21 | cout< 2 | using namespace std; 3 | 4 | int solution(int num){ 5 | if(num==0){ 6 | return 0; 7 | } 8 | int sum=0; 9 | while(num){ 10 | if(num<10){ 11 | return num; 12 | } 13 | else{ 14 | num=num%10 + num/10; 15 | } 16 | } 17 | } 18 | 19 | int main() { 20 | int T; 21 | cin>>T; 22 | while(T--){ 23 | int num; 24 | cin>>num; 25 | cout< 2 | using namespace std; 3 | int dp[2001][2001]; 4 | 5 | long long solution(long long N,long long diff){ 6 | if(abs(diff)>N){ 7 | return 0; 8 | } 9 | else if(N==1 && diff==0){ 10 | return 2; 11 | } 12 | else if(N==1 && abs(diff)==1){ 13 | return 1; 14 | } 15 | else if(dp[N][N+diff]!=-1){ 16 | return dp[N][N+diff]; 17 | } 18 | else{ 19 | return dp[N][N+diff]=(long long)((solution(N-1,diff-1)+solution(N-1,diff+1)+2*solution(N-1,diff))%1000000007); 20 | } 21 | } 22 | 23 | int main() { 24 | int T; 25 | cin>>T; 26 | while(T--){ 27 | int N; 28 | cin>>N; 29 | memset(dp,-1,sizeof(dp)); 30 | cout< 2 | using namespace std; 3 | 4 | int solution(int maximum,int n){ 5 | int dp[maximum+1][n+1]; 6 | for(int i=0;i<=maximum;i++){ 7 | for(int j=0;j<=n;j++){ 8 | if(i==0 || j==0 || i>T; 25 | while(T--){ 26 | int maximum,n; 27 | cin>>maximum>>n; 28 | cout< 2 | using namespace std; 3 | int dp[101][101]; 4 | 5 | int solution(string s1,int m,string s2,int n){ 6 | for(int i=1;i<=m;i++){ 7 | for(int j=1;j<=n;j++){ 8 | if(s1[i-1]==s2[j-1]){ 9 | dp[i][j]=1+dp[i-1][j-1]; 10 | } 11 | else{ 12 | dp[i][j]=max(dp[i][j-1],dp[i-1][j]); 13 | } 14 | } 15 | } 16 | return dp[m][n]; 17 | } 18 | 19 | int main() { 20 | int T; 21 | cin>>T; 22 | while(T--){ 23 | int m,n; 24 | cin>>m>>n; 25 | string s1,s2; 26 | cin>>s1>>s2; 27 | memset(dp,0,sizeof(dp)); 28 | cout< 2 | using namespace std; 3 | int dp[1001][1001]; 4 | 5 | int solution(string s,int N){ 6 | for(int i=0;i<=N;i++){ 7 | for(int j=0;j<=N;j++){ 8 | if(i==0 || j==0){ 9 | dp[i][j]=0; 10 | } 11 | else if(s[i-1]==s[j-1] && i!=j){ 12 | dp[i][j]=1+dp[i-1][j-1]; 13 | } 14 | else{ 15 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 16 | } 17 | } 18 | } 19 | return dp[N][N]; 20 | } 21 | 22 | int main() { 23 | int T; 24 | cin>>T; 25 | while(T--){ 26 | int N; 27 | cin>>N; 28 | string s; 29 | cin>>s; 30 | cout< 2 | using namespace std; 3 | 4 | void solution(int A[],int N){ 5 | int dp[N]; 6 | for(int i=0;iA[j]){ 12 | dp[i]=max(dp[i],1+dp[j]); 13 | } 14 | } 15 | } 16 | int ans=INT_MIN; 17 | for(int i=0;i>T; 26 | while(T--){ 27 | int N; 28 | cin>>N; 29 | int A[N]; 30 | for(int i=0;i>A[i]; 32 | } 33 | solution(A,N); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 15/8. Maximum sum increasing subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solution(int dp[],int A[],int N){ 5 | if(N<=0){ 6 | return 0; 7 | } 8 | for(int i=1;iA[j]){ 11 | dp[i]=max(dp[i],A[i]+dp[j]); 12 | } 13 | } 14 | } 15 | int maximum=INT_MIN; 16 | for(int i=0;i>T; 25 | while(T--){ 26 | int N; 27 | cin>>N; 28 | int A[N]; 29 | int dp[N]; 30 | for(int i=0;i>A[i]; 32 | dp[i]=A[i]; 33 | } 34 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int dp[N]; 11 | dp[0]=0; 12 | dp[1]=0; 13 | dp[2]=1; 14 | dp[3]=1; 15 | for(int i=4;i<=N;i++){ 16 | if(i%2==0 && i%3==0){ 17 | dp[i]=min({dp[i/2],dp[i/3],dp[i-1]})+1; 18 | } 19 | else if(i%3==0){ 20 | dp[i]=min(dp[i/3],dp[i-1])+1; 21 | } 22 | else if(i%2==0){ 23 | dp[i]=min(dp[i/2],dp[i-1])+1; 24 | } 25 | else{ 26 | dp[i]=dp[i-1]+1; 27 | } 28 | } 29 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N]; 11 | for(int i=0;i>A[i]; 13 | } 14 | int dp[N]; 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int dp[N]; 11 | memset(dp,0,sizeof(dp)); 12 | for(int i=1;i<=N;i++){ 13 | dp[i]=max(i,dp[i/2]+dp[i/3]+dp[i/4]); 14 | } 15 | cout< 2 | using namespace std; 3 | 4 | int solution(int row,int col){ 5 | int mat[row][col]; 6 | for(int i=0;i>mat[i][j]; 9 | } 10 | } 11 | int ans=0; 12 | int dp[row+1][col+1]; 13 | for(int i=0;i<=row;i++){ 14 | for(int j=0;j<=col;j++){ 15 | if(i==0 || j==0){ 16 | dp[i][j]=0; 17 | } 18 | else if(mat[i-1][j-1]==1){ 19 | dp[i][j]=min({dp[i-1][j-1],dp[i-1][j],dp[i][j-1]})+1; 20 | ans=max(ans,dp[i][j]); 21 | } 22 | else{ 23 | dp[i][j]=0; 24 | } 25 | } 26 | } 27 | return ans; 28 | } 29 | 30 | int main() { 31 | int T; 32 | cin>>T; 33 | while(T--){ 34 | int row,col; 35 | cin>>row>>col; 36 | cout< 2 | using namespace std; 3 | int dp[1001][1001]; 4 | 5 | int solution(int A[],int N,int K){ 6 | if(N<=1){ 7 | return 0; 8 | } 9 | if(dp[N][N+K]!=-1){ 10 | return dp[N][N+K]; 11 | } 12 | if(A[N-1]-A[N-2]>T; 23 | while(T--){ 24 | int N; 25 | cin>>N; 26 | int A[N]; 27 | for(int i=0;i>A[i]; 29 | } 30 | int K; 31 | cin>>K; 32 | sort(A,A+N); 33 | memset(dp,-1,sizeof(dp)); 34 | cout< 2 | using namespace std; 3 | #define ll long long 4 | ll dp[10000001]; 5 | 6 | ll solution(ll A[],ll N){ 7 | if(A[0]==0 || N==0){ 8 | return -1; 9 | } 10 | ll dp[N]; 11 | for(ll i=0;i>T; 28 | while(T--){ 29 | ll N; 30 | cin>>N; 31 | ll A[N]; 32 | for(ll i=0;i>A[i]; 34 | } 35 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int mat[N][N]; 11 | for(int i=0;i>mat[i][j]; 14 | } 15 | } 16 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int mat[2][N]; 11 | for(int i=0;i<2;i++){ 12 | for(int j=0;j>mat[i][j]; 14 | } 15 | } 16 | int dp[N]; 17 | for(int i=0;i 2 | using namespace std; 3 | 4 | int solution(string str1,int M,string str2,int N){ 5 | int dp[M][N]; 6 | for(int i=0;i>T; 28 | while(T--){ 29 | int M,N; 30 | cin>>M>>N; 31 | string str1,str2; 32 | cin>>str1>>str2; 33 | str1='0'+str1; 34 | str2='0'+str2; 35 | cout< 2 | using namespace std; 3 | long long int dp[101][101]; 4 | 5 | int main() { 6 | int T; 7 | cin>>T; 8 | while(T--){ 9 | int M,N; 10 | cin>>M>>N; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | long long dp[N+1]; 11 | dp[0]=dp[1]=dp[2]=dp[3]=1; 12 | for(int i=4;i<=N;i++){ 13 | dp[i]=dp[i-4]+dp[i-1]; 14 | } 15 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,i=0,min; 9 | cin>>N; 10 | int arr[N]; 11 | for(i=0;i>arr[i]; 13 | } 14 | min=arr[0]; 15 | for(i=1;i 2 | using namespace std; 3 | 4 | int solution(int A[],int N){ 5 | int dp[N]; 6 | for(int i=0;i>T; 23 | while(T--){ 24 | int N; 25 | cin>>N; 26 | int A[N]; 27 | for(int i=0;i>A[i]; 29 | } 30 | cout< 2 | using namespace std; 3 | #define ll long long 4 | 5 | int main() { 6 | int T; 7 | cin>>T; 8 | while(T--){ 9 | int N,num; 10 | cin>>N>>num; 11 | int A[N]; 12 | for(int i=0;i>A[i]; 14 | } 15 | ll dp[num+1]; 16 | memset(dp,0,sizeof(dp)); 17 | dp[0]=1; 18 | for(int i=1;i<=num;i++){ 19 | for(int j=0;j=A[j]){ 21 | dp[i] = (dp[i]+dp[i-A[j]])%(ll)(1e9+7); 22 | } 23 | } 24 | } 25 | cout< 2 | using namespace std; 3 | int dp[101]; 4 | 5 | int solution(int num,int ins,int del,int copy){ 6 | if(num==0){ 7 | return 0; 8 | } 9 | if(num==1){ 10 | return ins; 11 | } 12 | if(dp[num]!=-1){ 13 | return dp[num]; 14 | } 15 | else{ 16 | if(num%2==1){ 17 | return dp[num]=min(ins+solution(num-1,ins,del,copy),del+copy+solution((num+1)/2,ins,del,copy)); 18 | } 19 | return dp[num]=min(ins+solution(num-1,ins,del,copy),copy+solution(num/2,ins,del,copy)); 20 | } 21 | } 22 | 23 | int main() { 24 | int T; 25 | cin>>T; 26 | while(T--){ 27 | int num; 28 | cin>>num; 29 | int ins,del,copy; 30 | cin>>ins>>del>>copy; 31 | memset(dp,-1,sizeof(dp)); 32 | cout< 2 | using namespace std; 3 | 4 | int solution(string str1,int M,string str2,int N){ 5 | int dp[M+1][N+1]; 6 | int ans=INT_MIN; 7 | for(int i=0;i<=M;i++){ 8 | for(int j=0;j<=N;j++){ 9 | if(i==0||j==0){ 10 | dp[i][j]=0; 11 | } 12 | else if(str1[i-1]==str2[j-1]){ 13 | dp[i][j]=1+dp[i-1][j-1]; 14 | } 15 | else{ 16 | dp[i][j]=0; 17 | } 18 | ans=max(ans,dp[i][j]); 19 | } 20 | } 21 | return ans; 22 | } 23 | 24 | int main() { 25 | int T; 26 | cin>>T; 27 | while(T--){ 28 | int M,N; 29 | cin>>M>>N; 30 | string str1,str2; 31 | cin>>str1>>str2; 32 | cout< 2 | using namespace std; 3 | #define ull unsigned long long 4 | 5 | int main() { 6 | int T; 7 | cin>>T; 8 | while(T--){ 9 | string str; 10 | cin>>str; 11 | int N=str.length(); 12 | ull sum=0; 13 | ull dp[N]; 14 | dp[0]=str[0]-'0'; 15 | sum=dp[0]; 16 | for(int i=1;i 2 | using namespace std; 3 | 4 | int solution(int A[],int N){ 5 | int excl=0; 6 | int incl=A[0]; 7 | int excl_new; 8 | for(int i=1;i>T; 19 | while(T--){ 20 | int N; 21 | cin>>N; 22 | int A[N]; 23 | for(int i=0;i>A[i]; 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N+1]={0}; 11 | A[0]=1; 12 | for(int i=3;i<=N;i++){ 13 | A[i]+=A[i-3]; 14 | } 15 | for(int i=5;i<=N;i++){ 16 | A[i]+=A[i-5]; 17 | } 18 | for(int i=10;i<=N;i++){ 19 | A[i]+=A[i-10]; 20 | } 21 | cout< 2 | using namespace std; 3 | int dp[26][26]; 4 | 5 | int solution(int no_of_row,int no_of_column){ 6 | if(no_of_row<0 || no_of_column<0){ 7 | return 0; 8 | } 9 | else if(no_of_row==0 || no_of_column==0){ 10 | return 1; 11 | } 12 | else if(dp[no_of_row][no_of_column]!=-1){ 13 | return dp[no_of_row][no_of_column]; 14 | } 15 | else{ 16 | return dp[no_of_row][no_of_column]=solution(no_of_row-1,no_of_column)+solution(no_of_row,no_of_column-1); 17 | } 18 | } 19 | 20 | int main() { 21 | int T; 22 | cin>>T; 23 | while(T--){ 24 | int no_of_row,no_of_column; 25 | cin>>no_of_row>>no_of_column; 26 | dp[no_of_row][no_of_column]; 27 | memset(dp,-1,sizeof(dp)); 28 | cout< 2 | using namespace std; 3 | #define ll long long 4 | ll dp[10001]; 5 | 6 | ll min(ll a,ll b,ll c){ 7 | ll minimum=a>T; 14 | while(T--){ 15 | int N; 16 | cin>>N; 17 | dp[0]=1; 18 | ll index2=0,index3=0,index5=0; 19 | ll value2=2; 20 | ll value3=3; 21 | ll value5=5; 22 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,K; 9 | cin>>N>>K; 10 | int A[N]; 11 | for(int i=0;i>A[i]; 13 | } 14 | sort(A,A+N,greater()); 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N][N]; 11 | for(int i=0;i>A[i][j]; 14 | } 15 | } 16 | map> mp; 17 | for(int i=0;isecond.begin();p!=it->second.end();p++){ 24 | cout<<*p<<" "; 25 | } 26 | } 27 | cout< 2 | using namespace std; 3 | 4 | struct Activity{ 5 | long int start,end,index; 6 | }; 7 | 8 | bool compare(Activity A1, Activity A2){ 9 | return A1.end>T; 15 | while(T--){ 16 | int N; 17 | cin>>N; 18 | Activity A[N]; 19 | for(int i=0;i>A[i].start; 21 | A[i].index=i+1; 22 | } 23 | for(int i=0;i>A[i].end; 25 | } 26 | sort(A,A+N,compare); 27 | int i=0; 28 | cout<=A[i].end){ 31 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N]; 11 | for(int i=0;i>A[i]; 13 | } 14 | map mp; 15 | for(int i=0;isecond)>N/2){ 21 | ans=it->first; 22 | break; 23 | } 24 | } 25 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int len=2*N+2; 11 | int A[len]; 12 | for(int i=0;i>A[i]; 14 | } 15 | map mp; 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | void solution(vector& v,string str,string s,int start,int end){ 5 | if(start==end){ 6 | v.push_back(s); 7 | return ; 8 | } 9 | if(str[start]=='?'){ 10 | solution(v,str,s+'0',start+1,end); 11 | solution(v,str,s+'1',start+1,end); 12 | } 13 | else{ 14 | solution(v,str,s+str[start],start+1,end); 15 | } 16 | } 17 | 18 | int main() { 19 | int T; 20 | cin>>T; 21 | while(T--){ 22 | string str; 23 | cin>>str; 24 | vector v; 25 | int len=str.length(); 26 | string s; 27 | solution(v,str,s,0,len); 28 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool compare(pair p1,pair p2){ 5 | return p1.second>T; 11 | while(T--){ 12 | int N; 13 | cin>>N; 14 | string A[N]; 15 | for(int i=0;i>A[i]; 17 | sort(A[i].begin(),A[i].end()); 18 | } 19 | unordered_map mp; 20 | for(int i=0;i> v(mp.begin(),mp.end()); 24 | sort(v.begin(),v.end(),compare); 25 | for(auto it:v){ 26 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | long long T; 6 | cin>>T; 7 | while(T--){ 8 | long long N; 9 | cin>>N; 10 | long long A[N],sum=0,prod=1; 11 | for(long long i=0;i>A[i]; 13 | } 14 | long long repeating_no,missing_no; 15 | unordered_mapmp; 16 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll solution(ll A[],ll N){ 6 | ll max_sum=INT_MIN; 7 | ll curr_max_sum=0; 8 | for(ll i=0;i0){ 30 | curr_min_sum=0; 31 | } 32 | } 33 | return max(max_sum,total_sum-min_sum); 34 | } 35 | 36 | int main(){ 37 | int T; 38 | cin>>T; 39 | while(T--){ 40 | ll N; 41 | cin>>N; 42 | ll A[N]; 43 | for(ll i=0;i>A[i]; 45 | } 46 | cout< 2 | #include 3 | using namespace std; 4 | using boost::multiprecision::cpp_int; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int n; 11 | cin>>n; 12 | cpp_int dp[n+1]; 13 | dp[0]=1; 14 | dp[1]=1; 15 | for(int i=2;i<=n;i++){ 16 | dp[i]=0; 17 | for(int j=0;j 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int count=0; 11 | while(N){ 12 | if(N%2==1){ 13 | count++; 14 | } 15 | N=N/2; 16 | } 17 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | vector> v; 11 | int profit=0,deadline=0,id=0; 12 | for(int i=0;i>id>>deadline>>profit; 14 | v.push_back(make_pair(profit,deadline)); 15 | } 16 | int max_profit=0,count=0; 17 | sort(v.begin(),v.end(),greater>()); 18 | int slot[N]={0}; 19 | for(int i=0;i=0;j--){ 21 | if(slot[j]==0){ 22 | slot[j]=1; 23 | count++; 24 | max_profit+=v[i].first; 25 | break; 26 | } 27 | } 28 | } 29 | cout< 2 | using namespace std; 3 | #define ull long long 4 | 5 | void solution(ull v[],ull N){ 6 | ull ans[N]={0}; 7 | stack s; 8 | for(int i=N-1;i>=0;i--){ 9 | while(!s.empty() && v[i]>s.top()){ 10 | s.pop(); 11 | } 12 | if(s.empty()){ 13 | ans[i]=-1; 14 | } 15 | else{ 16 | ans[i]=s.top(); 17 | } 18 | s.push(v[i]); 19 | } 20 | for(int i=0;i>T; 29 | while(T--){ 30 | ull N; 31 | cin>>N; 32 | ull v[N]; 33 | for(int i=0;i>v[i]; 35 | } 36 | solution(v,N); 37 | cout< 2 | using namespace std; 3 | 4 | long long solution(string str,long long N){ 5 | unordered_map mp; 6 | for(long long i=0;i temp; 12 | for(long long i=0;i1){ 19 | if(temp[str[start]]>1){ 20 | temp[str[start]]--; 21 | } 22 | start++; 23 | } 24 | min_len=min(min_len,i-start+1); 25 | } 26 | } 27 | return min_len; 28 | } 29 | 30 | int main() { 31 | int T; 32 | cin>>T; 33 | while(T--){ 34 | string str; 35 | cin>>str; 36 | long long N=str.length(); 37 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | vectorv; 11 | for(int i=0;i>temp; 14 | v.push_back(temp); 15 | } 16 | sort(v.begin(),v.end()); 17 | vector :: iterator it; 18 | for(it=v.begin();it!=v.end();it++){ 19 | cout<<*it<<" "; 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | int solution(int row,int col){ 5 | int mat[row][col]; 6 | for(int i=0;i>mat[i][j]; 9 | } 10 | } 11 | int ans=0; 12 | int dp[row+1][col+1]; 13 | for(int i=0;i<=row;i++){ 14 | for(int j=0;j<=col;j++){ 15 | if(i==0 || j==0){ 16 | dp[i][j]=0; 17 | } 18 | else if(mat[i-1][j-1]==1){ 19 | dp[i][j]=min({dp[i-1][j-1],dp[i-1][j],dp[i][j-1]})+1; 20 | ans=max(ans,dp[i][j]); 21 | } 22 | else{ 23 | dp[i][j]=0; 24 | } 25 | } 26 | } 27 | return ans; 28 | } 29 | 30 | int main() { 31 | int T; 32 | cin>>T; 33 | while(T--){ 34 | int row,col; 35 | cin>>row>>col; 36 | cout< 2 | using namespace std; 3 | 4 | void solution(vector& ans, int no_of_digit, int sum){ 5 | if(sum==0 && no_of_digit!=1){ 6 | ans.push_back(-1); 7 | return ; 8 | } 9 | else if(sum > 9*no_of_digit){ 10 | ans.push_back(-1); 11 | return ; 12 | } 13 | for(int i=0;i=9){ 15 | ans.push_back(9); 16 | sum=sum-9; 17 | } 18 | else if(sum==0){ 19 | ans.push_back(0); 20 | } 21 | else{ 22 | ans.push_back(sum); 23 | sum=0; 24 | } 25 | } 26 | return; 27 | } 28 | 29 | int main() { 30 | int T; 31 | cin>>T; 32 | while(T--){ 33 | int no_of_digit=0,sum=0; 34 | cin>>no_of_digit>>sum; 35 | vector ans; 36 | solution(ans,no_of_digit,sum); 37 | for(int i=0;i 2 | using namespace std; 3 | 4 | int solution(int N){ 5 | int sub_count=0; 6 | int div_count=0; 7 | while(N){ 8 | int temp=N%2; 9 | if(temp==1){ 10 | sub_count++; 11 | } 12 | div_count++; 13 | N=N/2; 14 | } 15 | return (div_count+sub_count-1); 16 | } 17 | 18 | int main() { 19 | int T; 20 | cin>>T; 21 | while(T--){ 22 | int N; 23 | cin>>N; 24 | cout< 2 | using namespace std; 3 | 4 | struct Activity{ 5 | long int start,end,index; 6 | }; 7 | 8 | bool compare(Activity A1, Activity A2){ 9 | return A1.end>T; 15 | while(T--){ 16 | int N; 17 | cin>>N; 18 | Activity A[N]; 19 | for(int i=0;i>A[i].start; 21 | A[i].index=i+1; 22 | } 23 | for(int i=0;i>A[i].end; 25 | } 26 | sort(A,A+N,compare); 27 | int i=0; 28 | cout<=A[i].end){ 31 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N=0,K=0; 9 | cin>>N>>K; 10 | int A[N]; 11 | for(int i=0;i>A[i]; 13 | } 14 | int i=0; 15 | sort(A,A+N); 16 | int sum=0; 17 | for(i=0;iK){ 20 | break; 21 | } 22 | } 23 | cout< 2 | using namespace std; 3 | 4 | int solution_min(int A[],int N,int K){ 5 | sort(A,A+N); 6 | int sum=0,i=0,j=N-1; 7 | while(i<=j){ 8 | sum=sum+A[i]; 9 | j=j-K; 10 | i++; 11 | } 12 | return sum; 13 | } 14 | 15 | int solution_max(int A[],int N,int K){ 16 | sort(A,A+N,greater()); 17 | int sum=0,i=0,j=N-1; 18 | while(i<=j){ 19 | sum=sum+A[i]; 20 | j=j-K; 21 | i++; 22 | } 23 | return sum; 24 | } 25 | 26 | int main() { 27 | int T; 28 | cin>>T; 29 | while(T--){ 30 | int N=0,K=0; 31 | cin>>N>>K; 32 | int A[N]; 33 | for(int i=0;i>A[i]; 35 | } 36 | int min=solution_min(A,N,K); 37 | int max=solution_max(A,N,K); 38 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,weight; 9 | cin>>N>>weight; 10 | vector>> ans; 11 | double value, item; 12 | for(int i=0;i>value>>item; 14 | ans.push_back(make_pair((double)(value/item),make_pair(value,item))); 15 | } 16 | double solution=0; 17 | int i=N-1; 18 | sort(ans.begin(),ans.end()); 19 | while(weight && i>=0){ 20 | if(ans[i].second.second<=weight){ 21 | weight=weight-ans[i].second.second; 22 | solution=solution+ans[i].second.first; 23 | } 24 | else{ 25 | solution=solution + (weight*(ans[i].first)); 26 | break; 27 | } 28 | i--; 29 | } 30 | //ans.clear(); 31 | cout< 2 | using namespace std; 3 | 4 | void solution(vector& ans,int no_of_digit,int sum){ 5 | if(sum>9*no_of_digit){ 6 | ans.push_back(-1); 7 | return ; 8 | } 9 | else{ 10 | for(int i=0;i9){ 12 | ans.push_back(9); 13 | sum=sum-9; 14 | } 15 | else if(sum==0){ 16 | ans.push_back(0); 17 | } 18 | else if(sum<=9){ 19 | ans.push_back(sum); 20 | sum=0; 21 | } 22 | } 23 | return ; 24 | } 25 | } 26 | 27 | int main() { 28 | int T; 29 | cin>>T; 30 | while(T--){ 31 | int N,S; 32 | cin>>N>>S; 33 | vector ans; 34 | solution(ans,N,S); 35 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | unsigned long long int num; 9 | cin>>num; 10 | unsigned long long int A[]={1,1,2,6,24,120,720,5040,40320,362880}; 11 | int i=9; 12 | vector ans; 13 | while(num && i>=0){ 14 | if(A[i]==num){ 15 | ans.push_back(i); 16 | break; 17 | } 18 | else if(num>A[i]){ 19 | num=num-A[i]; 20 | ans.push_back(i); 21 | } 22 | if(num=0;i--){ 27 | cout< 2 | using namespace std; 3 | 4 | void solution(float survival_days,float food_bought,float food_required){ 5 | if(food_bought6 && ((food_bought-food_required)*6 < food_required)){ 10 | cout<<-1; 11 | return ; 12 | } 13 | else{ 14 | float temp=(food_bought/food_required); 15 | //cout<<"temp "<>T; 27 | while(T--){ 28 | float survival_days,food_bought,food_required; 29 | cin>>survival_days>>food_bought>>food_required; 30 | solution(survival_days,food_bought,food_required); 31 | cout< 2 | using namespace std; 3 | 4 | void solution(vector& ans,int N){ 5 | for(int i=0;i9){ 14 | ans.push_back(9); 15 | N=N-9; 16 | } 17 | } 18 | return ; 19 | } 20 | 21 | int main() { 22 | int T; 23 | cin>>T; 24 | while(T--){ 25 | int N; 26 | cin>>N; 27 | vector ans; 28 | solution(ans,N); 29 | for(int i=ans.size()-1;i>=0;i--){ 30 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | long int N; 9 | cin>>N; 10 | string str; 11 | cin>>str; 12 | int j=0,count=0; 13 | stack s; 14 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int len,money; 9 | cin>>len>>money; 10 | int A[len]; 11 | for(int i=0;i>A[i]; 13 | } 14 | map mp; 15 | for(int i=0;i0){ 21 | continue; 22 | } 23 | else if(mp.count(i)==0 && money>0){ 24 | count++; 25 | money=money-i; 26 | } 27 | else if(money<=0){ 28 | break; 29 | } 30 | } 31 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | vector> v; 11 | for(int i=0;i>x>>y; 14 | v.push_back(make_pair(x+y,i+1)); 15 | sort(v.begin(),v.end()); 16 | } 17 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | string str; 9 | cin>>str; 10 | string cp=str; 11 | sort(cp.begin(),cp.end()); 12 | int i; 13 | char c1='&',c2='&'; 14 | int flag=0; 15 | for(i=0;icp[i]){ 19 | c1=str[j]; 20 | c2=cp[i]; 21 | flag=1; 22 | break; 23 | } 24 | } 25 | if(flag==1) 26 | break; 27 | } 28 | for(int i=0;i 2 | using namespace std; 3 | 4 | int solution(int A[],int N,int K){ 5 | if(N==1){ 6 | return 0; 7 | } 8 | sort(A,A+N); 9 | int diff=A[N-1]-A[0]; 10 | int small=A[0]+K; 11 | int big=A[N-1]-K; 12 | if(small>big){ 13 | swap(small,big); 14 | } 15 | for(int i=1;i=small || add<=big){ 19 | continue; 20 | } 21 | if(big-sub<=add-small){ 22 | small=sub; 23 | } 24 | else{ 25 | big=add; 26 | } 27 | } 28 | return min(diff,big-small); 29 | } 30 | 31 | int main() { 32 | int T; 33 | cin>>T; 34 | while(T--){ 35 | int K,N; 36 | cin>>K>>N; 37 | int A[N]; 38 | for(int i=0;i>A[i]; 40 | } 41 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | vector> v; 11 | int profit=0,deadline=0,id=0; 12 | for(int i=0;i>id>>deadline>>profit; 14 | v.push_back(make_pair(profit,deadline)); 15 | } 16 | int max_profit=0,count=0; 17 | sort(v.begin(),v.end(),greater>()); 18 | int slot[N]={0}; 19 | for(int i=0;i=0;j--){ 21 | if(slot[j]==0){ 22 | slot[j]=1; 23 | count++; 24 | max_profit+=v[i].first; 25 | break; 26 | } 27 | } 28 | } 29 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N]; 11 | for(int i=0;i>A[i]; 13 | } 14 | int k,page_fault=0; 15 | cin>>k; 16 | int j=0; 17 | vector v; 18 | vector :: iterator it; 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,K; 9 | cin>>N>>K; 10 | long long int A[N]; 11 | for(int i=0;i>A[i]; 13 | } 14 | sort(A,A+N); 15 | long long int temp=0,sum=0,ans=INT_MAX; 16 | for(int i=0;ii;j--){ 20 | if(A[j]-A[i]-K>0){ 21 | temp=temp+A[j]-A[i]-K; 22 | } 23 | } 24 | ans=min(ans,temp); 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | void solution(long long int dp[],int N){ 5 | if(N>=2){ 6 | for(int i=2;i<=N;i++){ 7 | dp[i]=dp[i-1]+dp[i-2]; 8 | } 9 | } 10 | for(int i=0;i>T; 20 | while(T--){ 21 | int N; 22 | cin>>N; 23 | long long int dp[N+1]; 24 | dp[0]=dp[1]=1; 25 | solution(dp,N); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Deepak Kumar/Day 8/2. Padovan Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solution(int N){ 5 | int dp[N]; 6 | dp[0]=dp[1]=dp[2]=1; 7 | for(int i=3;i<=N;i++){ 8 | dp[i]=((dp[i-2]+dp[i-3])%1000000007); 9 | } 10 | return dp[N] ; 11 | } 12 | 13 | int main() { 14 | int T; 15 | cin>>T; 16 | while(T--){ 17 | int N; 18 | cin>>N; 19 | cout< 2 | using namespace std; 3 | int dp[1000001]; 4 | 5 | int solution(int N){ 6 | dp[0]=dp[1]=dp[2]=dp[3]=0; 7 | dp[4]=1; 8 | for(int i=5;i<=N;i++){ 9 | int num=i; 10 | dp[i]=dp[i-1]; 11 | while(num){ 12 | int temp=num%10; 13 | if(temp==4){ 14 | dp[i]=dp[i-1]+1; 15 | } 16 | num=num/10; 17 | } 18 | } 19 | return dp[N]; 20 | } 21 | 22 | int main() { 23 | int T; 24 | cin>>T; 25 | while(T--){ 26 | int N; 27 | cin>>N; 28 | cout< 2 | using namespace std; 3 | 4 | int solution(int dp[],int A[],int N){ 5 | if(N<=0){ 6 | return 0; 7 | } 8 | for(int i=1;iA[j]){ 11 | dp[i]=max(dp[i],A[i]+dp[j]); 12 | } 13 | } 14 | } 15 | int maximum=INT_MIN; 16 | for(int i=0;i>T; 25 | while(T--){ 26 | int N; 27 | cin>>N; 28 | int A[N]; 29 | int dp[N]; 30 | for(int i=0;i>A[i]; 32 | dp[i]=A[i]; 33 | } 34 | cout< 2 | using namespace std; 3 | long long int dp[101][101]; 4 | 5 | int main() { 6 | int T; 7 | cin>>T; 8 | while(T--){ 9 | int M,N; 10 | cin>>M>>N; 11 | for(int i=0;i 2 | using namespace std; 3 | int dp[10001]; 4 | 5 | int solution(int A[],int N){ 6 | if(N<=0){ 7 | return 0; 8 | } 9 | dp[0]=A[0]; 10 | dp[1]=max(A[0],A[1]); 11 | for(int i=2;i>T; 20 | while(T--){ 21 | int N; 22 | cin>>N; 23 | int A[N]; 24 | int dp[N]; 25 | for(int i=0;i>A[i]; 27 | } 28 | memset(dp,-1,sizeof(dp)); 29 | cout< 2 | using namespace std; 3 | 4 | unsigned long long int findMinSum(unsigned long long int A[],unsigned long long int B[], int N); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | cin>>N; 13 | unsigned long long int A[N],B[N]; 14 | for(int i=0;i>A[i]; 16 | for(int i=0;i>B[i]; 18 | sort(A,A+N); 19 | sort(B,B+N); 20 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int chocolate[N]; 11 | for(int i=0;i>chocolate[i]; 13 | 14 | cout<<*min_element(chocolate,chocolate+N)< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int sumOfDigits,totalDigits,j=0; 9 | string num = ""; 10 | bool notFound; 11 | cin>>sumOfDigits; 12 | cin>>totalDigits; 13 | for(int i=0;i=sumOfDigits){ 21 | sumOfDigits-=j; 22 | num+=to_string(j); 23 | notFound = false; 24 | break; 25 | } 26 | } 27 | if(notFound){ 28 | break; 29 | } 30 | } 31 | if(notFound) 32 | cout<<-1< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | cout<<2*(N-1)< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | int A[N],newArray[N],k=0,sum=0; 11 | for(int i=0;i>A[i]; 13 | } 14 | sort(A,A+N); 15 | int totalElements = 0; 16 | for(int i=0,j=N-1;i 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | long long int jumps[51]; 8 | int max=4; 9 | jumps[0]=0; 10 | jumps[1]=1; jumps[2]=2;jumps[3]=4; 11 | while(T--){ 12 | int N; 13 | cin>>N; 14 | for(int i=max;i<=N;i++){ 15 | jumps[i] = jumps[i-1]+jumps[i-2]+jumps[i-3]; 16 | } 17 | max = max>N?max:N; 18 | cout< 2 | #include 3 | using namespace std; 4 | using namespace boost::multiprecision; 5 | 6 | int main() 7 | { 8 | int256_t catalan[101]={0}; 9 | int max=1; 10 | catalan[0] = catalan[1] = 1; 11 | int T; 12 | cin>>T; 13 | while(T--){ 14 | int N; 15 | cin>>N; 16 | for(int i=max+1;i<=N;i++){ 17 | for(int j=0;jN?max:N; 23 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | long long int jumps[91]={0}; 6 | int max=3; 7 | jumps[1]=1; jumps[2]=2; 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | cin>>N; 13 | for(int i=max;i<=N;i++){ 14 | jumps[i]=jumps[i-1]+jumps[i-2]; 15 | } 16 | max=max>N?max:N; 17 | cout<>T; 8 | while(T--){ 9 | string s; 10 | cin>>s; 11 | cout< 2 | using namespace std; 3 | 4 | long long int FindPaths(int N,int M); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,M; 12 | cin>>N>>M; 13 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | long long int friends[101]; 6 | int max=3; 7 | friends[0]=0;friends[1]=1;friends[2]=2; 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | cin>>N; 13 | int mod = 1000000007; 14 | for(int i=max;i<=N;i++){ 15 | friends[i] = (friends[i-1]+((i-1)*friends[i-2])%mod)%mod; 16 | } 17 | max=max>N?max:N; 18 | cout< 2 | using namespace std; 3 | 4 | int cutRod(int arr[],int N); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | cin>>N; 13 | int rods[N]; 14 | for(int i=0;i>rods[i]; 16 | cout< 2 | using namespace std; 3 | 4 | int knapSack(int val[],int wt[],int N,int cap); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,cap; 12 | cin>>N>>cap; 13 | int val[N],wt[N]; 14 | for(int i=0,j=0;i>val[i++]; 17 | else 18 | cin>>wt[j++]; 19 | } 20 | cout< 2 | using namespace std; 3 | 4 | long long int FindPaths(int N,int M); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,M; 12 | cin>>N>>M; 13 | N++,M++; 14 | cout< 2 | using namespace std; 3 | 4 | int CountStrings(int len,int Bs,int Cs,int dp[][2][3]); 5 | 6 | int main() 7 | { 8 | long long int strings[1001]; 9 | strings[0] = 0; 10 | strings[1] = 3; 11 | strings[2] = 4; 12 | int max = 3; 13 | int T; 14 | cin>>T; 15 | while(T--){ 16 | int N; 17 | cin>>N; 18 | int dp[N+1][2][3]; 19 | memset(dp,-1,sizeof(dp)); 20 | cout< 2 | using namespace std; 3 | 4 | bool findEqualSets(int set[],int sum,int size); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | cin>>N; 13 | int set[N]; 14 | for(int i=0;i>set[i]; 16 | int sum = accumulate(set,set+N,0); 17 | if(sum%2==0) 18 | findEqualSets(set,sum/2,N)==1?cout<<"YES"< 2 | using namespace std; 3 | 4 | bool isDivisible(int arr[],int sum,int size,int divisor); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,K; 12 | cin>>N>>K; 13 | int arr[N]; 14 | for(int i=0;i>arr[i]; 16 | int sum = accumulate(arr,arr+N,0); 17 | cout<& prices) { 4 | if(prices.size()<2) 5 | return 0; 6 | int maxProfit=0,buy=prices[0],sell; 7 | for(int i=1;i=prices[i]){ 9 | buy = prices[i]; 10 | }else{ 11 | sell = prices[i]; 12 | if(sell-buy>maxProfit) 13 | maxProfit = sell-buy; 14 | } 15 | } 16 | return maxProfit; 17 | } 18 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 13/60. Min Cost Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | cost.push_back(0); 5 | int minCost[cost.size()]; 6 | minCost[0] = cost[0]; minCost[1] = cost[1]; 7 | for(int i=2;i& nums) { 4 | int maxSum=nums[0],sum=nums[0]; 5 | for(int i=1;i=nums[i]){ 7 | sum+=nums[i]; 8 | maxSum=max(maxSum,sum); 9 | }else{ 10 | sum=nums[i]; 11 | maxSum=max(maxSum,sum); 12 | } 13 | } 14 | return maxSum; 15 | } 16 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 14/64. Range Sum Query - Immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | int *sumArray; 4 | NumArray(vector& nums) { 5 | sumArray = new int[nums.size()]; 6 | int sum=0; 7 | for(int i=0;i=0){ 16 | return sumArray[j]-sumArray[i]; 17 | } 18 | return sumArray[j]; 19 | } 20 | }; 21 | 22 | /** 23 | * Your NumArray object will be instantiated and called as such: 24 | * NumArray* obj = new NumArray(nums); 25 | * int param_1 = obj->sumRange(i,j); 26 | */ -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 14/66. Count Square Submatrices with All Ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSquares(vector>& matrix) { 4 | int n = matrix.size(),m = matrix[0].size(); 5 | int dp[n+1][m+1],sum=0; 6 | memset(dp,0,sizeof(dp)); 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=m;j++){ 9 | //min of above cell, left cell and diagonal cell 10 | if(matrix[i-1][j-1]==1){ 11 | dp[i][j] = min(dp[i-1][j-1],min(dp[i-1][j], 12 | dp[i][j-1])) 13 | +matrix[i-1][j-1]; 14 | sum+=dp[i][j]; 15 | } 16 | } 17 | } 18 | return sum; 19 | } 20 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 14/67. Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int num) { 4 | vector Ones; 5 | for(int i=0;i<=num;i++){ 6 | Ones.push_back(__builtin_popcount(i)); //Used to count number of ones in 7 | //binary representation 8 | } 9 | return Ones; 10 | } 11 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 14/68. Stone Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool stoneGame(vector& piles) { 4 | return true; 5 | } 6 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 14/69. Minimum Falling Path Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minFallingPathSum(vector>& dp) { 4 | int n = dp.size(); 5 | if(n==1) 6 | return dp[0][0]; 7 | for(int row=1;row 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,K; 9 | long long mod = pow(10,9)+7; 10 | cin>>N>>K; 11 | long long int dp[N+1]={0}; 12 | dp[1] = K,dp[2] = K*K; 13 | for(int i=3;i<=N;i++) 14 | dp[i] = (long long)((K-1)*(dp[i-1]+dp[i-2])%mod); 15 | 16 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | cin>>n; 10 | if(n%9==0) 11 | cout<<9< 2 | using namespace std; 3 | int main() 4 | { 5 | long long int nCr[1001]; 6 | nCr[0] = 1; 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | long long int N,res=1; 11 | long long int mod = pow(10,9)+7; 12 | cin>>N; 13 | for(long long int i=1;i 2 | using namespace std; 3 | 4 | long long int findSOS(int m,int n); 5 | 6 | int main() 7 | { 8 | long long int dp[101]; 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int m,n; 13 | cin>>m>>n; 14 | cout<j) 26 | SOS[i][j]=0; 27 | else if(i==1) 28 | SOS[i][j]=j; 29 | else{ 30 | SOS[i][j] = SOS[i][j-1]+SOS[i-1][j/2]; 31 | } 32 | } 33 | } 34 | return SOS[n][m]; 35 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 15/75. Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findLCS(string s1,string s2); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,M; 12 | string s1,s2; 13 | cin>>N>>M; 14 | cin>>s1>>s2; 15 | cout< 2 | using namespace std; 3 | 4 | int findLRS(string s); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | string s; 13 | cin>>N>>s; 14 | cout<=j) 27 | LRS[i][j] = 0; 28 | else if(s[i-1] == s[j-1]){ 29 | LRS[i][j] = LRS[i-1][j-1]+1; 30 | if(LRS[i][j]>maximum) 31 | maximum =LRS[i][j]; 32 | } 33 | else{ 34 | LRS[i][j] = max(LRS[i-1][j],LRS[i][j-1]); 35 | } 36 | } 37 | } 38 | return maximum; 39 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 15/77. Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct LIS{ 5 | int value,LIS; 6 | }; 7 | 8 | int findLIS(LIS arr[],int N); 9 | 10 | int main() 11 | { 12 | int T; 13 | cin>>T; 14 | while(T--){ 15 | int N; 16 | cin>>N; 17 | LIS arr[N]; 18 | for(int i=0;i>arr[i].value; 20 | arr[i].LIS = 1; 21 | } 22 | cout<arr[j].value and 32 | arr[i].LIS<=arr[j].LIS){ 33 | arr[i].LIS = max(arr[j].LIS+1,arr[i].LIS); 34 | maxLIS = maxLIS>arr[i].LIS?maxLIS:arr[i].LIS; 35 | } 36 | } 37 | } 38 | return maxLIS; 39 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 15/78. Maximum sum increasing subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //LIS ---> Longest Increasing Sum 5 | 6 | struct Sequence{ 7 | int value, LIS; 8 | }; 9 | 10 | int MSIS(Sequence seq[], int N); 11 | 12 | int main() 13 | { 14 | int T; 15 | cin>>T; 16 | while(T--){ 17 | int N; 18 | cin>>N; 19 | int x; 20 | Sequence seq[N]; 21 | for(int i=0;i>x; 23 | seq[i].value = seq[i].LIS = x; 24 | } 25 | cout<seq[i].LIS?maximumSum:seq[i].LIS; 42 | } 43 | return maximumSum; 44 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 15/79. LCS of three strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findLCS(string s1,string s2,string s3); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,M,L; 12 | string s1,s2,s3; 13 | cin>>N>>M>>L; 14 | cin>>s1>>s2>>s3; 15 | cout< 2 | using namespace std; 3 | 4 | struct LS{ 5 | int val,LIS=1; 6 | }; 7 | 8 | int main() 9 | { 10 | int T; 11 | cin>>T; 12 | while(T--){ 13 | int N,maxLS=1; 14 | cin>>N; 15 | LS A[N]; 16 | for(int i=0;i>A[i].val; 18 | for(int i=1;i 2 | using namespace std; 3 | 4 | int maxSum(long long int n,long long int maxSumDp[],long long int &maxFound); 5 | 6 | int main() 7 | { 8 | long long int MaxSumDp[100001]; 9 | long long int maxFound=4; 10 | MaxSumDp[0] = 0; 11 | MaxSumDp[1] = 1,MaxSumDp[2] = 2,MaxSumDp[3] = 3,MaxSumDp[4] = 4; 12 | int T; 13 | cin>>T; 14 | while(T--){ 15 | int n; 16 | cin>>n; 17 | 18 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,M,maxSquare=0; 9 | cin>>N>>M; 10 | N++,M++; 11 | int matrix[N][M]; 12 | memset(matrix,0,sizeof(matrix)); 13 | for(int i=1;i>matrix[i][j]; 16 | } 17 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin>>T; 8 | while(T--){ 9 | int N,K,sum=0; 10 | cin>>N; 11 | int arr[N]; 12 | for(int i=0;i>arr[i]; 14 | cin>>K; 15 | sort(arr,arr+N); 16 | for(int i=N-1,j=N-2;i>=0 and j>=0;){ 17 | if(abs(arr[i]-arr[j]) 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n,maxSum=0; 9 | cin>>n; 10 | int matrix[2][n]; 11 | for(int i=0;i<2;i++) 12 | for(int j=0;j>matrix[i][j]; 14 | 15 | 16 | int dp[n+1]; 17 | dp[0] = 0; 18 | dp[1] = max(matrix[0][0],matrix[1][0]); 19 | for(int i=2;i<=n;i++){ 20 | dp[i] = max(max(matrix[0][i-1],matrix[1][i-1])+dp[i-2],dp[i-1]); 21 | } 22 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int dp[10001]; 6 | dp[0] = 0,dp[1] = 0,dp[2] = 1,dp[3] = 1; 7 | int maxFound=3; 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int n,steps=0; 12 | cin>>n; 13 | for(int i=maxFound+1;i<=n;i++){ 14 | dp[i] = min(dp[i/2]+i%2+1,dp[i/3]+i%3+1); 15 | } 16 | maxFound = max(maxFound,n); 17 | cout< 2 | using namespace std; 3 | 4 | void findNonRepeating(int arr[],int n); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int n; 12 | cin>>n; 13 | n = 2*n+2; 14 | int arr[n]; 15 | for(int i=0;i>arr[i]; 17 | findNonRepeating(arr,n); 18 | } 19 | return 0; 20 | } 21 | 22 | void findNonRepeating(int arr[],int n){ 23 | int set_bit_no,xor_ = arr[0],x=0,y=0; 24 | for(int i=1;i 2 | using namespace std; 3 | 4 | unordered_setus; 5 | 6 | void generateBinaryString(string s,int i,int n,int count); 7 | 8 | int main() 9 | { 10 | int T; 11 | cin>>T; 12 | while(T--){ 13 | us.clear(); 14 | string s; 15 | cin>>s; 16 | int n = s.length(),count=0; 17 | for(int i=0;i=n){ 29 | cout<0){ 38 | generateBinaryString(s,i+1,n,count); 39 | } 40 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 18/110.Group Anagrams Together.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int T; 7 | cin>>T; 8 | while(T--){ 9 | int n,count=0; 10 | cin>>n; 11 | string s[n]; 12 | vectorgroups; 13 | vectorisAnagram(n,false); 14 | for(int i=0;i>s[i]; 16 | for(int i=0;i& arr, int k) { 4 | long long maxSum,arraySum = accumulate(arr.begin(),arr.end(),0); 5 | maxSum = kadaneAlgo(arr,arr.size(),2); 6 | return (maxSum + (arraySum>0?(k-2)*arraySum:0))%1000000007; 7 | } 8 | long long kadaneAlgo(vector& arr,int n,int k){ 9 | long long sum=0,maxSum=0; 10 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n,k; 9 | cin>>n>>k; 10 | int arr[n]; 11 | for(int i=0;i>arr[i]; 13 | priority_queuepq; 14 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n,totalDiagonals; 9 | cin>>n; 10 | int matrix[n][n]; 11 | for(int i=0;i>matrix[i][j]; 14 | 15 | totalDiagonals = n*2-1; 16 | int UD = (totalDiagonals/2)+1,LD = totalDiagonals/2; 17 | for(int d=0;d=0 and i<=d;j--,i++) 19 | cout<0;d--){ 22 | for(int j=n-1,i=UD-d;i 2 | using namespace std; 3 | 4 | struct Metting{ 5 | int start,end,index; 6 | }; 7 | 8 | bool CompareMettings(Metting a, Metting b){ 9 | return a.end>T; 18 | while(T--){ 19 | int N; 20 | cin>>N; 21 | Metting mettings[N]; 22 | for(int i=0;i>mettings[i].start; 24 | mettings[i].index = i; 25 | } 26 | for(int i=0;i>mettings[i].end; 28 | sort(mettings, mettings+N, CompareMettings); 29 | findMaxMettings(mettings,N); 30 | } 31 | return 0; 32 | } 33 | 34 | void findMaxMettings(Metting mettings[],int N){ 35 | int j=0; 36 | cout<=mettings[j].end){ 39 | j = i; 40 | cout< 2 | using namespace std; 3 | 4 | int MooreAlgo(int arr[],int n); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int n; 12 | cin>>n; 13 | int arr[n]; 14 | for(int i=0;i>arr[i]; 16 | cout<n/2) 38 | return majorityEle; 39 | return -1; 40 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 18/123.Generalised Fibonacci numbers.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kushagra414/Dynamic-Programming/32e1faac325d2ed3479abf3cef4a7c2470356667/Kushagra Shekhawat/Day 18/123.Generalised Fibonacci numbers.cpp -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 19/124.Find Missing And Repeating.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to Find the repeating 2 | // and missing elements 3 | 4 | #include 5 | using namespace std; 6 | 7 | void printTwoElements(int arr[], int size) 8 | { 9 | int i; 10 | cout << " The repeating element is "; 11 | 12 | for (i = 0; i < size; i++) { 13 | if (arr[abs(arr[i]) - 1] > 0) 14 | arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1]; 15 | else 16 | cout << abs(arr[i]) << "\n"; 17 | } 18 | 19 | cout << "and the missing element is "; 20 | for (i = 0; i < size; i++) { 21 | if (arr[i] > 0) 22 | cout << (i + 1); 23 | } 24 | } 25 | 26 | /* Driver code */ 27 | int main() 28 | { 29 | int arr[] = { 7, 3, 4, 5, 5, 6, 2 }; 30 | int n = sizeof(arr) / sizeof(arr[0]); 31 | printTwoElements(arr, n); 32 | } 33 | 34 | // This code is contributed by Shivi_Aggarwal 35 | -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 19/126.Next larger element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void findNextLarger(long long int array[], long long int N); 5 | 6 | int main() { 7 | int T; 8 | scanf("%d",&T); 9 | while(T--){ 10 | long long int N; 11 | scanf("%lld",&N); 12 | long long int array[N]; 13 | for(int i=0;i s; 24 | for(int i=N-1;i>=0;i--){ 25 | while(!s.empty()&&array[i]>s.top()) 26 | s.pop(); 27 | ans[i] = s.empty()?-1:s.top(); 28 | s.push(array[i]); 29 | } 30 | for(int i=0;i>T; 6 | while(T--){ 7 | string str; 8 | int len = 0,temp,minLen=INT_MAX,count=0; 9 | cin>>str; 10 | int Strlen = str.length(); 11 | int Patlen = 0; 12 | int strAscii[256]={0},patAscii[256]={0}; 13 | for(int i=0;ipatAscii[str[j]] or patAscii[str[j]]==0){ 25 | 26 | if(strAscii[str[j]]>patAscii[str[j]]) 27 | strAscii[str[j]]--; 28 | j++; 29 | } 30 | temp = i-j+1; 31 | if(temp 3 | using namespace std; 4 | 5 | struct Element{ 6 | int val,row,col; 7 | }; 8 | struct Compare{ 9 | bool operator()(Element e1,Element e2){ 10 | return e1.val>e2.val; 11 | } 12 | }; 13 | 14 | int main() 15 | { 16 | int T; 17 | cin>>T; 18 | while(T--){ 19 | int n,val; 20 | cin>>n; 21 | Element matrix[n][n],temp; 22 | for(int i=0;i>val; 25 | matrix[i][j].val = val; 26 | matrix[i][j].row = i; 27 | matrix[i][j].col = j; 28 | } 29 | priority_queue, Compare>pq; 30 | for(int j=0;j<1;j++){ 31 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,M,maxSquare=0; 9 | cin>>N>>M; 10 | N++,M++; 11 | int matrix[N][M]; 12 | memset(matrix,0,sizeof(matrix)); 13 | for(int i=1;i>matrix[i][j]; 16 | } 17 | for(int i=1;i 2 | using namespace std; 3 | 4 | int ModifiedKadane(int arr[],int n); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int n; 12 | cin>>n; 13 | int arr[n]; 14 | for(int i=0;i>arr[i]; 16 | } 17 | cout< 2 | #include 3 | using namespace std; 4 | using namespace boost::multiprecision; 5 | 6 | int main() 7 | { 8 | int256_t catalan[101]={0}; 9 | int max=1; 10 | catalan[0] = catalan[1] = 1; 11 | int T; 12 | cin>>T; 13 | while(T--){ 14 | int N; 15 | cin>>N; 16 | for(int i=max+1;i<=N;i++){ 17 | for(int j=0;jN?max:N; 23 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | cin>>n; 10 | cout<<__builtin_popcount(n)< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,K,minCost=0,maxCost=0; 9 | cin>>N>>K; 10 | int candy[N]; 11 | for(int i=0;i>candy[i]; 13 | sort(candy,candy+N); 14 | int end = N; 15 | for(int i=0;i=end;i--){ 21 | maxCost+=candy[i]; 22 | end+=K; 23 | } 24 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,S,sum=0,lowerLimit=0; 9 | string num=""; 10 | bool notFound; 11 | cin>>N; 12 | cin>>S; 13 | for(int i=0;i=lowerLimit;j--){ 20 | if(j+sum<=S){ 21 | sum+=j; 22 | num+=to_string(j); 23 | notFound = false; 24 | break; 25 | } 26 | } 27 | if(notFound) 28 | break; 29 | } 30 | if(notFound or sum != S) 31 | cout<<-1< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,num,steps = 0; 9 | cin>>N; 10 | num=N; 11 | while(num>0){ 12 | if(num%2 == 0){ 13 | steps++; 14 | num/=2; 15 | } 16 | else{ 17 | steps++; 18 | num--; 19 | } 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | struct Metting{ 5 | int start,end,index; 6 | }; 7 | 8 | bool CompareMettings(Metting a, Metting b){ 9 | return a.end>T; 18 | while(T--){ 19 | int N; 20 | cin>>N; 21 | Metting mettings[N]; 22 | for(int i=0;i>mettings[i].start; 24 | mettings[i].index = i; 25 | } 26 | for(int i=0;i>mettings[i].end; 28 | sort(mettings, mettings+N, CompareMettings); 29 | findMaxMettings(mettings,N); 30 | } 31 | return 0; 32 | } 33 | 34 | void findMaxMettings(Metting mettings[],int N){ 35 | int j=0; 36 | cout<=mettings[j].end){ 39 | j = i; 40 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,K,totalToys=0; 9 | cin>>N>>K; 10 | int toys[N]; 11 | for(int i=0;i>toys[i]; 13 | } 14 | sort(toys,toys+N); 15 | for(int i=0;i>T; 6 | while(T--){ 7 | int v,e,x,y; 8 | cin>>v>>e; 9 | vectorvertices[v]; 10 | for(int i=0;i>x>>y; 12 | vertices[x].push_back(y); 13 | vertices[y].push_back(x); 14 | } 15 | for(int i=0;i "< 2 | #define INT 32 3 | using namespace std; 4 | 5 | int findTheElement(vector arr); 6 | 7 | int main(){ 8 | int t; 9 | cin>>t; 10 | while(t--){ 11 | int n; 12 | cin>>n; 13 | vector arr(n); 14 | for(int i=0;i>arr[i]; 16 | cout< arr){ 21 | vector countBits(32,0); 22 | string SingleElement=""; 23 | for(auto element:arr){ 24 | bitset binaryRepresentation(element); 25 | cout< 2 | using namespace std; 3 | 4 | #define INT 32 5 | 6 | bool areOpposite(int a,int b); 7 | 8 | int main(){ 9 | int t; 10 | cin>>t; 11 | while(t--){ 12 | int a,b; 13 | cin>>a>>b; 14 | cout< 2 | using namespace std; 3 | 4 | #define INT 32 5 | 6 | int multiplyBy3_5(int a); 7 | 8 | int main(){ 9 | int t; 10 | cin>>t; 11 | while(t--){ 12 | int a; 13 | cin>>a; 14 | cout<>1; 26 | // now add 2x + 0.5x + x 27 | return (a2+aBy2+a); 28 | 29 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 23/178.Turn off the rightmost set bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int turnOffRightMostBit(int a); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int a; 11 | cin>>a; 12 | cout< 3 | using namespace std; 4 | 5 | int isPowerOfFour(unsigned int n); 6 | 7 | /*Driver program to test above function*/ 8 | int main() 9 | { 10 | int t,n; 11 | cin>>t; 12 | while(t--) 13 | { 14 | cin>>n; 15 | if(isPowerOfFour(n)) 16 | cout<<1< 2 | using namespace std; 3 | 4 | int findModulus(unsigned int a,unsigned int powerOf2); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | unsigned int a,powerOf2; 11 | cin>>a>>powerOf2; 12 | cout<>shift; 22 | powerOf2 = powerOf2>>shift; 23 | return (a | powerOf2); 24 | 25 | //or simply return 26 | //return (a&(powerOf2-1)); 27 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 23/181.Rotate Bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define bits 16 3 | using namespace std; 4 | typedef unsigned short int usi; 5 | 6 | usi leftCircularShift(usi n,usi d); 7 | usi rightCircularShift(usi n,usi d); 8 | 9 | int main() 10 | { 11 | int T; 12 | cin>>T; 13 | while(T--){ 14 | usi n,d; 15 | cin>>n>>d; 16 | d%=bits; 17 | cout<>(bits-d)); 25 | } 26 | usi rightCircularShift(usi n,usi d){ 27 | return (n>>d | n<<(bits-d)); 28 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 23/182.Find the Number Occurring Odd Number of Times.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n,xor_=0; 9 | cin>>n; 10 | int arr[n]; 11 | for(int i=0;i>arr[i]; 13 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | cin>>n; 10 | cout<<__builtin_popcount(n)< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int a,b; 9 | cin>>a>>b; 10 | cout<<__builtin_popcount(a^b)< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | long long int n; 9 | cin>>n; 10 | if(n>0 and !(n&(n-1))) 11 | cout<<"YES"< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | cin>>n; 10 | //n = n&(-n); //to get the right most bit 11 | // then do log2(n) 12 | //but the best is using ffs() function which returns index of 13 | //least significant bit 14 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | cin>>n; 10 | bitset<14> binary(n); 11 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | //Your code here 9 | unsigned int n; 10 | cin>>n; 11 | if(!(n&(n-1))) 12 | cout< 2 | using namespace std; 3 | typedef unsigned char uc; 4 | 5 | int leftCircularShift(uc n); 6 | 7 | int main() 8 | { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int n; 13 | cin>>n; 14 | //Simply do a left circular shift of 4 to swap the nibbles 15 | cout<>4)|255); 22 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 23/How to turn off a particular bit in a number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int turnOffKthBit(int a,int k); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int a,k; 11 | cin>>a>>k; 12 | cout<>(k-1))); 19 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 24/190. Russian Peasant Multiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned int RussianPeasant(unsigned int x,unsigned int y); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | unsigned int x,y; 11 | cin>>x,y; 12 | cout<0){ 19 | if(y & 1) 20 | res+=x; 21 | x = x<<1; 22 | y = y>>1; 23 | } 24 | return res; 25 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 24/192. Write your own strcmp that ignores cases.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int strcmp(string b1, string b2); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | string b1,b2; 12 | cin>>b1>>b2; 13 | cout< a 30 | if((str1[i] | 32) < (str2[i] | 32)) 31 | return -1; 32 | return 1; 33 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 24/193. XOR counts of 0s and 1s in binary representation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int XOROf0sand1s(unsigned int x); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | unsigned int x; 11 | cin>>x; 12 | XOROf0sand1s(x); 13 | } 14 | } 15 | 16 | int XOROf0sand1s(unsigned int x){ 17 | int ones = __builtin_popcount(x); 18 | int leadingZeros = __builtin_clz(x); 19 | int zeros = sizeof(unsigned int)*8 - leadingZeros; 20 | return ones ^ zeros; 21 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 24/195. Equal Sum and XOR.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include 3 | 4 | 5 | int countValues (int n); 6 | int main() 7 | { 8 | int t; 9 | scanf("%d",&t); 10 | while(t--) 11 | { 12 | int n; 13 | scanf("%d",&n); 14 | 15 | printf("%d\n",countValues(n)); 16 | } 17 | }// } Driver Code Ends 18 | 19 | 20 | /*You are required to complete this method */ 21 | int countValues (int n) 22 | { 23 | int unsetBits=0; 24 | while(n){ 25 | if(!(n & 1)) 26 | unsetBits++; 27 | n = n>>1; 28 | } 29 | return 1< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | string num; 9 | cin>>num; 10 | int numInt = stoul(num,0,2); 11 | int numLen = num.length(),ten = 10; 12 | bitset<10> complement2s(-numInt); 13 | for(int i=numLen-1;i>=0;i--) 14 | cout< 2 | using namespace std; 3 | 4 | string toggleCases(string x); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | string x; 11 | cin>>x; 12 | cout< 2 | using namespace std; 3 | 4 | int toggleKthBit(int x,int k); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int x,k; 11 | cin>>x>>k; 12 | cout< 4 | using namespace std; 5 | 6 | // To calculate sum of numbers 7 | int findSum(int n) 8 | { 9 | int sum = 0; 10 | 11 | // Find numbers whose 2 bits are set 12 | for (int i = 1; (1 << i) < n; i++) { 13 | for (int j = 0; j < i; j++) { 14 | int num = (1 << i) + (1 << j); 15 | 16 | // If number is greater then n 17 | // we don't include this in sum 18 | if (num <= n) 19 | sum += num; 20 | } 21 | } 22 | 23 | // Return sum of numbers 24 | return sum; 25 | } 26 | 27 | // Driver program to test findSum() 28 | int main(){ 29 | int t; 30 | cin>>t; 31 | while(t--){ 32 | int x; 33 | cin>>x; 34 | cout<>=1; 11 | count++; 12 | } 13 | } 14 | return count; 15 | } 16 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 25/202. XOR Operation in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int xorOperation(int n, int start) { 4 | vector nums(n); 5 | int ans=0; 6 | for(int i=0;ival); 17 | head = head->next; 18 | } 19 | return stoul(num,0,2); 20 | } 21 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 25/204. Hamming Distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingDistance(int x, int y) { 4 | return __builtin_popcount(x ^ y); 5 | } 6 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 25/205. Sort Integers by The Number of 1 Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortByBits(vector& arr) { 4 | sort(arr.begin(),arr.end(),[](int num1,int num2){ 5 | int onesNum1 = __builtin_popcount(num1), onesNum2 = 6 | __builtin_popcount(num2); 7 | if(onesNum1onesNum2) 10 | return false; 11 | else 12 | return num1& nums) { 4 | int oddNum=0; 5 | for(auto num:nums){ 6 | oddNum ^= num; 7 | } 8 | return oddNum; 9 | } 10 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 25/207. Number Complement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | int size = sizeof(int)*8 - __builtin_clz(num); 5 | if(size>30) 6 | return ~(INT_MIN) ^ num; 7 | num = num ^ (1<isPrime = findAllPrimeTill32(); 6 | for(int i=L;i<=R;i++) 7 | if(isPrime[__builtin_popcount(i)]) 8 | count++; 9 | return count; 10 | } 11 | vector findAllPrimeTill32(){ 12 | vectorisPrime(33,true); 13 | isPrime[0]=false,isPrime[1]=false; 14 | for(int i=2;i*i<=32;i++){ 15 | if(isPrime[i]==true){ 16 | for(int j=i*i;j<=32;j+=i) 17 | isPrime[j] = false; 18 | } 19 | } 20 | return isPrime; 21 | } 22 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 25/209. Binary Number with Alternating Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasAlternatingBits(int num) { 4 | bool isOdd,wasOdd = num & 1; 5 | while(num){ 6 | num = num>>1; 7 | isOdd = num & 1; 8 | if(wasOdd == isOdd){ 9 | //There are 2 consecutive 1s or two conecutive 0s 10 | return false; 11 | } 12 | wasOdd = isOdd; 13 | } 14 | return true; 15 | } 16 | 17 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 25/210. Majority Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | int count=1,majEle=nums[0]; 5 | for(int i=1;i& nums) { 4 | int XorWhenNoneMissing=0,trueXor=0,i; 5 | for(i=0;i>1; 9 | } 10 | n = stoul(reverseNum,0,2); 11 | return n; 12 | } 13 | 14 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 25/218. Count Triplets That Can Form Two Arrays of Equal XOR.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countTriplets(vector& arr) { 4 | int zero = 0,equal=0,count=0,temp; 5 | for(int i=1;i=0;j--){ 9 | zero ^= arr[j]; 10 | if(zero == 0){ 11 | temp = zero; 12 | equal = 0; 13 | for(int k = j;k countBits(int num) { 4 | vector Ones; 5 | int count=0,x; 6 | Ones.push_back(0); 7 | for(int i=1;i<=num;i++){ 8 | x = i; 9 | count=0; 10 | while(x){ 11 | x = x & (x-1); 12 | count++; 13 | } 14 | Ones.push_back(count); 15 | } 16 | return Ones; 17 | } 18 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/220. XOR Queries of a Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector xorQueries(vector& arr, vector>& queries) { 4 | // We can easily reduce the space used here to O(1), by using arr for storing Xor 5 | // instead of Map 6 | unordered_map storeXor; 7 | vector queried(queries.size()); 8 | int Xor = 0,L,R,index=0; 9 | for(auto ele:arr){ 10 | Xor ^= ele; 11 | storeXor[index++] = Xor; 12 | } 13 | index=0; 14 | for(auto arr: queries){ 15 | L = arr[0]-1,R = arr[1]; 16 | Xor = storeXor[R]; 17 | if(storeXor.find(L) != storeXor.end()) 18 | Xor ^= storeXor[L]; 19 | queried[index++] = Xor; 20 | } 21 | return queried; 22 | } 23 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/221. Number of Good Ways to Split a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSplits(string s) { 4 | int goodSplit=0; 5 | int s2[26] = {0}; 6 | int bitVectorS1=0,bitVectorS2=0; 7 | for(int i=0;ival -1); 23 | int res = postOrder(root->left,count) + 24 | postOrder(root->right,count); 25 | if(!root->left and !root->right and (count & (count-1)) == 0) 26 | res++; 27 | return res; 28 | } 29 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/223. Letter Case Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector letterCasePermutation(string S) { 4 | vector AllCases; 5 | findAllLetterCases(S,"",AllCases,0); 6 | return AllCases; 7 | } 8 | void findAllLetterCases(string S,string temp,vector 9 | &AllCases,int i){ 10 | if(i == S.length()){ 11 | AllCases.push_back(temp); 12 | return; 13 | } 14 | if(S[i]-'0'>9) 15 | findAllLetterCases(S,temp+(char)(S[i]^32), 16 | AllCases,i+1); 17 | findAllLetterCases(S,temp+S[i],AllCases,i+1); 18 | } 19 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/224. Single Number III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector& nums) { 4 | int Xor=0,num1=0,num2=0; 5 | for(auto num: nums) 6 | Xor ^= num; 7 | //Extract the right most set bit which is one. 8 | int rightMostBit = Xor & ~(Xor-1); 9 | for(auto num: nums){ 10 | if(rightMostBit & num) 11 | num1 ^= num; 12 | else 13 | num2 ^= num; 14 | } 15 | return {num1,num2}; 16 | } 17 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/225. Minimum Flips to Make a OR b Equal to c.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minFlips(int a, int b, int c) { 4 | return __builtin_popcount((a | b)^c) + 5 | __builtin_popcount( a & b & ((a | b)^c) ); 6 | } 7 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/226. Subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> AllSubSets; 4 | vector> subsets(vector& nums) { 5 | vector setVector; 6 | for(int i=1;i<=nums.size();i++) 7 | createSet(nums, {}, 0, i); 8 | AllSubSets.push_back({}); 9 | return AllSubSets; 10 | } 11 | void createSet(vector& nums,vector Set,int index, 12 | int posLeft){ 13 | if(posLeft<=0 or index >= nums.size()){ 14 | if(!Set.empty() and posLeft == 0) 15 | AllSubSets.push_back(Set); 16 | return; 17 | } 18 | Set.push_back(nums[index]); 19 | createSet(nums, Set,index+1,posLeft-1); 20 | Set.erase(Set.end()-1); 21 | createSet(nums, Set,index+1,posLeft); 22 | } 23 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/228. Maximum of Absolute Value Expression.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxAbsValExpr(vector& arr1, vector& arr2) { 4 | int n = arr1.size(); 5 | vector sum1(n,0); 6 | vector sum2 = sum1,diff1 = sum1,diff2 = sum1; 7 | for(int i=0;i arr){ 18 | int n = arr.size(); 19 | int Min = INT_MAX,Max = INT_MIN; 20 | for(int i=0;i& nums) { 4 | int bitCount[32] = {0}; 5 | string num; 6 | for(auto num: nums){ 7 | bitset<32> bitSet(num); 8 | for(int i=0;i<32;i++){ 9 | bitCount[i] += bitSet[i]; 10 | } 11 | } 12 | for(int i=0;i<32;i++) 13 | num += to_string(bitCount[i]%3); 14 | reverse(num.begin(),num.end()); 15 | return stoul(num,0,2); 16 | } 17 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/230. Maximum Product of Word Lengths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& words) { 4 | int n = words.size(); 5 | unsigned long maxLen=0; 6 | vector bitVector(n,0); 7 | for(int i=0;i& nums) { 4 | int total = 0, n = nums.size(); 5 | for (int j=0;j<32;j++) { 6 | int bitCount = 0; 7 | for (int i=0;i> j) & 1; 9 | total += bitCount*(n - bitCount); 10 | } 11 | return total; 12 | } 13 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 26/233. Maximum Length of a Concatenated String with Unique Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxLength(vector& arr) { 4 | vector> dp = {bitset<26>()}; 5 | int maxLen = 0; 6 | for(string s: arr){ 7 | bitset<26> bitRep; 8 | for(char c: s) 9 | bitRep.set(c-'a'); 10 | int n = bitRep.count(); 11 | if(n != s.length()) 12 | continue; 13 | //The string has repeated chars 14 | for(int i = 0;i bitRepDp = dp[i]; 16 | if((bitRep & bitRepDp).any()) 17 | continue; 18 | dp.push_back(bitRepDp | bitRep); 19 | maxLen = max(maxLen,(int)bitRepDp.count()+n); 20 | } 21 | } 22 | return maxLen; 23 | } 24 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/234. Number of Steps to Reduce a Number in Binary Representation to One.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSteps(string s) { 4 | int steps=0,index; 5 | while(s.length()>1){ 6 | index = s.length()-1; 7 | if(((s[index]-'0') & 1) == 0){ 8 | s.erase(s.end() - 1); 9 | }else{ 10 | while(index >= 0 and s[index] != '0'){ 11 | s[index--] = '0'; 12 | } 13 | if(index<0) 14 | s.insert(0,"0"); 15 | else 16 | s[index] = '1'; 17 | } 18 | steps++; 19 | } 20 | return steps; 21 | } 22 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/235. Check If a String Contains All Binary Codes of Size K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasAllCodes(string s, int k) { 4 | unordered_set comb; 5 | string find; 6 | int i=0; 7 | // window size of k; 8 | for(i=0;im) 5 | n = n & n-1; 6 | return m&n; 7 | } 8 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/237. Repeated DNA Sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findRepeatedDnaSequences(string s) { 4 | unordered_map DNA; 5 | int i,AGTC[4]={0}; 6 | bitset<16> DNA() 7 | string temp; 8 | for(i=0;i<10;i++){ 9 | temp += s[i]; 10 | increaseCount(AGTC,s[i]); 11 | } 12 | for(;i repeatedDna; 18 | for(auto map: DNA) 19 | if(map.second > 1) 20 | repeatedDna.push_back(map.first); 21 | return repeatedDna; 22 | 23 | } 24 | void increaseCount(int AGTC[],s[i]){ 25 | switch(s[i]){ 26 | case 'A': AGTC[0]++; 27 | break; 28 | case 'G': AGTC[1]++; 29 | break; 30 | case 'T': AGTC[2]++; 31 | break; 32 | case 'C': AGTC[3]++; 33 | } 34 | } 35 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/238. Bitwise ORs of Subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarrayBitwiseORs(vector& A) { 4 | vector uniq; 5 | int left = 0,right= 0; 6 | for(auto x: A){ 7 | right = uniq.size(); 8 | uniq.push_back(x); 9 | for(int i = left;i < right;i++){ 10 | if(uniq.back() != (x | uniq[i])) 11 | uniq.push_back(x | uniq[i]); 12 | } 13 | left = right; 14 | } 15 | return unordered_set(uniq.begin(),uniq.end()).size(); 16 | } 17 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/239. Integer Replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerReplacement(uint32_t n) { 4 | int steps = 0; 5 | while(n != 1){ 6 | if(n == 3){ 7 | steps +=2; 8 | break; 9 | } 10 | else if(n & 1){ 11 | if(((n+1)>>1 & 1)){ 12 | n -= 1; 13 | }else 14 | n += 1; 15 | }else 16 | n >>= 1; 17 | steps++; 18 | } 19 | return steps; 20 | } 21 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/240. Maximum Score Words Formed by Letters.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kushagra414/Dynamic-Programming/32e1faac325d2ed3479abf3cef4a7c2470356667/Kushagra Shekhawat/Day 27/240. Maximum Score Words Formed by Letters.cpp -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/242. Find a Value of a Mysterious Function Closest to Target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int closestToTarget(vector& arr, int target) { 4 | unordered_set unqis,prev; 5 | int minVal = INT_MAX; 6 | for(auto x: arr){ 7 | unordered_setcurr; 8 | prev.insert(0x7FFFFFFF); 9 | for(auto y: prev){ 10 | unqis.insert(x & y); 11 | curr.insert(x & y); 12 | } 13 | prev = curr; 14 | } 15 | for(auto num: unqis){ 16 | minVal = min(abs(num - target),minVal); 17 | } 18 | return minVal; 19 | } 20 | }; -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 27/244. Number of Valid Words for Each Puzzle.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kushagra414/Dynamic-Programming/32e1faac325d2ed3479abf3cef4a7c2470356667/Kushagra Shekhawat/Day 27/244. Number of Valid Words for Each Puzzle.cpp -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 3/12. Largest number with given sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,S,sum=0,lowerLimit=0; 9 | string num=""; 10 | bool notFound; 11 | cin>>N; 12 | cin>>S; 13 | for(int i=0;i=0;j--){ 16 | if(j+sum<=S){ 17 | sum+=j; 18 | num+=to_string(j); 19 | notFound = false; 20 | break; 21 | } 22 | } 23 | if(notFound) 24 | break; 25 | } 26 | if(notFound or sum != S) 27 | cout<<-1< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,S,sum=0,lowerLimit=0; 9 | string num=""; 10 | bool notFound; 11 | cin>>N; 12 | cin>>S; 13 | for(int i=0;i=0;j--){ 16 | if(j+sum<=S){ 17 | sum+=j; 18 | num+=to_string(j); 19 | notFound = false; 20 | break; 21 | } 22 | } 23 | if(notFound) 24 | break; 25 | } 26 | if(notFound or sum != S) 27 | cout<<-1< 2 | using namespace std; 3 | 4 | int fact[10]; 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | fact[0] = 1; 11 | for(int i=1;i<10;i++){ 12 | fact[i] = fact[i-1]*i; 13 | } 14 | while(T--){ 15 | int N; 16 | cin>>N; 17 | string num = ""; 18 | while(N>0){ 19 | for(int i=9;i>=0;i--){ 20 | if(fact[i]<=N){ 21 | num+=to_string(i); 22 | N -= fact[i]; 23 | break; 24 | } 25 | } 26 | } 27 | reverse(num.begin(),num.end()); 28 | if(N == 0) 29 | cout< 2 | using namespace std; 3 | 4 | int MinSurvivalKit(int surviveDays,int maxFoodBuy,int FoodEachDay); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int S,N,M; 12 | cin>>S>>N>>M; 13 | cout<=maxFoodBuy or surviveDays>6 and(maxFoodBuy - FoodEachDay)*6 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,M; 9 | cin>>N; 10 | string num=""; 11 | M = N; 12 | while(N){ 13 | for(int i=9;i>=0;i--){ 14 | if(N-i>=0){ 15 | num += to_string(i); 16 | N-=i; 17 | break; 18 | } 19 | } 20 | } 21 | reverse(num.begin(),num.end()); 22 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int rows,columns,sum=0,MaxElement; 9 | bool elementNotFound; 10 | cin>>rows>>columns; 11 | int matrix[rows][columns],previousMaxElement = INT_MAX; 12 | for(int i=0;i>matrix[i][j]; 15 | } 16 | for(int i=rows-1;i>=0;i--){ 17 | MaxElement = INT_MIN; 18 | elementNotFound = true; 19 | for(int j=0;j 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | string brackets; 9 | int countLeft=0,countRight=0,imbalance=0,swaps=0,N; 10 | cin>>N>>brackets; 11 | for(int i=0;i0){ 15 | swaps+=imbalance; 16 | imbalance--; 17 | } 18 | }else{ 19 | countLeft++; 20 | imbalance = (countLeft-countRight); 21 | } 22 | } 23 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,K,temp,buy=0; 9 | cin>>N>>K; 10 | sets; 11 | for(int i=0;i>temp; 13 | s.insert(temp); 14 | } 15 | int maxCoins = pow(10,9); 16 | for(int i=1;i<=maxCoins;i++){ 17 | if(s.find(i) == s.end() and K>=i){ 18 | buy++; 19 | K-=i; 20 | } 21 | else if(K 2 | using namespace std; 3 | 4 | struct PizzaOrders{ 5 | int x,y,index; 6 | }; 7 | 8 | bool Compare(PizzaOrders a, PizzaOrders b){ 9 | if(a.x+a.y==b.x+b.y) 10 | return a.index>T; 18 | while(T--){ 19 | int N; 20 | cin>>N; 21 | PizzaOrders orders[N]; 22 | for(int i=0;i>orders[i].x; 24 | cin>>orders[i].y; 25 | orders[i].index = i; 26 | } 27 | sort(orders,orders+N,Compare); 28 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | string s; 9 | cin>>s; 10 | int characters[26],N=s.length(); 11 | bool replaced = false; 12 | memset(characters,-1,sizeof(int)*26); 13 | for(int i=0;i=0 and i 2 | using namespace std; 3 | void KswapPermutation(int arr[], int n, int k) 4 | { 5 | int pos[n + 1]; 6 | 7 | for (int i = 0; i < n; ++i) 8 | pos[arr[i]] = i; 9 | 10 | for (int i = 0; i < n && k; ++i) { 11 | if (arr[i] == n - i) 12 | continue; 13 | 14 | int temp = pos[n - i]; 15 | pos[arr[i]] = pos[n - i]; 16 | pos[n - i] = i; 17 | 18 | swap(arr[temp], arr[i]); 19 | --k; 20 | } 21 | } 22 | 23 | // Driver code 24 | int main() 25 | { 26 | int T; 27 | cin>>T; 28 | while(T--){ 29 | int N,K; 30 | cin>>N>>K; 31 | int arr[N]; 32 | for(int i=0;i>arr[i]; 34 | KswapPermutation(arr, N, K); 35 | for (int i = 0; i < N; ++i) 36 | cout< 2 | using namespace std; 3 | 4 | int minDiff(int arr[],int N,int K); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,K; 12 | cin>>K>>N; 13 | int arr[N]; 14 | for(int i=0;i>arr[i]; 16 | } 17 | sort(arr,arr+N); 18 | cout<maxi) 30 | swap(mini,maxi); 31 | 32 | for(int i=0;i=mini or add<=maxi) 36 | continue; 37 | 38 | if(maxi-sub<=add-mini) 39 | mini =sub; 40 | else 41 | maxi = add; 42 | } 43 | return min(ans,maxi-mini); 44 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 7/31. Coin Piles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findMinimumCoins(int arr[],int N,int K); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,K; 12 | cin>>N>>K; 13 | int arr[N]; 14 | for(int i=0;i>arr[i]; 16 | sort(arr,arr+N); 17 | cout<=i;j--){ 33 | int removeCoins = arr[j]-arr[i]; 34 | if(removeCoins>K){ 35 | sum=sum+removeCoins-K; 36 | }else 37 | break; 38 | } 39 | RemovedCoin = min(sum,RemovedCoin); 40 | } 41 | return RemovedCoin; 42 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 7/Do these Aptitude Questions Again!.md: -------------------------------------------------------------------------------- 1 | # Do these Questions Again - Volume and Surface Area 2 | 3 | ## 1,7,9,10,12,15 4 | 5 | # Do these Questions Again - Simplification 6 | 7 | # 4,8,9,15,12 -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 8/33. Print first n Fibonacci Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | long long fibonacci[84]; 6 | memset(fibonacci,-1,sizeof(long long int)*84); 7 | fibonacci[0]=1,fibonacci[1]=1; 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | cin>>N; 13 | for(int i=2;i 2 | using namespace std; 3 | 4 | unsigned long long int arr[100]; 5 | int Max=3; 6 | void PadovanSequence(int n); 7 | 8 | int main() 9 | { 10 | int T; 11 | cin>>T; 12 | while(T--){ 13 | int N; 14 | cin>>N; 15 | arr[0]=arr[1]=arr[2]=1; 16 | PadovanSequence(N); 17 | cout<2){ 24 | for(int i=Max;i<=n;i++){ 25 | arr[i]=arr[i-2]+arr[i-3]; 26 | } 27 | Max=Max>T; 11 | while(T--){ 12 | int N,count=0; 13 | cin>>N; 14 | arr[0]=0; 15 | Count4(N); 16 | 17 | cout<>T; 9 | while(T--){ 10 | int N,cap; 11 | cin>>N>>cap; 12 | int val[N],wt[N]; 13 | for(int i=0,j=0;i>val[i++]; 16 | else 17 | cin>>wt[j++]; 18 | } 19 | cout< 2 | using namespace std; 3 | 4 | //LIS ---> Longest Increasing Sum 5 | 6 | struct Sequence{ 7 | int value, LIS; 8 | }; 9 | 10 | int MSIS(Sequence seq[], int N); 11 | 12 | int main() 13 | { 14 | int T; 15 | cin>>T; 16 | while(T--){ 17 | int N; 18 | cin>>N; 19 | int x; 20 | Sequence seq[N]; 21 | for(int i=0;i>x; 23 | seq[i].value = seq[i].LIS = x; 24 | } 25 | cout<seq[i].LIS?maximumSum:seq[i].LIS; 42 | } 43 | return maximumSum; 44 | } -------------------------------------------------------------------------------- /Kushagra Shekhawat/Day 9/38. Maximize The Cut Segments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxCutSegments(int N,int x,int y,int z); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,x,y,z; 12 | cin>>N>>x>>y>>z; 13 | cout< 2 | using namespace std; 3 | 4 | long long int FindPaths(int N,int M); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,M; 12 | cin>>N>>M; 13 | cout< 2 | using namespace std; 3 | 4 | long long int SticklerThief(long long int houses[],int N); 5 | 6 | int main() 7 | { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | cin>>N; 13 | long long int houses[N]; 14 | for(int i=0;i>houses[i]; 16 | cout< 2 | using namespace std; 3 | 4 | int rupee[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 5 | void findMinCoins(int N); 6 | 7 | int main() 8 | { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N; 13 | cin>>N; 14 | findMinCoins(N); 15 | cout<=0;i--){ 23 | wholeCoins = N/rupee[i]; 24 | if(wholeCoins) 25 | N %= rupee[i]; 26 | while(wholeCoins--){ 27 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t ; cin>>t ; 7 | while( t-- ){ 8 | int n ; cin>>n ; 9 | int a[n] ; 10 | 11 | for( int i = 0 ; i>a[i]; 13 | } 14 | 15 | sort(a,a+n); 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t ; cin>>t ; 6 | while( t-- ){ 7 | int n ; cin>>n ; 8 | cout<<(n-1)*2< 2 | #include 3 | using namespace std; 4 | 5 | bool compare( int x , int y ){ 6 | if( x > y ) return true; 7 | else return false ; 8 | } 9 | 10 | int main() { 11 | int t ; cin>>t ; 12 | while( t-- ){ 13 | 14 | int n ; cin>>n ; 15 | int a[n], b[n] ; 16 | for( int i = 0 ; i < n ; i++ ){ 17 | cin>>a[i]; 18 | } 19 | for( int i = 0 ; i < n ; i++ ){ 20 | cin>>b[i]; 21 | } 22 | 23 | sort( a , a+n ); 24 | sort( b , b+n , compare ); 25 | 26 | 27 | 28 | long long int sum = 0 ; 29 | for( int i = 0 ; i < n ; i++ ){ 30 | sum += a[i]*b[i] ; 31 | } 32 | cout< 2 | using namespace std; 3 | 4 | vector v; 5 | 6 | int check( int s , int d ){ 7 | v.clear() ; 8 | if( s > 9*d ){ 9 | return -1; 10 | } 11 | 12 | s = s-1 ; 13 | for( int i = d-1 ; i >= 1 ; i-- ){ 14 | if( s > 9 ){ 15 | v.push_back(9) ; 16 | s-=9 ; 17 | } 18 | else{ 19 | v.push_back(s) ; 20 | s=0; 21 | } 22 | } 23 | v.push_back(s+1); 24 | return 0 ; 25 | } 26 | 27 | int main() { 28 | int T; 29 | cin>>T; 30 | while(T--){ 31 | int s = 0, d = 0; 32 | cin>>s>>d; 33 | vector ans; 34 | if( check( s , d ) != -1 ) { 35 | for( int i = v.size()-1 ; i >= 0 ; i-- ){ 36 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int t ; cin>>t ; 8 | while( t-- ){ 9 | int n ; cin>>n; 10 | int a[n]; 11 | for( int i = 0 ; i>a[i]; 13 | } 14 | sort( a , a+n ); 15 | int i = 0 , j = n-1 , s = 0 , f = 0 ; 16 | while( i 2 | #include 3 | using namespace std; 4 | 5 | vector v; 6 | 7 | int check( int d , int s ){ 8 | v.clear(); 9 | if( d > 1 && s == 0 ){ 10 | return -1; 11 | } 12 | if( s > d*9 ){ 13 | return -1; 14 | } 15 | 16 | for(int i=1 ;i <= d ; i++){ 17 | if( s>9 ){ 18 | v.push_back( 9 ); 19 | s-=9; 20 | } 21 | else{ 22 | v.push_back(s); 23 | s = 0 ; 24 | } 25 | } 26 | return 0 ; 27 | } 28 | 29 | int main() { 30 | int t ; cin>>t; 31 | while(t--){ 32 | int d , s ; 33 | cin>>d>>s; 34 | if( check(d,s) != -1 ){ 35 | for( int x : v ){ 36 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t ; cin>>t; 7 | while( t-- ){ 8 | int n , k , c = 0 ; 9 | cin>>n>>k; 10 | int a[n] ; 11 | for( int i = 0 ; i < n ; i++ ){ 12 | cin>>a[i]; 13 | } 14 | 15 | sort( a , a+n ); 16 | 17 | for( int i = 0 ; i < n; i++ ){ 18 | if( a[i] > k ){ 19 | break; 20 | } 21 | else{ 22 | c++ ; 23 | k-=a[i] ; 24 | } 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; cin>>t; 6 | while( t-- ){ 7 | int n ,temp , c=0 ; cin>>n; 8 | temp = n; 9 | 10 | while( temp!=0 ){ 11 | if( temp%2==0 && temp-temp/2 != 0 ){ 12 | c++ ; 13 | temp /= 2 ; 14 | } 15 | else{ 16 | temp--; 17 | c++; 18 | } 19 | } 20 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t ; cin>>t; 7 | while(t--){ 8 | int n , f ; cin>>n>>f; 9 | int a[n] ; 10 | for(int i = 0 ; i < n ; i++){ 11 | cin>>a[i] ; 12 | } 13 | 14 | sort( a, a+n ); 15 | 16 | int temp = n ; int c=0; 17 | for( int i=0 ; i= 0 ; i-- ){ 24 | c+=a[i]; 25 | temp+=f; 26 | if( temp >= i ){ 27 | break; 28 | } 29 | } 30 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t ; cin>>t; 6 | while(t--){ 7 | int n, s, m , c=0 ; 8 | cin>>s>>n>>m ; 9 | if( m >= n ){ 10 | c = -1; 11 | } 12 | else{ 13 | int stock = 0 ; 14 | int total_needed = s*m ; 15 | for(int i = 1; i <= s ; i++ ){ 16 | if( total_needed <= 0 ){ 17 | break; 18 | } 19 | if( stock < m && i % 7 == 0 ){ 20 | c=-1; 21 | break; 22 | } 23 | else if( i % 7 != 0 ){ 24 | stock+=n; 25 | c++ ; 26 | stock-=m; 27 | total_needed -= n ; 28 | } 29 | else{ 30 | stock-=m; 31 | } 32 | } 33 | } 34 | cout< 2 | #include 3 | using namespace std; 4 | 5 | vector v ; 6 | 7 | int check(int d , int s){ 8 | v.clear() ; 9 | if( s > d*9 ){ 10 | return -1 ; 11 | } 12 | 13 | for( int i=1; i<=d ; i++ ){ 14 | if( s > 9 ){ 15 | v.push_back(9) ; 16 | s-=9 ; 17 | } 18 | else{ 19 | v.push_back(s); 20 | s = 0 ; 21 | } 22 | } 23 | return 0 ; 24 | } 25 | 26 | int main(){ 27 | int t; cin>>t; 28 | while(t--){ 29 | int d,s; cin>>d>>s; 30 | if( check(d,s) !=-1 ){ 31 | for(int x:v){ 32 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t ; cin>>t ; 6 | while(t--){ 7 | int n ; 8 | string s ; cin>>n>>s; 9 | int l = s.length() ; 10 | int lc = 0 , rc = 0 , bal = 0 , c = 0 ; 11 | for(int i=0; i 0 ){ 15 | c+=bal ; 16 | bal--; 17 | } 18 | } 19 | else{ 20 | rc++ ; 21 | bal = rc - lc ; 22 | } 23 | } 24 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t ; cin>>t ; 7 | while(t--){ 8 | long long int n , k ; cin>>n>>k ; 9 | long long int a[n] ; 10 | for(long long int i=0 ; i>a[i] ; 12 | } 13 | sort( a , a+n ); 14 | long long int l = 1 , i = 0 , c = 0 ; 15 | while( k >= l ){ 16 | if( a[i] != l ){ 17 | k-=l ; 18 | c++ ; 19 | } 20 | else{ 21 | i++; 22 | } 23 | l++ ; 24 | } 25 | // cout<<"L="< 2 | #include 3 | using namespace std; 4 | 5 | vector v ; 6 | 7 | void find(int temp){ 8 | v.clear() ; 9 | while( temp!=0 ){ 10 | if( temp > 9 ){ 11 | v.push_back(9); 12 | temp -= 9 ; 13 | } 14 | else{ 15 | v.push_back(temp); 16 | temp = 0 ; 17 | } 18 | } 19 | } 20 | 21 | int main() { 22 | int t ; cin>>t ; 23 | while(t--){ 24 | int n ; cin>>n; 25 | string s = ""; 26 | find(n); 27 | 28 | for(int i=v.size()-1 ; i>=0 ;i--){ 29 | cout< 2 | #include 3 | using namespace std; 4 | 5 | struct order{ 6 | int no , st , ti, end ; 7 | }; 8 | 9 | bool compare( struct order a , struct order b){ 10 | if( a.end < b.end ){ 11 | return true ; 12 | } 13 | else if( a.end == b.end ){ 14 | if( a.no < b.no ) 15 | return true ; 16 | else return false ; 17 | } 18 | else{ 19 | return false ; 20 | } 21 | } 22 | 23 | int main() { 24 | int t ; cin>>t ; 25 | while( t-- ){ 26 | int n ; cin>> n ; 27 | struct order a[n]; 28 | for(int i=0;i>a[i].st>>a[i].ti; 30 | a[i].no = i+1 ; 31 | a[i].end = a[i].st + a[i].ti ; 32 | } 33 | sort( a , a+n , compare ) ; 34 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int t ; cin>>t ; 6 | while(t--){ 7 | int n , k ; cin>>n>>k ; 8 | int a[n] , b[n+1] = {0} , temp = k ; 9 | for(int i=0; i < n ;i++){ 10 | cin>>temp ; 11 | a[i] = temp ; 12 | b[temp] = i ; 13 | } 14 | 15 | temp = k ; 16 | int lol ; 17 | for( int i = 0 ; i < n && temp ; i++ ){ 18 | 19 | if(a[i]== n-i ) continue; 20 | 21 | lol = b[n-i] ; 22 | b[a[i]] = b[n-i] ; 23 | b[n-i] = i ; 24 | 25 | swap(a[lol], a[i]); 26 | temp-- ; 27 | } 28 | int i ; 29 | for( i = 0 ; i< n ; i++){ 30 | cout<