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


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 findMaximum(int size) {
   int term[size + 1];
   term[0] = 0;
   term[1] = 1;
   int i=2;
   while(i <= size) {
      term[i] = max(i, (term[i / 2] + term[i / 3] + term[i / 4] + term[i / 5]));
      i = i+1;
   }
   return term[size];
}
int main() {
   int number = 37;
   cout << "Maximum possible sum: " << findMaximum(number)<< endl;
   return 0;
}

Output

Maximum possible sum: 57

Updated on: 21-Aug-2020

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements