Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)

length | 1 2 3 4 5 6 7 8 -------------------------------------------- price | 1 5 8 9 10 17 17 20

And if the prices are as following, then the maximum obtainable value is 24 (by cutting in eight pieces of length 1)

length | 1 2 3 4 5 6 7 8 -------------------------------------------- price | 3 5 8 9 10 17 17 20

##
Recommended: Please solve it on “__PRACTICE__ ” first, before moving on to the solution.

__PRACTICE__Following is simple recursive implementation of the Rod Cutting problem. The implementation simply follows the recursive structure mentioned above.

## C#

`// A Naive recursive solution for ` `// Rod cutting problem ` `using` `System; ` `class` `GFG { ` ` ` ` ` `/* Returns the best obtainable ` ` ` `price for a rod of length ` ` ` `n and price[] as prices of ` ` ` `different pieces */` ` ` `static` `int` `cutRod(` `int` `[] price, ` `int` `n) ` ` ` `{ ` ` ` `if` `(n <= 0) ` ` ` `return` `0; ` ` ` `int` `max_val = ` `int` `.MinValue; ` ` ` ` ` `// Recursively cut the rod in ` ` ` `// different pieces and compare ` ` ` `// different configurations ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `max_val = Math.Max(max_val, price[i] + cutRod(price, n - i - 1)); ` ` ` ` ` `return` `max_val; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = ` `new` `int` `[] { 1, 5, 8, 9, 10, 17, 17, 20 }; ` ` ` `int` `size = arr.Length; ` ` ` `Console.WriteLine(` `"Maximum Obtainable Value is "` `+ cutRod(arr, size)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

**Output:**

Maximum Obtainable Value is 22

Considering the above implementation, following is recursion tree for a Rod of length 4.

cR() ---> cutRod() cR(4) / / / / cR(3) cR(2) cR(1) cR(0) / | / | / | / | cR(2) cR(1) cR(0) cR(1) cR(0) cR(0) / | | / | | cR(1) cR(0) cR(0) cR(0) / / CR(0)

In the above partial recursion tree, cR(2) is being solved twice. We can see that there are many subproblems which are solved again and again. Since same suproblems are called again, this problem has Overlapping Subprolems property. So the Rod Cutting problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array val[] in bottom up manner.

## C#

`// A Dynamic Programming solution ` `// for Rod cutting problem ` `using` `System; ` `class` `GFG { ` ` ` ` ` `/* Returns the best obtainable ` ` ` `price for a rod of length n ` ` ` `and price[] as prices of ` ` ` `different pieces */` ` ` `static` `int` `cutRod(` `int` `[] price, ` `int` `n) ` ` ` `{ ` ` ` `int` `[] val = ` `new` `int` `[n + 1]; ` ` ` `val[0] = 0; ` ` ` ` ` `// Build the table val[] in ` ` ` `// bottom up manner and return ` ` ` `// the last entry from the table ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `int` `max_val = ` `int` `.MinValue; ` ` ` `for` `(` `int` `j = 0; j < i; j++) ` ` ` `max_val = Math.Max(max_val, ` ` ` `price[j] + val[i - j - 1]); ` ` ` `val[i] = max_val; ` ` ` `} ` ` ` ` ` `return` `val[n]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = ` `new` `int` `[] { 1, 5, 8, 9, 10, 17, 17, 20 }; ` ` ` `int` `size = arr.Length; ` ` ` `Console.WriteLine(` `"Maximum Obtainable Value is "` `+ cutRod(arr, size)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

**Output:**

Maximum Obtainable Value is 22

Please refer complete article on Cutting a Rod | DP-13 for more details!

## Recommended Posts:

- C# Program for Naive algorithm for Pattern Searching
- C# Program for KMP Algorithm for Pattern Searching
- C# Program to print all permutations of a given string
- C# Program for Count Inversions in an array | Set 1 (Using Merge Sort)
- C# Program for Median of two sorted arrays of same size
- C# Program for Activity Selection Problem | Greedy Algo-1
- C# Program for Subset Sum Problem | DP-25
- C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- C# Program for Maximum sum rectangle in a 2D matrix | DP-27
- C# Program for Largest Sum Contiguous Subarray
- C# Program for Maximum size square sub-matrix with all 1s
- C# Program for Minimum number of jumps to reach end
- C# Program for Longest Palindromic Subsequence | DP-12
- C# Program for Egg Dropping Puzzle | DP-11
- C# Program for Matrix Chain Multiplication | DP-8
- C# Program for Count set bits in an integer
- C# Program to Find the Number Occurring Odd Number of Times
- C# Program for Anagram Substring Search (Or Search for all permutations)
- C# Program to Count number of binary strings without consecutive 1's
- C# Program for Count ways to reach the n\'th stair