# Maximize the number of segments of length p, q and r

Given a rod of length L, the task is to cut the rod in such a way that the total number of segments of length p, q and r is maximized. The segments can only be of length p, q, and r.

Examples:

Input: l = 11, p = 2, q = 3, r = 5
Output: 5
Segments of 2, 2, 2, 2 and 3

Input: l = 7, p = 2, q = 5, r = 5
Output: 2
Segments of 2 and 5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: As the solution for maximum number of cuts that can be made in a given length depends on the maximum number of cuts previously made in shorter lengths, this question could be solved by the approach of Dynamic Programming. Suppose we are given a length ‘l’. For finding the maximum number of cuts that can be made in length ‘l’, find number of cuts made in shorter previous length ‘l-p’,’l-q’,’l-r’ lengths respectively. The required answer would be the max(l-p,l-q,l-r)+1 as one more cut should be needed after this to cut length ‘l’. So for solving this problem for given length, find maximum number of cuts that can be made in lengths ranging from ‘1’ to ‘l’.
Example:

l = 11, p = 2, q = 3, r = 5
Analysing lengths from 1 to 11:

1. Not possible to cut->0
2. Possible cut is of lengths 2->1 (2)
3. Possible cut is of lengths 3->1 (3)
4. Possible cuts are of lengths max(arr[4-2],arr[4-3])+1->2 (2,2)
5. Possible cuts are of lengths max(arr[5-2],arr[5-3])+1->2 (2,3)
6. Possible cuts are of lengths max(arr[6-2],arr[6-3],arr[6-5])+1->3 (2,2,2)
7. Possible cuts are of lengths max(arr[7-2],arr[7-3],arr[7-5])+1->3 (2,3,2) or (2,2,3)
8. Possible cuts are of lengths max(arr[8-2],arr[8-3],arr[8-5])+1->4 (2,2,2,2)
9. Possible cuts are of lengths max(arr[9-2],arr[9-3],arr[9-5])+1->4 (2,3,2,2) or (2,2,3,2) or (2,2,2,3)
10. Possible cuts are of lengths max(arr[10-2],arr[10-3],arr[10-5])+1->5 (2,2,2,2,2)
11. Possible cuts are of lengths max(arr[11-2],arr[11-3],arr[11-5])+1->5 (2,3,2,2,2) or (2,2,3,2,2) or (2,2,2,3,2) or (2,2,2,2,3)

Algorithm:

1. Initialise an array DP[]={-1} and DP=0.
2. Run a loop from ‘1’ to ‘l’
3. If DP[i]=-1 means it’s not possible to divide it using giving segments p,q,r so continue;
4. DP[i+p]=max(DP[i+p],DP[i]+1)
5. DP[i+q]=max(DP[i+q],DP[i]+1)
6. DP[i+r]=max(DP[i+r],DP[i]+1)
7. print DP[l]

Pseudo Code:

```DP[l+1]={-1}
DP=0
for(i from 0 to l)
if(DP[i]==-1)
continue
DP[i+p]=max(DP[i+p],DP[i]+1)
DP[i+q]=max(DP[i+q],DP[i]+1)
DP[i+r]=max(DP[i+r],DP[i]+1)

print(DP[l])
```

Implementation:

## C++

 `// C++ program to maximize the number ` `// of segments of length p, q and r ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns the maximum number ` `// of segments possible ` `int` `findMaximum(``int` `l, ``int` `p, ``int` `q, ``int` `r) ` `{ ` ` `  `    ``// Array to store the cut at each length ` `    ``int` `dp[l + 1]; ` ` `  `    ``// All values with -1 ` `    ``memset``(dp, -1, ``sizeof``(dp)); ` ` `  `    ``// if length of rod is 0 then total cuts will be 0 ` `    ``// so, initialize the dp with 0 ` `    ``dp = 0; ` ` `  `    ``for` `(``int` `i = 0; i <= l; i++) { ` ` `  `        ``// if certain length is not possible ` `        ``if` `(dp[i] == -1) ` `            ``continue``; ` ` `  `        ``// if a segment of p is possible ` `        ``if` `(i + p <= l) ` `            ``dp[i + p] = max(dp[i + p], dp[i] + 1); ` ` `  `        ``// if a segment of q is possible ` `        ``if` `(i + q <= l) ` `            ``dp[i + q] = max(dp[i + q], dp[i] + 1); ` ` `  `        ``// if a segment of r is possible ` `        ``if` `(i + r <= l) ` `            ``dp[i + r] = max(dp[i + r], dp[i] + 1); ` `    ``} ` ` `  `    ``// return value corresponding to length l ` `    ``return` `dp[l]; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `l = 11, p = 2, q = 3, r = 5; ` ` `  `    ``// Calling Function ` `    ``int` `ans = findMaximum(l, p, q, r); ` `    ``cout << ans; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to maximize  ` `// the number of segments  ` `// of length p, q and r ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `// Function that returns ` `// the maximum number ` `// of segments possible ` `static` `int` `findMaximum(``int` `l, ``int` `p,  ` `                       ``int` `q, ``int` `r) ` `{ ` ` `  `    ``// Array to store the ` `    ``// cut at each length ` `    ``int` `dp[] = ``new` `int``[l + ``1``]; ` ` `  `    ``// All values with -1 ` `    ``for``(``int` `i = ``0``; i < l + ``1``; i++) ` `    ``dp[i] = -``1``; ` `     `  ` `  `    ``// if length of rod is 0  ` `    ``// then total cuts will  ` `    ``// be 0 so, initialize  ` `    ``// the dp with 0 ` `    ``dp[``0``] = ``0``; ` ` `  `    ``for` `(``int` `i = ``0``; i <= l; i++)  ` `    ``{ ` ` `  `        ``// if certain length  ` `        ``// is not possible ` `        ``if` `(dp[i] == -``1``) ` `            ``continue``; ` ` `  `        ``// if a segment of ` `        ``// p is possible ` `        ``if` `(i + p <= l) ` `            ``dp[i + p] = Math.max(dp[i + p],  ` `                                 ``dp[i] + ``1``); ` ` `  `        ``// if a segment of  ` `        ``// q is possible ` `        ``if` `(i + q <= l) ` `            ``dp[i + q] = Math.max(dp[i + q], ` `                                 ``dp[i] + ``1``); ` ` `  `        ``// if a segment of ` `        ``// r is possible ` `        ``if` `(i + r <= l) ` `            ``dp[i + r] = Math.max(dp[i + r],  ` `                                 ``dp[i] + ``1``); ` `    ``} ` ` `  `    ``// return value corresponding  ` `    ``// to length l ` `    ``return` `dp[l]; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `l = ``11``, p = ``2``,  ` `        ``q = ``3``, r = ``5``; ` ` `  `    ``// Calling Function ` `    ``int` `ans = findMaximum(l, p, q, r); ` `    ``System.out.println( ans); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67. `

## Python 3

 `# Python 3 program to  ` `# maximize the number ` `# of segments of length  ` `# p, q and r ` ` `  `# Function that returns  ` `# the maximum number ` `# of segments possible ` `def` `findMaximum( l, p, q, r): ` ` `  `    ``# Array to store the cut  ` `    ``# at each length ` `    ``# All values with -1 ` `    ``dp ``=` `[``-``1``]``*``(l ``+` `1``)  ` ` `  `    ``# if length of rod is 0 then  ` `    ``# total cuts will be 0 ` `    ``# so, initialize the dp with 0 ` `    ``dp[``0``] ``=` `0` ` `  `    ``for` `i ``in` `range``( l``+``1``) : ` ` `  `        ``# if certain length is not  ` `        ``# possible ` `        ``if` `(dp[i] ``=``=` `-``1``): ` `            ``continue` ` `  `        ``# if a segment of p is possible ` `        ``if` `(i ``+` `p <``=` `l): ` `            ``dp[i ``+` `p] ``=` `(``max``(dp[i ``+` `p],  ` `                        ``dp[i] ``+` `1``)) ` ` `  `        ``# if a segment of q is possible ` `        ``if` `(i ``+` `q <``=` `l): ` `            ``dp[i ``+` `q] ``=` `(``max``(dp[i ``+` `q],  ` `                        ``dp[i] ``+` `1``)) ` ` `  `        ``# if a segment of r is possible ` `        ``if` `(i ``+` `r <``=` `l): ` `            ``dp[i ``+` `r] ``=` `(``max``(dp[i ``+` `r], ` `                        ``dp[i] ``+` `1``)) ` ` `  `    ``# return value corresponding  ` `    ``# to length l ` `    ``return` `dp[l] ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``l ``=` `11` `    ``p ``=` `2` `    ``q ``=` `3` `    ``r ``=` `5` ` `  `    ``# Calling Function ` `    ``ans ``=` `findMaximum(l, p, q, r) ` `    ``print``(ans) ` ` `  `# This code is contributed by  ` `# ChitraNayal `

## C#

 `// C# program to maximize  ` `// the number of segments  ` `// of length p, q and r ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `// Function that returns ` `// the maximum number ` `// of segments possible ` `static` `int` `findMaximum(``int` `l, ``int` `p,  ` `                       ``int` `q, ``int` `r) ` `{ ` ` `  `    ``// Array to store the ` `    ``// cut at each length ` `    ``int` `[]dp = ``new` `int``[l + 1]; ` ` `  `    ``// All values with -1 ` `    ``for``(``int` `i = 0; i < l + 1; i++) ` `    ``dp[i] = -1; ` `     `  ` `  `    ``// if length of rod is 0  ` `    ``// then total cuts will  ` `    ``// be 0 so, initialize  ` `    ``// the dp with 0 ` `    ``dp = 0; ` ` `  `    ``for` `(``int` `i = 0; i <= l; i++)  ` `    ``{ ` ` `  `        ``// if certain length  ` `        ``// is not possible ` `        ``if` `(dp[i] == -1) ` `            ``continue``; ` ` `  `        ``// if a segment of ` `        ``// p is possible ` `        ``if` `(i + p <= l) ` `            ``dp[i + p] = Math.Max(dp[i + p],  ` `                                 ``dp[i] + 1); ` ` `  `        ``// if a segment of  ` `        ``// q is possible ` `        ``if` `(i + q <= l) ` `            ``dp[i + q] = Math.Max(dp[i + q], ` `                                 ``dp[i] + 1); ` ` `  `        ``// if a segment of ` `        ``// r is possible ` `        ``if` `(i + r <= l) ` `            ``dp[i + r] = Math.Max(dp[i + r],  ` `                                 ``dp[i] + 1); ` `    ``} ` ` `  `    ``// return value corresponding  ` `    ``// to length l ` `    ``return` `dp[l]; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main () ` `{ ` `    ``int` `l = 11, p = 2,  ` `        ``q = 3, r = 5; ` ` `  `    ``// Calling Function ` `    ``int` `ans = findMaximum(l, p, q, r); ` `    ``Console.WriteLine(ans); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67. `

Output:

```5
```

Complexity Analysis:

• Time Complexity: O(N).
Use of a single for loop till length ‘N’.
• Auxiliary Space: O(N).
Use of an array ‘DP’ to keep track of segments. My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Improved By : vt_m, chitranayal, bidibaaz123

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.