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.

- Ceate 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. - Maximise matrix sum by following the given Path
- Maximise array sum after taking non-overlapping sub-arrays of length K
- Possible cuts of a number such that maximum parts are divisible by 3
- Cutting a Rod | DP-13
- Minimum tiles of sizes in powers of two to cover whole area
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Paper Cut into Minimum Number of Squares | Set 2
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Check if it is possible to get back to 12'0 clock only by adding or subtracting given seconds
- Count subtrees that sum up to a given value x only using single recursive function
- Number of handshakes such that a person shakes hands only once
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Minimum number of coins that can generate all the values in the given range
- Maximum number of strings that can be formed with given zeros and ones
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only
- Longest Consecuetive Subsequence when only one insert operation is allowed
- Maximize sum of K elements in Array by taking only corner elements

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> ` `using` `namespace` `std; ` ` ` `// function that eturns the maximum ` `// number of rods that can be ` `// made from the rod of length N ` `int` `cuttingRod(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `int` `dp[N + 1]; ` ` ` ` ` `// Initializing the number of rods we ` ` ` `// can make from length 0 ` ` ` `dp[0] = 0; ` ` ` ` ` `// Iterating over lengths that can ` ` ` `// be formed ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `// Initializing the possible ` ` ` `// cuts as infinite ` ` ` `dp[i] = INT_MIN; ` ` ` ` ` `// Cutting the desired lengths ` ` ` `for` `(` `int` `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]] != INT_MIN) { ` ` ` ` ` `// Choosing the maximum ` ` ` `// possible desired ` ` ` `// length cuts to be made ` ` ` `dp[i] = max(dp[i - arr[j]] + 1, ` ` ` `dp[i]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 17; ` ` ` `int` `arr[] = { 10, 11, 3 }; ` ` ` `cout << cuttingRod(arr, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// A Dynamic Programming solution for ` `// Maximum Rod cutting problem ` `class` `GFG{ ` ` ` `// Function that eturns the maximum ` `// number of rods that can be ` `// made from the rod of length N ` `static` `int` `cuttingRod(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `int` `[]dp = ` `new` `int` `[N + ` `1` `]; ` ` ` ` ` `// Initializing the number of rods we ` ` ` `// can make from length 0 ` ` ` `dp[` `0` `] = ` `0` `; ` ` ` ` ` `// Iterating over lengths that can ` ` ` `// be formed ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) ` ` ` `{ ` ` ` ` ` `// Initializing the possible ` ` ` `// cuts as infinite ` ` ` `dp[i] = Integer.MIN_VALUE; ` ` ` ` ` `// Cutting the desired lengths ` ` ` `for` `(` `int` `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]] != Integer.MIN_VALUE) ` ` ` `{ ` ` ` ` ` `// Choosing the maximum ` ` ` `// possible desired ` ` ` `// length cuts to be made ` ` ` `dp[i] = Math.max(dp[i - arr[j]] + ` `1` `, ` ` ` `dp[i]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `17` `; ` ` ` `int` `arr[] = { ` `10` `, ` `11` `, ` `3` `}; ` ` ` ` ` `System.out.print(cuttingRod(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python3

`# A Dynamic Programming solution for ` `# Maximum Rod cutting problem ` `import` `sys ` ` ` `# Function that returns the maximum ` `# number of rods that can be ` `# made from the rod of length N ` `def` `cuttingRod(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 ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `): ` ` ` ` ` `# Initializing the possible ` ` ` `# cuts as infinite ` ` ` `dp[i] ` `=` `-` `sys.maxsize ` `-` `1` ` ` ` ` `# Cutting the desired lengths ` ` ` `for` `j ` `in` `range` `(` `3` `): ` ` ` ` ` `# Checking whether the length of ` ` ` `# rod becomes 0 or if after cutting ` ` ` `# the rod, it becomes useless ` ` ` `if` `((i ` `-` `arr[j]) >` `=` `0` `and` ` ` `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]) ` ` ` `return` `dp[N] ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `17` ` ` `arr ` `=` `[ ` `10` `, ` `11` `, ` `3` `] ` ` ` ` ` `print` `(cuttingRod(arr, N)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// A Dynamic Programming solution for ` `// Maximum Rod cutting problem ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that eturns the maximum ` `// number of rods that can be ` `// made from the rod of length N ` `static` `int` `cuttingRod(` `int` `[] arr, ` `int` `N) ` `{ ` ` ` `int` `[]dp = ` `new` `int` `[N + 1]; ` ` ` ` ` `// Initializing the number of rods we ` ` ` `// can make from length 0 ` ` ` `dp[0] = 0; ` ` ` ` ` `// Iterating over lengths that can ` ` ` `// be formed ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `{ ` ` ` ` ` `// Initializing the possible ` ` ` `// cuts as infinite ` ` ` `dp[i] = Int32.MinValue; ` ` ` ` ` `// Cutting the desired lengths ` ` ` `for` `(` `int` `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]] != Int32.MinValue) ` ` ` `{ ` ` ` ` ` `// Choosing the maximum ` ` ` `// possible desired ` ` ` `// length cuts to be made ` ` ` `dp[i] = Math.Max(dp[i - arr[j]] + 1, ` ` ` `dp[i]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 17; ` ` ` `int` `[] arr = { 10, 11, 3 }; ` ` ` ` ` `Console.Write(cuttingRod(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O (N)

**Auxiliary Space:** 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.

## Recommended Posts:

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.