# Minimize the total number of teddies to be distributed in C++

C++Server Side ProgrammingProgramming

<h2>Problem statement</h2><p>Given N number of students and an array which represent the mark obtained by students. School has dicided to give them teddy as a price. Hoever, school wants to save money, so they to minimize the total number of teddies to be distrubuted by imposing following constrain &minus;</p><ul class="list"><li>All students must get atleast one teddy</li><li>If two students are sitting next to each other then student with the higher marks must get more</li><li>If two students have same marks then they are allowed to get different number of teddies</li></ul><h2>Example</h2><p>Let us suppose there are 3 students and marks obtained are represented in array as &minus;</p><pre class="result notranslate">arr[] = {2, 3, 3} So, total number of teddies to be distributed: {1, 2, 1} i.e. 4 teddies</pre><h2>Algorithm</h2><p>This problem can be solved using dynamic programming as follows &minus;</p><pre class="result notranslate">1. Create a table of size N and initialize it with 1 as each student must get atleast one teddy 2. Iterate over marks array and perform below step: &nbsp; &nbsp;a. If current student has more marks than previous student then: &nbsp; &nbsp; &nbsp; i. Get the number of teddies given to the previous student &nbsp; &nbsp; &nbsp; ii. Increment teddie count by 1 &nbsp; &nbsp;b. If current student has lesser marks than previous student then: &nbsp; &nbsp; &nbsp; i. Review and change all the values assigned earlier</pre><h2>Example</h2><pre class="prettyprint notranslate">#include &lt;iostream&gt; #include &lt;algorithm&gt; #define SIZE(arr) (sizeof(arr) / sizeof(arr)) using namespace std; int teddieDistribution(int *marks, int n) { &nbsp; &nbsp;int table[n]; &nbsp; &nbsp;fill(table, table + n, 1); &nbsp; &nbsp;for (int i = 0; i &lt; n - 1; ++i) { &nbsp; &nbsp; &nbsp; if (marks[i + 1] &gt; marks[i]) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table[i + 1] = table[i] + 1; &nbsp; &nbsp; &nbsp; } else if (marks[i] &gt; marks[i + 1]) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int temp = i; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (true) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (temp &gt;= 0 &amp;&amp; (marks[temp] &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; marks[temp + 1])) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (table[temp] &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table[temp + 1]) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --temp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table[temp] = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table[temp + 1] + 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --temp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;int totalTeddies = 0; &nbsp; &nbsp;for (int i = 0; i &lt; n; ++i) { &nbsp; &nbsp; &nbsp; totalTeddies += table[i]; &nbsp; &nbsp;} &nbsp; &nbsp;return totalTeddies; } int main() { &nbsp; &nbsp;int marks[] = {2, 6, 5, 2, 3, 7}; &nbsp; &nbsp;int totalTeddies = teddieDistribution(marks, &nbsp; &nbsp;SIZE(marks)); &nbsp; &nbsp; &nbsp; cout &lt;&lt; &quot;Total teddies to be distributed: &quot; &lt;&lt; &nbsp; &nbsp;totalTeddies &lt;&lt; &quot; &quot;; &nbsp; &nbsp; &nbsp; return 0; }</pre><h2>Output</h2><p>When you compile and execute above program. It generates following output &minus;</p><pre class="result notranslate">Total teddies to be distributed: 12</pre>