Maximum value with the choice of either dividing or considering as it is in C++ program

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find maximum value with the choice of either dividing or considering as it is.

For this we will be provided with an integer value. Our task is to find the maximum value with either by dividing the number into four parts recursively or choosing it as it is using the given function F(n) = max( (F(n/2) + F(n/3) + F(n/4) + F(n/5)), n).

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//calculating the maximum result
int maxDP(int n) {
   int res[n + 1];
   res[0] = 0;
   res[1] = 1;
   for (int i = 2; i <= n; i++)
      res[i] = max(i, (res[i / 2] + res[i / 3] + res[i / 4] + res[i / 5]));
   return res[n];
}
int main() {
   int n = 60;
   cout << "MaxSum =" << maxDP(n);
   return 0;
}

Output

MaxSum =106
raja
Published on 27-Jul-2020 12:14:08
Advertisements