diff --git a/Dynamic Programming/rod_cutting.cpp b/Dynamic Programming/rod_cutting.cpp new file mode 100644 index 0000000..d150f74 --- /dev/null +++ b/Dynamic Programming/rod_cutting.cpp @@ -0,0 +1,36 @@ + +#include +#include +#include +using namespace std; + +int max(int a, int b) { return (a > b) ? a : b; } + +int cutRod(int price[], int index, int n) +{ + if (index == 0) { + return n * price[0]; + } + + int notCut = cutRod(price,index - 1,n); + int cut = INT_MIN; + int rod_length = index + 1; + + if (rod_length <= n) + cut = price[index] + + cutRod(price,index,n - rod_length); + + return max(notCut, cut); +} + +int main() +{ + int arr[] = { 1, 5, 8, 9, 10, 17, 17, 20 }; + int size = sizeof(arr) / sizeof(arr[0]); + cout << "Maximum Obtainable Value is " + << cutRod(arr, size - 1, size); + getchar(); + return 0; +} + +