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
Explanation: Segments of 2, 2, 2, 2 and 3
Input: l = 7, p = 2, q = 5, r = 5
Output: 2
Explanation: Segments of 2 and 5
BRUTE METHOD(Recursion)
Intuition:
- We look for all possibilities for every individual length and send the remaining value in recursion.
- Once the base case is hit, i.e n becomes 0, we return 0;
- And this continues till all the lengths x,y,z are looked whether they will contribute to it or not.
Implementation:
C++
#include <bits/stdc++.h>
#include <limits.h>
#include <iostream>
using namespace std;
int maximizeCuts( int n, int x, int y, int z)
{
if (n == 0)
return 0;
if (n < 0)
return INT_MIN;
int a = maximizeCuts(n - x, x, y, z) + 1;
int b = maximizeCuts(n - y, x, y, z) + 1;
int c = maximizeCuts(n - z, x, y, z) + 1;
int d = max(a, max(b, c));
return d;
}
int main()
{
int l = 11, p = 2, q = 3, r = 5;
cout << maximizeCuts(l, p, q, r) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int maximizeCuts( int n, int x, int y, int z)
{
if (n == 0 )
return 0 ;
if (n < 0 )
return Integer.MIN_VALUE;
int a = maximizeCuts(n - x, x, y, z) + 1 ;
int b = maximizeCuts(n - y, x, y, z) + 1 ;
int c = maximizeCuts(n - z, x, y, z) + 1 ;
int d = Math.max(a, Math.max(b, c));
return d;
}
public static void main(String[] args)
{
int l = 11 , p = 2 , q = 3 , r = 5 ;
System.out.println(maximizeCuts(l, p, q, r));
}
}
|
Python3
def maximize_cuts(n, x, y, z):
if n = = 0 :
return 0
if n < 0 :
return float ( '-inf' )
a = maximize_cuts(n - x, x, y, z) + 1
b = maximize_cuts(n - y, x, y, z) + 1
c = maximize_cuts(n - z, x, y, z) + 1
d = max (a, max (b, c))
return d
if __name__ = = "__main__" :
l = 11
p = 2
q = 3
r = 5
print (maximize_cuts(l, p, q, r))
|
C#
using System;
class Program {
static int MaximizeCuts( int n, int x, int y, int z)
{
if (n == 0)
return 0;
if (n < 0)
return int .MinValue;
int a = MaximizeCuts(n - x, x, y, z) + 1;
int b = MaximizeCuts(n - y, x, y, z) + 1;
int c = MaximizeCuts(n - z, x, y, z) + 1;
return Math.Max(a, Math.Max(b, c));
}
static void Main()
{
int length = 11, cut1 = 2, cut2 = 3, cut3 = 5;
Console.WriteLine(
MaximizeCuts(length, cut1, cut2, cut3));
}
}
|
Time Complexity: O(3^n)
Space Complexity: O(1)
Maximize the number of segments of length p, q, and r using Memoization:
This can be visualized as a classical recursion problem, which further narrows down to the memoization ( top-down ) method of Dynamic Programming.
Follow the below steps to solve the problem:
- Initially, we have length l present with us, we’d have three size choices to cut from this, either we can make a cut of length p, q, or r
- Let’s say we made a cut of length p, so the remaining length would be l-p, and similarly, with cuts q & r resulting in remaining lengths l-q & l-r respectively.
- We will call the recursive function for the remaining lengths and at any subsequent instance, we’ll have these three choices.
- We will store the answer from all these recursive calls & take the maximum out of them +1 as at any instance we’ll have 1 cut from this particular call as well.
Note: The recursive call would be made if and only if the available length is greater than the length we want to cut i.e. suppose p=3, and after certain recursive calls the available length is 2 only, so we can’t cut this line in lengths of p anymore.
Below is the pseudocode for the above approach:
C
if (l == 0)
return 0;
int a, b, c;
if (p <= l)
a = func(l - p, p, q, r);
if (q <= l)
b = func(l - q, p, q, r);
if (r <= l)
c = func(l - r, p, q, r);
return 1 + max({ a, b, c });
|
Below is the recursion tree for Input: l=4,p=2,q=1 and r=1:

Recursion Tree for l=4 , p=2 ,q=1 and r=1
One can clearly observe that at each call, the given length ( 4 initially ) is divided into 3 different subparts. Also, we can see that the recursion is being repeated for certain entries (The red arrow represents a repetitive call for l=2, Yellow for l=3, and Blue for l=1). Therefore, we can memoize the results in any container or array, so that repetition of the same recursive calls is avoided.
Now, the above pseudocode changes to:
C++
vector< int > dp(10005,-1);
if (l==0)
return 0;
if (dp[l]!=-1)
return dp[l];
int a,b,c;
if (p<=l)
a=func(l-p,p,q,r);
if (q<=l)
b=func(l-q,p,q,r);
if (r<=l)
c=func(l-r,p,q,r);
return dp[l]=1+max({a,b,c});
|
Java
import java.util.Arrays;
public class DPExample {
static int [] dp = new int [ 10005 ];
static {
Arrays.fill(dp, - 1 );
}
static int func( int l, int p, int q, int r) {
if (l == 0 )
return 0 ;
if (dp[l] != - 1 )
return dp[l];
int a = Integer.MIN_VALUE, b = Integer.MIN_VALUE, c = Integer.MIN_VALUE;
if (p <= l)
a = func(l - p, p, q, r);
if (q <= l)
b = func(l - q, p, q, r);
if (r <= l)
c = func(l - r, p, q, r);
return dp[l] = 1 + Math.max(Math.max(a, b), c);
}
public static void main(String[] args) {
int l = 10 ;
int p = 2 , q = 3 , r = 5 ;
int result = func(l, p, q, r);
System.out.println( "Maximum value: " + result);
}
}
|
Python3
dp = [ - 1 ] * 10005
def func(l, p, q, r):
if l = = 0 :
return 0
if dp[l] ! = - 1 :
return dp[l]
a, b, c = float ( "-inf" ), float ( "-inf" ), float ( "-inf" )
if p < = l:
a = func(l - p, p, q, r)
if q < = l:
b = func(l - q, p, q, r)
if r < = l:
c = func(l - r, p, q, r)
dp[l] = 1 + max (a, b, c)
return dp[l]
if __name__ = = "__main__" :
l = 10
p, q, r = 2 , 3 , 5
result = func(l, p, q, r)
print ( "Maximum value:" , result)
|
C#
using System;
class Program {
static int [] dp = new int [10005];
static int Func( int l, int p, int q, int r)
{
if (l == 0)
return 0;
if (dp[l] != -1)
return dp[l];
int a = int .MinValue, b = int .MinValue,
c = int .MinValue;
if (p <= l)
a = Func(l - p, p, q, r);
if (q <= l)
b = Func(l - q, p, q, r);
if (r <= l)
c = Func(l - r, p, q, r);
dp[l] = 1 + Math.Max(a, Math.Max(b, c));
return dp[l];
}
static void Main( string [] args)
{
int l = 10;
int p = 2, q = 3, r = 5;
for ( int i = 0; i < dp.Length; i++) {
dp[i] = -1;
}
int result = Func(l, p, q, r);
Console.WriteLine( "Maximum value: " + result);
}
}
|
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int dp[10005];
int func( int l, int p, int q, int r)
{
if (l == 0)
return 0;
if (dp[l] != -1)
return dp[l];
int a(INT_MIN), b(INT_MIN),
c(INT_MIN);
if (p <= l)
a = func(l - p, p, q, r);
if (q <= l)
b = func(l - q, p, q, r);
if (r <= l)
c = func(l - r, p, q, r);
return dp[l] = 1 + max({ a, b, c });
}
int maximizeTheCuts( int l, int p, int q, int r)
{
memset (dp, -1, sizeof (dp));
int ans = func(l, p, q, r);
if (ans < 0)
return 0;
return ans;
}
int main()
{
int l = 11, p = 2, q = 3, r = 5;
cout << maximizeTheCuts(l, p, q, r) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int [] dp;
static int func( int l, int p, int q, int r)
{
if (l == 0 )
return 0 ;
if (dp[l] != - 1 )
return dp[l];
int a, b, c;
a = Integer.MIN_VALUE;
b = Integer.MIN_VALUE;
c = Integer.MIN_VALUE;
if (p <= l)
a = func(l - p, p, q, r);
if (q <= l)
b = func(l - q, p, q, r);
if (r <= l)
c = func(l - r, p, q, r);
return dp[l] = 1
+ Math.max(Math.max(a, b),
c);
}
static int maximizeTheCuts( int l, int p, int q, int r)
{
Arrays.fill(dp, - 1 );
int ans = func(l, p, q, r);
if (ans < 0 )
return 0 ;
return ans;
}
public static void main(String[] args)
{
dp = new int [ 10005 ];
int l = 11 , p = 2 , q = 3 , r = 5 ;
System.out.println(maximizeTheCuts(l, p, q, r));
}
}
|
Python3
import sys
dp = [ 0 ] * 10005
def func(l, p, q, r):
if (l = = 0 ):
return 0
if (dp[l] ! = - 1 ):
return dp[l]
a, b, c = - 1 * sys.maxsize, - 1 * sys.maxsize, - 1 * sys.maxsize
if (p < = l):
a = func(l - p, p, q, r)
if (q < = l):
b = func(l - q, p, q, r)
if (r < = l):
c = func(l - r, p, q, r)
dp[l] = 1 + max ( max (a, b),c)
return dp[l]
def maximizeTheCuts(l, p, q, r):
for i in range ( len (dp)):
dp[i] = - 1
ans = func(l, p, q, r)
if (ans < 0 ):
return 0
return ans
l = 11
p = 2
q = 3
r = 5
print (maximizeTheCuts(l, p, q, r))
|
C#
using System;
class GFG {
static int [] dp;
static int func( int l, int p, int q, int r)
{
if (l == 0)
return 0;
if (dp[l] != -1)
return dp[l];
int a, b, c;
a = Int32.MinValue;
b = Int32.MinValue;
c = Int32.MinValue;
if (p <= l)
a = func(l - p, p, q, r);
if (q <= l)
b = func(l - q, p, q, r);
if (r <= l)
c = func(l - r, p, q, r);
return dp[l] = 1
+ Math.Max(Math.Max(a, b),
c);
}
static int maximizeTheCuts( int l, int p, int q, int r)
{
for ( int i=0;i<dp.Length;i++)
{
dp[i]=-1;
}
int ans = func(l, p, q, r);
if (ans < 0)
return 0;
return ans;
}
static public void Main ()
{
dp = new int [10005];
int l = 11, p = 2, q = 3, r = 5;
Console.WriteLine(maximizeTheCuts(l, p, q, r));
}
}
|
Javascript
let dp= new Array(10005);
function func( l, p, q, r)
{
if (l == 0)
return 0;
if (dp[l] != -1)
return dp[l];
let a=Number.MIN_SAFE_INTEGER;
let b=Number.MIN_SAFE_INTEGER;
let c=Number.MIN_SAFE_INTEGER;
if (p <= l)
a = func(l - p, p, q, r);
if (q <= l)
b = func(l - q, p, q, r);
if (r <= l)
c = func(l - r, p, q, r);
return dp[l] = 1 + Math.max(Math.max(a, b),c);
}
function maximizeTheCuts(l, p, q, r)
{
for (let i = 0; i < dp.length; i++)
dp[i] = -1;
let ans = func(l, p, q, r);
if (ans < 0)
return 0;
return ans;
}
let l = 11, p = 2, q = 3, r = 5;
console.log(maximizeTheCuts(l, p, q, r));
|
Time Complexity: O(N) where n is the length of the rod or line segment that has to be cut
Auxiliary Space: O(N) where n is the length of the rod or line segment that has to be cut
Maximize the number of segments of length p, q, and r using Dynamic Programming (DP):
As the solution for the 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 the 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 a given length, find the 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:
- Not possible to cut->0
- Possible cut is of lengths 2->1 (2)
- Possible cut is of lengths 3->1 (3)
- Possible cuts are of lengths max(arr[4-2],arr[4-3])+1->2 (2,2)
- Possible cuts are of lengths max(arr[5-2],arr[5-3])+1->2 (2,3)
- Possible cuts are of lengths max(arr[6-2],arr[6-3],arr[6-5])+1->3 (2,2,2)
- Possible cuts are of lengths max(arr[7-2],arr[7-3],arr[7-5])+1->3 (2,3,2) or (2,2,3)
- Possible cuts are of lengths max(arr[8-2],arr[8-3],arr[8-5])+1->4 (2,2,2,2)
- 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)
- Possible cuts are of lengths max(arr[10-2],arr[10-3],arr[10-5])+1->5 (2,2,2,2,2)
- 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)
Follow the below steps to solve the problem:
- Initialise an array DP[]={-1} and DP[0]=0.
- Run a loop from ‘1’ to ‘l’
- If DP[i]=-1 means it’s not possible to divide it using giving segments p,q,r so 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]
Pseudo-Code:
C++
DP[l+1]={-1}
DP[0]=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])
|
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaximum( int l, int p, int q, int r)
{
int dp[l + 1];
memset (dp, -1, sizeof (dp));
dp[0] = 0;
for ( int i = 0; i <= l; i++) {
if (dp[i] == -1)
continue ;
if (i + p <= l)
dp[i + p] = max(dp[i + p], dp[i] + 1);
if (i + q <= l)
dp[i + q] = max(dp[i + q], dp[i] + 1);
if (i + r <= l)
dp[i + r] = max(dp[i + r], dp[i] + 1);
}
if (dp[l] == -1) {
dp[l] = 0;
}
return dp[l];
}
int main()
{
int l = 11, p = 2, q = 3, r = 5;
int ans = findMaximum(l, p, q, r);
cout << ans;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findMaximum( int l, int p, int q, int r)
{
int dp[] = new int [l + 1 ];
for ( int i = 0 ; i < l + 1 ; i++)
dp[i] = - 1 ;
dp[ 0 ] = 0 ;
for ( int i = 0 ; i <= l; i++) {
if (dp[i] == - 1 )
continue ;
if (i + p <= l)
dp[i + p] = Math.max(dp[i + p], dp[i] + 1 );
if (i + q <= l)
dp[i + q] = Math.max(dp[i + q], dp[i] + 1 );
if (i + r <= l)
dp[i + r] = Math.max(dp[i + r], dp[i] + 1 );
}
if (dp[l] == - 1 ) {
dp[l] = 0 ;
}
return dp[l];
}
public static void main(String[] args)
{
int l = 11 , p = 2 , q = 3 , r = 5 ;
int ans = findMaximum(l, p, q, r);
System.out.println(ans);
}
}
|
Python3
def findMaximum(l, p, q, r):
dp = [ - 1 ] * (l + 1 )
dp[ 0 ] = 0
for i in range (l + 1 ):
if (dp[i] = = - 1 ):
continue
if (i + p < = l):
dp[i + p] = ( max (dp[i + p],
dp[i] + 1 ))
if (i + q < = l):
dp[i + q] = ( max (dp[i + q],
dp[i] + 1 ))
if (i + r < = l):
dp[i + r] = ( max (dp[i + r],
dp[i] + 1 ))
if dp[l] = = - 1 :
dp[l] = 0
return dp[l]
if __name__ = = "__main__" :
l = 11
p = 2
q = 3
r = 5
ans = findMaximum(l, p, q, r)
print (ans)
|
C#
using System;
class GFG {
static int findMaximum( int l, int p, int q, int r)
{
int [] dp = new int [l + 1];
for ( int i = 0; i < l + 1; i++)
dp[i] = -1;
dp[0] = 0;
for ( int i = 0; i <= l; i++) {
if (dp[i] == -1)
continue ;
if (i + p <= l)
dp[i + p] = Math.Max(dp[i + p], dp[i] + 1);
if (i + q <= l)
dp[i + q] = Math.Max(dp[i + q], dp[i] + 1);
if (i + r <= l)
dp[i + r] = Math.Max(dp[i + r], dp[i] + 1);
}
if (dp[l] == -1) {
dp[l] = 0;
}
return dp[l];
}
public static void Main()
{
int l = 11, p = 2, q = 3, r = 5;
int ans = findMaximum(l, p, q, r);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function findMaximum(l,p,q,r)
{
let dp = new Array(l + 1);
for (let i = 0; i < l + 1; i++)
dp[i] = -1;
dp[0] = 0;
for (let i = 0; i <= l; i++) {
if (dp[i] == -1)
continue ;
if (i + p <= l)
dp[i + p] = Math.max(dp[i + p], dp[i] + 1);
if (i + q <= l)
dp[i + q] = Math.max(dp[i + q], dp[i] + 1);
if (i + r <= l)
dp[i + r] = Math.max(dp[i + r], dp[i] + 1);
}
if (dp[l] == -1) {
dp[l] = 0;
}
return dp[l];
}
let l = 11, p = 2, q = 3, r = 5;
let ans = findMaximum(l, p, q, r);
document.write(ans);
</script>
|
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
Note: This problem can also be thought of as a minimum coin change problem because we are given a certain length to acquire which is the same as the value of the amount whose minimum change is needed. Now the x,y, and z are the same as the denomination of the coin given. So length is the same as the amount and x y z are the same as denominations, thus we need to change only one condition that is instead of finding the minimum we need to find the maximum and we will get the answer. As the minimum coin change problem is the basic dynamic programming question so this will help to solve this question also.
The condition we need to change in the minimum coin change problem is:
C++
for (ll i=1;i<=n;i++)
{
for (ll j=1;j<=3;j++)
{
if (i>=a[j]&&m[i-a[j]]!=-1)
{
dp[i]=max(dp[i],1+dp[i-a[j]]);
}
}
}
|
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Nov, 2023
Like Article
Save Article