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

C++Server Side ProgrammingProgramming

<p style="">In this tutorial, we will be discussing a program to find maximum value with the choice of either dividing or considering as it is.</p><p>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).</p><h2 style="">Example</h2><p><a class="demo" href="http://tpcg.io/gisIta1L" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">#include &lt;bits/stdc++.h&gt; using namespace std; //calculating the maximum result int findMaximum(int size) { &nbsp; &nbsp;int term[size + 1]; &nbsp; &nbsp;term[0] = 0; &nbsp; &nbsp;term[1] = 1; &nbsp; &nbsp;int i=2; &nbsp; &nbsp;while(i &lt;= size) { &nbsp; &nbsp; &nbsp; term[i] = max(i, (term[i / 2] + term[i / 3] + term[i / 4] + term[i / 5])); &nbsp; &nbsp; &nbsp; i = i+1; &nbsp; &nbsp;} &nbsp; &nbsp;return term[size]; } int main() { &nbsp; &nbsp;int number = 37; &nbsp; &nbsp;cout &lt;&lt; &quot;Maximum possible sum: &quot; &lt;&lt; findMaximum(number)&lt;&lt; endl; &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><pre class="result notranslate" style="">Maximum possible sum: 57</pre>
raja
Updated on 21-Aug-2020 05:33:51

Advertisements