Minimum number of letters needed to make a total of n in C++.

C++Server Side ProgrammingProgramming

<h2>Problem statement</h2><p>Given an integer n and let a = 1, b = 2, c= 3, &hellip;.., z = 26. The task is to find the minimum number of letters needed to make a total of n</p><pre class="result notranslate">If n = 23 then output is 1 If n = 72 then output is 3(26 + 26 + 20)</pre><h2>Algorithm</h2><pre class="result notranslate">1. If n is divisible by 26 then answer is (n/26) 2. If n is not divisible by 26 then answer is (n/26) + 1</pre><h2>Example</h2><!--<p><a href="" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a></p>--><pre class="prettyprint notranslate">#include &lt;iostream&gt; using namespace std; int minRequiredSets(int n){ &nbsp; &nbsp;if (n % 26 == 0) { &nbsp; &nbsp; &nbsp; return (n / 26); &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; return (n / 26) + 1; &nbsp; &nbsp;} } int main(){ &nbsp; &nbsp;int n = 72; &nbsp; &nbsp;cout &lt;&lt; &quot;Minimum required sets: &quot; &lt;&lt; minRequiredSets(n) &lt;&lt; endl; &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>When you compile and execute the above program. It generates the following output &minus;</p><pre class="result notranslate">Minimum required sets: 3</pre>
raja
Updated on 31-Oct-2019 07:02:45

Advertisements