Maximum Product Cutting | DP-36 in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find maximum Product Cutting | DP-36.

For this we will be provided with a rope of N meters. Our task is to cut the rope in different integer lengths such that the product of their lengths is the maximum

Example

 Live Demo

#include <iostream>
using namespace std;
//finding maximum of two, three integers
int max(int a, int b) {
   return (a > b)? a : b;
}
int max(int a, int b, int c) {
   return max(a, max(b, c));
}
//returning maximum product
int maxProd(int n) {
   if (n == 0 || n == 1) return 0;
      int max_val = 0;
   for (int i = 1; i < n; i++)
      max_val = max(max_val, i*(n-i), maxProd(n-i)*i);
   return max_val;
}
int main() {
   cout << "Maximum Product is " << maxProd(10);
   return 0;
}

Output

Maximum Product is 36
raja
Published on 09-Sep-2020 12:48:26
Advertisements