This is a C++ Program that Solves Rod Cutting – Maximum Product Problem using Dynamic Programming technique.
You are given a rod of integer length. You have to cut the rod in various pieces such that the product of the lengths of all pieces is maximum.
Calculate the results in bottom up fashion, starting from 1 and cache the results to avoid recomputation.
If length=1, Expected result=0 since the rod can not be cut
If length=2, Expected result=1 (1*1)
If length=3, Expected result=2 (2*1)
Here is source code of the C++ Program to Solve Rod Cutting – Maximum Product Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
using namespace std;
int maxProductCutting(int length)
//array to cache results
//result[i]=maximum product after cutting the rod of length i
//rod of length 0 and 1 can not be cut into pieced
cout<<"Enter the length of the rod"<<endl;
cout<<"Maximum product after cutting the rod into pieces is "<<endl;
In the main function, we ask the user to input the value for length of hte rod. We pass this value to the function maxProductCutting as parameter. This function will calculate the expected result and return it. The returned value will be displayed.
Case-1: $ g++ max_product_cutting.cpp $ ./a.out Enter the length of the rod 7 Maximum product after cutting the rod into pieces is 12
Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.