Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum Product Cutting | DP-36 in C++
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
#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
Advertisements