<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Vamsi Ambati's Blog]]></title><description><![CDATA[Vamsi Ambati's Blog]]></description><link>https://tech.vamsiambati.com</link><generator>RSS for Node</generator><lastBuildDate>Sun, 31 May 2026 14:03:25 GMT</lastBuildDate><atom:link href="https://tech.vamsiambati.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Best Time to Buy and Sell Stocks - Finding Max Profit]]></title><description><![CDATA[Problem Description
Say you have an array, A, for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find ...]]></description><link>https://tech.vamsiambati.com/best-time-to-buy-and-sell-stocks-finding-max-profit</link><guid isPermaLink="true">https://tech.vamsiambati.com/best-time-to-buy-and-sell-stocks-finding-max-profit</guid><category><![CDATA[Carry Forward]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[DSA]]></category><dc:creator><![CDATA[Vamsi Ambati]]></dc:creator><pubDate>Fri, 01 Dec 2023 12:00:54 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-problem-description">Problem Description</h3>
<p>Say you have an array, <strong>A</strong>, for which the <strong>i</strong><sup>th</sup> element is the price of a given stock on day <strong>i</strong>.</p>
<p>If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.</p>
<p>Return the <strong>maximum</strong> possible profit.</p>
<h3 id="heading-problem-constraints"><strong>Problem Constraints</strong></h3>
<p>0 &lt;= <strong>A.length</strong>&lt;= 700000<br />1 &lt;= <strong>A[i]</strong> &lt;= 10<sup>7</sup></p>
<h3 id="heading-example-input-output"><strong>Example Input / Output</strong></h3>
<p>Input 1:<br />A = [1, 2]<br />Output:<br />1</p>
<p>Input 1:<br />A = [1, 4, 5, 2, 4]<br />Output:<br />4</p>
<hr />
<p>Let us assume, you are buying the stock on i<sup>th</sup> day. When would you like to sell it?<br />The answer is, we would like to sell when price of the stock is highest on or after i<sup>th</sup> day.<br />That means, we need to find the maximum element on the right hand side.</p>
<p>So, for every i<sup>th</sup> index find maximum on right side and calculate the profit. Maximum profit will be the answer.</p>
<h3 id="heading-solution-approach">Solution Approach</h3>
<p>If you buy your stock on day i, you’d obviously want to sell it on the day its price is maximum after that day.<br />Now this part can be done as:</p>
<ul>
<li><p>Start processing entries from the end maintaining a maximum till start. and do the same from start maintaining the minimum till end Constant additional space requirement.</p>
</li>
<li><p>Store the values as in the same array like [min,max]</p>
</li>
<li><p>Run another loop on the modified array to find the max profit</p>
</li>
</ul>
<pre><code class="lang-javascript">maxProfit : <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">A</span>)</span>{
        <span class="hljs-keyword">let</span> profit = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">let</span> max = <span class="hljs-built_in">Number</span>.NEGATIVE_INFINITY;
        <span class="hljs-keyword">let</span> min = <span class="hljs-built_in">Number</span>.POSITIVE_INFINITY;

        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=A.length<span class="hljs-number">-1</span>;i&gt;=<span class="hljs-number">0</span>;i--){
            max = <span class="hljs-built_in">Math</span>.max(max,A[i])
           A[i] = [A[i],max]
        }
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;A.length;i++) {
            min = <span class="hljs-built_in">Math</span>.min(min,A[i][<span class="hljs-number">0</span>]);
            A[i][<span class="hljs-number">0</span>] = min
        }

        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;A.length;i++) {
            profit = <span class="hljs-built_in">Math</span>.max(A[i][<span class="hljs-number">1</span>]-A[i][<span class="hljs-number">0</span>],profit)
        }
        <span class="hljs-keyword">return</span> profit
    }
</code></pre>
]]></content:encoded></item></channel></rss>