<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Greedy on Krishnatejaswi S</title>
    <link>https://KTS-o7.github.io/bear/tags/greedy/</link>
    <description>Recent content in Greedy on Krishnatejaswi S</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <managingEditor>shentharkrishnatejaswi@gmail.com (Krishnatejaswi S)</managingEditor>
    <webMaster>shentharkrishnatejaswi@gmail.com (Krishnatejaswi S)</webMaster>
    <copyright>Krishnatejaswi S</copyright>
    <lastBuildDate>Thu, 18 Sep 2025 22:37:39 +0530</lastBuildDate>
    <atom:link href="https://KTS-o7.github.io/bear/tags/greedy/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Increasing Array</title>
      <link>https://KTS-o7.github.io/bear/notes/increasing_array/</link>
      <pubDate>Thu, 18 Sep 2025 22:37:39 +0530</pubDate><author>shentharkrishnatejaswi@gmail.com (Krishnatejaswi S)</author>
      <guid>https://KTS-o7.github.io/bear/notes/increasing_array/</guid>
      <description>&lt;p&gt;Link to the problem : &lt;a href=&#34;https://cses.fi/problemset/task/1094&#34;&gt;Increasing Array&lt;/a&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;intuition&#34;&gt;Intuition&lt;/h2&gt;&#xA;&lt;p&gt;The idea is to make an array increasing by making the current element equal to or greater than the previous element.&#xA;and in each step we can increase the value of a single element by 1.&lt;/p&gt;&#xA;&lt;p&gt;That means for &lt;code&gt;i&lt;/code&gt;th element which is less than the &lt;code&gt;i-1&lt;/code&gt;th element, we need &lt;code&gt;elem[i-1] - elem[i]&lt;/code&gt; steps to make it equal to or greater than the &lt;code&gt;i-1&lt;/code&gt;th element.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Link to the problem : <a href="https://cses.fi/problemset/task/1094">Increasing Array</a></p>
<h2 id="intuition">Intuition</h2>
<p>The idea is to make an array increasing by making the current element equal to or greater than the previous element.
and in each step we can increase the value of a single element by 1.</p>
<p>That means for <code>i</code>th element which is less than the <code>i-1</code>th element, we need <code>elem[i-1] - elem[i]</code> steps to make it equal to or greater than the <code>i-1</code>th element.</p>
<p>So we can come up with the following algorithm:</p>
<ul>
<li>Loop through the array</li>
<li>If the current element is less than the previous element, we need to increase the current element to the previous element.</li>
<li>Increment the count by the difference between the previous element and the current element.</li>
<li>Update the current element to the previous element.</li>
</ul>
<p>At the end of the loop, we will have the minimum number of steps to make the array increasing.</p>
<h2 id="solution">Solution</h2>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln"> 1</span><span class="cl"><span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
</span></span><span class="line"><span class="ln"> 2</span><span class="cl">    <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">input</span><span class="p">())</span>
</span></span><span class="line"><span class="ln"> 3</span><span class="cl">    <span class="n">nums</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">input</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()))</span>
</span></span><span class="line"><span class="ln"> 4</span><span class="cl">    <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
</span></span><span class="line"><span class="ln"> 5</span><span class="cl">    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span>
</span></span><span class="line"><span class="ln"> 6</span><span class="cl">        <span class="k">if</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]:</span>
</span></span><span class="line"><span class="ln"> 7</span><span class="cl">            <span class="n">count</span> <span class="o">+=</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span>
</span></span><span class="line"><span class="ln"> 8</span><span class="cl">            <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
</span></span><span class="line"><span class="ln"> 9</span><span class="cl">    <span class="nb">print</span><span class="p">(</span><span class="n">count</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">10</span><span class="cl">
</span></span><span class="line"><span class="ln">11</span><span class="cl"><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&#34;__main__&#34;</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">12</span><span class="cl">    <span class="n">main</span><span class="p">()</span></span></span></code></pre></div><h3 id="mathematical-proof">Mathematical Proof</h3>
<p><strong>Theorem:</strong> The greedy algorithm that ensures each element is at least as large as its predecessor gives the minimum total cost to make the array non-decreasing.</p>
<p><strong>Proof by Induction:</strong></p>
<p><strong>Base Cases:</strong></p>
<ul>
<li>For an array of size 1: The cost is 0, which is trivially optimal.</li>
<li>For an array of size 2: If <code>nums[0] ≤ nums[1]</code>, cost is 0. If <code>nums[0] &gt; nums[1]</code>, we must increment <code>nums[1]</code> by <code>nums[0] - nums[1]</code> to make it ≥ <code>nums[0]</code>. This is optimal since we must satisfy <code>nums[1] ≥ nums[0]</code>.</li>
</ul>
<p><strong>Inductive Hypothesis:</strong> Assume the greedy algorithm is optimal for arrays of size ≤ k.</p>
<p><strong>Inductive Step:</strong> Consider an array of size k+1. Let the array be <code>a[0], a[1], ..., a[k]</code>.</p>
<p>Apply the greedy algorithm:</p>
<ol>
<li>Process the first k elements optimally (by inductive hypothesis)</li>
<li>For the last element <code>a[k]</code>, if <code>a[k-1] ≤ a[k]</code>, no change needed (cost 0)</li>
<li>If <code>a[k-1] &gt; a[k]</code>, increment <code>a[k]</code> to <code>a[k-1]</code>, costing <code>a[k-1] - a[k]</code></li>
</ol>
<p><strong>Why this is optimal:</strong> Any optimal solution must satisfy <code>a'[i] ≥ a'[i-1]</code> for all i, where <code>a'</code> is the final array.</p>
<p>For the prefix <code>a'[0] ... a'[k-1]</code>, the inductive hypothesis says our greedy choices give the minimum cost.</p>
<p>For the last pair <code>a'[k-1]</code> and <code>a'[k]</code>:</p>
<ul>
<li>We must have <code>a'[k] ≥ a'[k-1]</code></li>
<li>The minimum cost to achieve this is exactly <code>max(0, a'[k-1] - a'[k])</code></li>
<li>Our greedy algorithm achieves exactly this minimum cost</li>
</ul>
<p><strong>Key insight:</strong> The greedy choice doesn&rsquo;t affect the optimality of the prefix because:</p>
<ol>
<li>The prefix constraints are already satisfied optimally</li>
<li>Setting <code>a[k] = max(a[k], a[k-1])</code> only affects the relationship between a[k-1] and a[k]</li>
<li>This choice is independent of future elements since there are none</li>
</ol>
<p><strong>Proof by Contradiction :</strong>
Suppose there exists an optimal solution with lower total cost than the greedy algorithm.
Let the greedy algorithm produce array <code>g</code> with cost <code>C_g</code>.
Let the optimal solution produce array <code>o</code> with cost <code>C_o &lt; C_g</code>.</p>
<p>Since both <code>g</code> and <code>o</code> must satisfy <code>g[i] ≥ g[i-1]</code> and <code>o[i] ≥ o[i-1]</code> for all i:</p>
<ul>
<li>At the first position where <code>g</code> and <code>o</code> differ, the optimal solution must have made a different choice</li>
<li>But making a different choice would either violate the non-decreasing constraint or require more operations later, contradicting optimality</li>
</ul>
<p>Therefore, the greedy algorithm is optimal.</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
