jain12

Maximum product by recursion

Jun 8th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int MaxP(int len){
  5.   if(len==1)
  6.     return 1;
  7.   int res=len;
  8.   for(int i=1;i<len;i++)
  9.     res=max(res,i*MaxP(len-i));
  10.   return res;
  11.   }
  12.  
  13. int MaxProduct(int length){
  14.  
  15.   int res=0;
  16.   for(int i=1;i<length;i++){
  17.     res=max(res,i*MaxP(length-i));
  18.     }
  19.   return res;
  20.   }
  21.  
  22. int main(){
  23.   int n= 10;
  24.   cout<<MaxProduct(n);
  25.   return 0;
  26.   }
Add Comment
Please, Sign In to add comment