<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mozammil Khan on Medium]]></title>
        <description><![CDATA[Stories by Mozammil Khan on Medium]]></description>
        <link>https://medium.com/@saminkhan1?source=rss-4c1c0f8563e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*o7lz0jk7fm7rv4x3lAM9_A.jpeg</url>
            <title>Stories by Mozammil Khan on Medium</title>
            <link>https://medium.com/@saminkhan1?source=rss-4c1c0f8563e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 01:41:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saminkhan1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Best Time to Buy and Sell Stock II]]></title>
            <link>https://medium.com/@saminkhan1/best-time-to-buy-and-sell-stock-ii-e60240731575?source=rss-4c1c0f8563e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e60240731575</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Mozammil Khan]]></dc:creator>
            <pubDate>Wed, 25 Mar 2020 01:25:33 GMT</pubDate>
            <atom:updated>2020-03-25T01:46:25.866Z</atom:updated>
            <content:encoded><![CDATA[<p>Say you have an array for which the <em>i</em>th element is the price of a given stock on day <em>i</em>.</p><p>Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).</p><p><strong>Note:</strong> You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).</p><p>Example 1:</p><pre><strong>Input:</strong> [7,1,5,3,6,4]<br><strong>Output:</strong> 7<br><strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.<br>             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.</pre><p>Example 2:</p><pre><strong>Input:</strong> [1,2,3,4,5]<br><strong>Output:</strong> 4<br><strong>Explanation:</strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.<br>             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are<br>             engaging multiple transactions at the same time. You must sell before buying again.</pre><p>Example 3:</p><pre><strong>Input:</strong> [7,6,4,3,1]<br><strong>Output:</strong> 0<br><strong>Explanation:</strong> In this case, no transaction is done, i.e. max profit = 0.</pre><p>Code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/747dde19bc30006722e9413f30c5d8b7/href">https://medium.com/media/747dde19bc30006722e9413f30c5d8b7/href</a></iframe><p>Key Points:</p><ul><li>Mathematically speaking, the total profit is the sum of the difference between all the “peaks ”and “valleys”</li><li>In the algorithm above, instead of looking for every peak following a valley, we instead add profit of the increasing price in every transaction.</li><li>The difference between the consecutive numbers of the array if the second number is larger than the first one, and at the total sum we obtain will be the maximum profit.</li><li>The example below clarifies what the algorithm is doing. It adds every positive increment and finds the maximum profit.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/995/1*06_bx5wcfPBSALWdUM1MHQ.png" /><figcaption>A + B + C = D</figcaption></figure><p>Complexity Analysis</p><ul><li>Time complexity : O(n) Single pass.</li><li>Space complexity: O(1) Constant space needed.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e60240731575" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Remove Duplicates from Sorted Array]]></title>
            <link>https://medium.com/@saminkhan1/remove-duplicates-from-sorted-array-f2a8b3193d80?source=rss-4c1c0f8563e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2a8b3193d80</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Mozammil Khan]]></dc:creator>
            <pubDate>Tue, 24 Mar 2020 19:34:00 GMT</pubDate>
            <atom:updated>2020-03-25T01:45:45.305Z</atom:updated>
            <content:encoded><![CDATA[<p>Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.</p><p>Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.</p><p>Example 1:</p><pre>Given <em>nums</em> = <strong>[1,1,2]</strong>,</pre><pre>Your function should return length = <strong>2</strong>, with the first two elements of <em>nums</em> being <strong>1</strong> and <strong>2</strong> respectively.</pre><pre>It doesn&#39;t matter what you leave beyond the returned length.</pre><p>Example 2:</p><pre>Given <em>nums</em> = <strong>[0,0,1,1,1,2,2,3,3,4]</strong>,</pre><pre>Your function should return length = <strong>5</strong>, with the first five elements of <em>nums</em> being modified to <strong>0</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, and <strong>4</strong> respectively.</pre><pre>It doesn&#39;t matter what values are set beyond the returned length.</pre><p>Code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae34921b28bc256b7123d5bb84acb9c5/href">https://medium.com/media/ae34921b28bc256b7123d5bb84acb9c5/href</a></iframe><p>Key Points:</p><ul><li>Since the array is sorted, all the duplicates are together.</li><li>We can keep two pointers, i, j where i is the slow runner while j is the fast runner.</li><li>As long as nums[i] = nums[j], we increment j to skip the duplicates</li><li>When we encounter nums[i] != nums[j], the duplicate ended so we must copy its value to nums[i+1]. Therefore</li></ul><p>Complexity analysis</p><ul><li>Time complexity : O(n) Assume that n is the length of array. Each of i and j traverses at most n steps.</li><li>Space complexity : O(1) Constant space needed.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2a8b3193d80" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>