Maximise number of cuts in a rod if it can be cut only in given 3 sizes

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 0   Âdp[0] = 0;   Â// Iterating over lengths that can   Â// be formed   Âfor(inti = 1; i <= N; i++) {       Â// Initializing the possible       Â// cuts as infinite       Âdp[i] = INT_MIN;       Â// Cutting the desired lengths       Âfor(intj = 0; j < 3; j++) {           Â// Checking whether the length of           Â// rod becomes 0 or if after cutting           Â// the rod, it becomes useless           Âif((i - arr[j]) >= 0               Â&& dp[i - arr[j]] != INT_MIN) {               Â// Choosing the maximum               Â// possible desired               Â// length cuts to be made               Âdp[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 0   Âdp[0] =0;   Â// Iterating over lengths that can   Â// be formed   Âfor(inti =1; i <= N; i++)   Â{       Â       Â// Initializing the possible       Â// cuts as infinite       Âdp[i] = Integer.MIN_VALUE;       Â// Cutting the desired lengths       Âfor(intj =0; j <3; j++)       Â{           Â           Â// Checking whether the length of           Â// rod becomes 0 or if after cutting           Â// the rod, it becomes useless           Âif((i - arr[j]) >=0&&             Âdp[i - arr[j]] != Integer.MIN_VALUE)           Â{               Â               Â// Choosing the maximum               Â// possible desired               Â// length cuts to be made               Âdp[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 0   Âdp[0]=0   Â# Iterating over lengths that can   Â# be formed   Âforiinrange(1, N+1):       Â# Initializing the possible       Â# cuts as infinite       Âdp[i]=-sys.maxsize-1       Â# Cutting the desired lengths       Âforjinrange(3):           Â# Checking whether the length of           Â# rod becomes 0 or if after cutting           Â# the rod, it becomes useless           Âif((i-arr[j]) >=0and             Âdp[i-arr[j]] !=-sys.maxsize-1):               Â# Choosing the maximum               Â# possible desired               Â# length cuts to be made               Âdp[i]=max(dp[i-arr[j]]+1,                           Âdp[i])   Âreturndp[N]Â# Driver codeif__name__=="__main__":   ÂN=17   Âarr=[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 0   Âdp[0] = 0;   Â// Iterating over lengths that can   Â// be formed   Âfor(inti = 1; i <= N; i++)   Â{       Â       Â// Initializing the possible       Â// cuts as infinite       Âdp[i] = Int32.MinValue;       Â// Cutting the desired lengths       Âfor(intj = 0; j < 3; j++)       Â{           Â           Â// Checking whether the length of           Â// rod becomes 0 or if after cutting           Â// the rod, it becomes useless           Âif((i - arr[j]) >= 0 &&             Âdp[i - arr[j]] != Int32.MinValue)           Â{               Â               Â// Choosing the maximum               Â// possible desired               Â// length cuts to be made               Âdp[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 0   Âdp[0] = 0;   Â   Â// Iterating over lengths that can   Â// be formed   Âfor(let i = 1; i <= N; i++) {       Â       Â// Initializing the possible       Â// cuts as infinite       Âdp[i] = Number.MIN_SAFE_INTEGER;       Â        Â// Cutting the desired lengths       Âfor(let j = 0; j < 3; j++) {           Â            Â// Checking whether the length of           Â// rod becomes 0 or if after cutting           Â// the rod, it becomes useless           Âif((i - arr[j]) >= 0 && dp[i - arr[j]] !== Number.MIN_SAFE_INTEGER) {               Â               Â// Choosing the maximum               Â// possible desired               Â// length cuts to be made               Âdp[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)
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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



