Which one provides the maximum revenue r(4)? 4 rods of size 1 (achieved by 3 cuts) = 4 x p(1) = 4 x 1 = 4 2 rods of size 1 + 1 rod of size 2 (achieved by 2 cuts) = 2 x p(1) + 1 x p(2) = 2 x 1 + 5 = 7 2 rods of size 2 (achieved by 1 cut)= 2 x p(2) = 2 x 5 = 10 1 rod of size 1 + 1 rod of size 3 (achieved by 2 cuts)= 1 x p(1) + 1 x p(3) = 1 + 8= 9 original rod of size 4 (achieved by no cuts)= 1 x p(4) = 9

Thus, maximum revenue possible is 10, can be achieved by making a cut at size=2, splitting the original rod into two rods of size 2 with no further cuts in any of them.

Generalizing it,

Max revenue r(n) on rod of size i=n, can be achieved by :

1. Starting with size i=1 and going up to size i=n repeat the following 2 & 3 : 2. Deciding whether a cut at i has to be made or not (results in 2 different cases) 3. Repeating from 1 for rest of the rod (n-i) 4. Calculating r(n) in every case at the end 5. Max value among all calculated r(n) is the answer. Additional storage is required to track the cuts made.

Recursion is an ideal candidate for this. Find recursive code here

What is the time complexity ?

This can be answered by asking how many ways a cut can be made ?

In a rod of size n, 1 cut can be made in (n-1)C(1) ways

2 cuts = (n-2) C(2) ways

k cuts = (n-k)C(k) ways

Totaling it all :

ways to do 1 cut + ways to do 2 cuts + ….. ways to do k cuts ..+ ways to do n-1 cuts

Thus, time complexity is exponential here. There should be a better way of doing things.

One trick is to use recursion with memoization i.e memo taking(explained here). However, still it remains an exponential operation which is infeasible for large values of n.

Enter Dynamic Programming.