Given a positive integer N, find the maximum number of segments of lengths a, b and c that can be formed from N .
Input : N = 7, a = 5, b, = 2, c = 5 Output : 2 N can be divided into 2 segments of lengths 2 and 5. For the second example, Input : N = 17, a = 2, b = 1, c = 3 Output : 17 N can be divided into 17 segments of 1 or 8 segments of 2 and 1 segment of 1. But 17 segments of 1 is greater than 9 segments of 2 and 1.
Approach : The approach used is Dynamic Programming. The base dp0 will be 0 as initially it has no segments. After that, iterate from 1 to n, and for each of the 3 states i.e, dpi+a, dpi+b and dpi+c, store the maximum value obtained by either using or not using the a, b or c segment.
The 3 states to deal with are :
Below is the implementation of above idea :
Time complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum length of segments of 0's and 1's
- Sum of lengths of all paths possible in a given tree
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Minimum splits required to convert a number into prime segments
- Count of ways to split a given number into prime segments
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- LCS formed by consecutive segments of at least length K
- Divide the array in K segments such that the sum of minimums is maximized
- Find the maximum number of composite summands of a number
- Maximum number of strings that can be formed with given zeros and ones
- Minimum and Maximum values of an expression with * and +
- How to print maximum number of A's using given four keys
- Recursively break a number in 3 parts to get maximum sum
- Maximum sum of a path in a Right Number Triangle
- Maximum number of trailing zeros in the product of the subsets of size k
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Possible cuts of a number such that maximum parts are divisible by 3
- Maximum number of given operations to remove the entire string
- Number of subsequences of maximum length K containing no repeated elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.