Given a rod of length
N
meters, and the rod can be cut in only 3 sizes
A
,
B
and
C
. The task is to maximizes the number of cuts in rod. If it is impossible to make cut then print
-1
.
Examples:
Input: N = 17, A = 10, B = 11, C = 3 Output: 3 Explanation: The maximum cut can be obtain after making 2 cut of length 3 and one cut of length 11. Input: N = 10, A = 9, B = 7, C = 11 Output: -1 Explanation: It is impossible to make any cut so output will be -1.
Naive Approach:
- Let us assume x, y, and z numbers of rods of sizes A, B, and C respectively are cut. And this can be written as a linear equation: x*A + y*B + z*C = N
- Now, simply iterate over all possible value of x and y and compute z using (N – x*A + y*B) / c.
- If x*A + y*B + z*C = N, then it is one of the possible answers.
- Finally compute the maximum value of x + y + z.
Time Complexity:
O(N
2
)
Auxiliary Space:
O(1)
Efficient Approach:
The problem can be solve using Dynamic Programming.
- Create a dp[] array of size N and initialise all value to INT_MIN.
- Set dp[0] = 0, as it will be base case for our approach.
- Iterate from 1 to N and check if it is possible to make a cut of any of possible length i.e A, B and C, and update dp[i] to minimum of all.
-
dp[i] = min (dp[i], 1 + subresult) where, subresult = min(dp[i – A], min(dp[i – B], dp[i – C]))
-
- Here is the implementation of the above approach:
-
C++
// A Dynamic Programming solution for// Maximum Rod cutting problem#include <bits/stdc++.h>usingnamespacestd;// function that eturns the maximum// number of rods that can be// made from the rod of length NintcuttingRod(intarr[],intN){intdp[N + 1];// Initializing the number of rods we// can make from length 0dp[0] = 0;// Iterating over lengths that can// be formedfor(inti = 1; i <= N; i++) {// Initializing the possible// cuts as infinitedp[i] = INT_MIN;// Cutting the desired lengthsfor(intj = 0; j < 3; j++) {// Checking whether the length of// rod becomes 0 or if after cutting// the rod, it becomes uselessif((i - arr[j]) >= 0&& dp[i - arr[j]] != INT_MIN) {// Choosing the maximum// possible desired// length cuts to be madedp[i] = max(dp[i - arr[j]] + 1,dp[i]);}}}returndp[N];}// Driver codeintmain(){intN = 17;intarr[] = { 10, 11, 3 };cout << cuttingRod(arr, N);return0;}Java
// A Dynamic Programming solution for// Maximum Rod cutting problemclassGFG{// Function that eturns the maximum// number of rods that can be// made from the rod of length NstaticintcuttingRod(intarr[],intN){int[]dp =newint[N +1];// Initializing the number of rods we// can make from length 0dp[0] =0;// Iterating over lengths that can// be formedfor(inti =1; i <= N; i++){// Initializing the possible// cuts as infinitedp[i] = Integer.MIN_VALUE;// Cutting the desired lengthsfor(intj =0; j <3; j++){// Checking whether the length of// rod becomes 0 or if after cutting// the rod, it becomes uselessif((i - arr[j]) >=0&&dp[i - arr[j]] != Integer.MIN_VALUE){// Choosing the maximum// possible desired// length cuts to be madedp[i] = Math.max(dp[i - arr[j]] +1,dp[i]);}}}returndp[N];}// Driver codepublicstaticvoidmain(String[] args){intN =17;intarr[] = {10,11,3};System.out.print(cuttingRod(arr, N));}}// This code is contributed by Princi SinghPython3
# A Dynamic Programming solution for# Maximum Rod cutting problemimportsys# Function that returns the maximum# number of rods that can be# made from the rod of length NdefcuttingRod(arr, N):dp=(N+1)*[0]# Initializing the number of rods we# can make from length 0dp[0]=0# Iterating over lengths that can# be formedforiinrange(1, N+1):# Initializing the possible# cuts as infinitedp[i]=-sys.maxsize-1# Cutting the desired lengthsforjinrange(3):# Checking whether the length of# rod becomes 0 or if after cutting# the rod, it becomes uselessif((i-arr[j]) >=0anddp[i-arr[j]] !=-sys.maxsize-1):# Choosing the maximum# possible desired# length cuts to be madedp[i]=max(dp[i-arr[j]]+1,dp[i])returndp[N]# Driver codeif__name__=="__main__":N=17arr=[10,11,3]print(cuttingRod(arr, N))# This code is contributed by chitranayalC#
// A Dynamic Programming solution for// Maximum Rod cutting problemusingSystem;classGFG{// Function that eturns the maximum// number of rods that can be// made from the rod of length NstaticintcuttingRod(int[] arr,intN){int[]dp =newint[N + 1];// Initializing the number of rods we// can make from length 0dp[0] = 0;// Iterating over lengths that can// be formedfor(inti = 1; i <= N; i++){// Initializing the possible// cuts as infinitedp[i] = Int32.MinValue;// Cutting the desired lengthsfor(intj = 0; j < 3; j++){// Checking whether the length of// rod becomes 0 or if after cutting// the rod, it becomes uselessif((i - arr[j]) >= 0 &&dp[i - arr[j]] != Int32.MinValue){// Choosing the maximum// possible desired// length cuts to be madedp[i] = Math.Max(dp[i - arr[j]] + 1,dp[i]);}}}returndp[N];}// Driver codepublicstaticvoidMain(){intN = 17;int[] arr = { 10, 11, 3 };Console.Write(cuttingRod(arr, N));}}// This code is contributed by code_huntJavascript
// A Dynamic Programming solution for// Maximum Rod cutting problem// function that eturns the maximum// number of rods that can be// made from the rod of length Nconst cuttingRod = (arr, N) => {const dp =newArray(N + 1);// Initializing the number of rods we// can make from length 0dp[0] = 0;// Iterating over lengths that can// be formedfor(let i = 1; i <= N; i++) {// Initializing the possible// cuts as infinitedp[i] = Number.MIN_SAFE_INTEGER;// Cutting the desired lengthsfor(let j = 0; j < 3; j++) {// Checking whether the length of// rod becomes 0 or if after cutting// the rod, it becomes uselessif((i - arr[j]) >= 0 && dp[i - arr[j]] !== Number.MIN_SAFE_INTEGER) {// Choosing the maximum// possible desired// length cuts to be madedp[i] = Math.max(dp[i - arr[j]] + 1, dp[i]);}}}returndp[N];}// Driver codeconst N = 17;const arr = [10, 11, 3];console.log(cuttingRod(arr, N)); -
Output
3
- Time Complexity:
- O (N)
- Auxiliary Space:
- O (N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Information to that Topic: geeksforgeeks.org/maximise-number-of-cuts-in-a-rod-if-it-can-be-cut-only-in-given-3-sizes/ […]
… [Trackback]
[…] Find More Information here on that Topic: geeksforgeeks.org/maximise-number-of-cuts-in-a-rod-if-it-can-be-cut-only-in-given-3-sizes/ […]