Question: 8. What is the approach implemented in the following code?<div class="hk1_style-wrap4"><div class="hk1_style-wrap3"><div class="hk1_style-wrap2"><div class="hk1_style-wrap"><div class="hk1_style"><div class="c"><pre class="de1"><span class="co2">#include&lt;iostream&gt; </span> using namespace std<span class="sy0">;</span> <span class="kw4">void</span> printArray<span class="br0">(</span><span class="kw4">int</span> p<span class="br0">[</span><span class="br0">]</span><span class="sy0">,</span> <span class="kw4">int</span> n<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">for</span> <span class="br0">(</span><span class="kw4">int</span> i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;=</span> n<span class="sy0">-</span><span class="nu0">1</span><span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">)</span> cout <span class="sy0">&lt;&lt;</span> p<span class="br0">[</span>i<span class="br0">]</span> <span class="sy0">&lt;&lt;</span> <span class="st0">" "</span><span class="sy0">;</span> cout <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span> <span class="br0">}</span> <span class="kw4">void</span> func1<span class="br0">(</span><span class="kw4">int</span> n<span class="br0">)</span> <span class="br0">{</span> <span class="kw4">int</span> p<span class="br0">[</span>n<span class="br0">]</span><span class="sy0">;</span> <span class="kw4">int</span> k <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> p<span class="br0">[</span>k<span class="br0">]</span> <span class="sy0">=</span> n<span class="sy0">;</span> <span class="kw1">while</span> <span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span> <span class="br0">{</span> printArray<span class="br0">(</span>p<span class="sy0">,</span> k<span class="sy0">+</span><span class="nu0">1</span><span class="br0">)</span><span class="sy0">;</span> <span class="kw4">int</span> rem_val <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="kw1">while</span> <span class="br0">(</span>k <span class="sy0">&gt;=</span> <span class="nu0">0</span> <span class="sy0">&amp;&amp;</span> p<span class="br0">[</span>k<span class="br0">]</span> <span class="sy0">==</span> <span class="nu0">1</span><span class="br0">)</span> <span class="br0">{</span> rem_val <span class="sy0">+=</span> p<span class="br0">[</span>k<span class="br0">]</span><span class="sy0">;</span> k<span class="sy0">--;</span> <span class="br0">}</span> <span class="kw1">if</span> <span class="br0">(</span>k <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="br0">)</span> <span class="kw1">return</span><span class="sy0">;</span> p<span class="br0">[</span>k<span class="br0">]</span><span class="sy0">--;</span> rem_val<span class="sy0">++;</span> <span class="kw1">while</span> <span class="br0">(</span>rem_val <span class="sy0">&gt;</span> p<span class="br0">[</span>k<span class="br0">]</span><span class="br0">)</span> <span class="br0">{</span> p<span class="br0">[</span>k<span class="sy0">+</span><span class="nu0">1</span><span class="br0">]</span> <span class="sy0">=</span> p<span class="br0">[</span>k<span class="br0">]</span><span class="sy0">;</span> rem_val <span class="sy0">=</span> rem_val <span class="sy0">-</span> p<span class="br0">[</span>k<span class="br0">]</span><span class="sy0">;</span> k<span class="sy0">++;</span> <span class="br0">}</span> p<span class="br0">[</span>k<span class="sy0">+</span><span class="nu0">1</span><span class="br0">]</span> <span class="sy0">=</span> rem_val<span class="sy0">;</span> k<span class="sy0">++;</span> <span class="br0">}</span> <span class="br0">}</span> &nbsp; <span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw4">int</span> n<span class="sy0">;</span> cin<span class="sy0">&gt;&gt;</span>n<span class="sy0">;</span> func1<span class="br0">(</span>n<span class="br0">)</span><span class="sy0">;</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="br0">}</span></pre></div></div></div></div></div></div>
Options:
greedy approach
dynamic programming
recursion(divide and conquer)
backtracking
Answer: D