<?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 Dinakar on Medium]]></title>
        <description><![CDATA[Stories by Dinakar on Medium]]></description>
        <link>https://medium.com/@dinakar17?source=rss-367199783266------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*J0rxxoHKUHZumhE0QzqdXA.png</url>
            <title>Stories by Dinakar on Medium</title>
            <link>https://medium.com/@dinakar17?source=rss-367199783266------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:20:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dinakar17/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[[It’s 2049] How to Differentiate AI scriptures from Human scriptures]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">AI has become so advanced that we can write and speak like humans. People are having trust issues to believe the content they see online&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@dinakar17/its-2049-how-to-differentiate-ai-scriptures-from-human-scriptures-e901ffd9538a?source=rss-367199783266------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@dinakar17/its-2049-how-to-differentiate-ai-scriptures-from-human-scriptures-e901ffd9538a?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/e901ffd9538a</guid>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Sun, 21 Sep 2025 09:32:20 GMT</pubDate>
            <atom:updated>2025-09-21T09:32:20.262Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[This company reached $100M in revenue in just under 4 months]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@dinakar17/this-company-reached-100m-in-revenue-in-just-under-4-months-8b7cf4b22026?source=rss-367199783266------2"><img src="https://cdn-images-1.medium.com/max/1456/0*7oqJom7a68RcVjiV.png" width="1456"></a></p><p class="medium-feed-snippet">It&#x2019;s not Lovable, Cluely, or Cursor we&#x2019;re talking about</p><p class="medium-feed-link"><a href="https://medium.com/@dinakar17/this-company-reached-100m-in-revenue-in-just-under-4-months-8b7cf4b22026?source=rss-367199783266------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@dinakar17/this-company-reached-100m-in-revenue-in-just-under-4-months-8b7cf4b22026?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b7cf4b22026</guid>
            <category><![CDATA[solana-network]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[ycombinator]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Wed, 10 Sep 2025 12:01:34 GMT</pubDate>
            <atom:updated>2025-09-10T12:01:34.055Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Where could be the next revolution lie in? This should be your long-term investment]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@dinakar17/where-could-be-the-next-revolution-lie-in-this-should-be-your-long-term-investment-62f92d2759d9?source=rss-367199783266------2"><img src="https://cdn-images-1.medium.com/max/1280/0*WuyufQV-PR0nAM1f.jpeg" width="1280"></a></p><p class="medium-feed-snippet">AI is going to stay just like the Internet. But what could be the next breakthrough that brings progress in this world?</p><p class="medium-feed-link"><a href="https://medium.com/@dinakar17/where-could-be-the-next-revolution-lie-in-this-should-be-your-long-term-investment-62f92d2759d9?source=rss-367199783266------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@dinakar17/where-could-be-the-next-revolution-lie-in-this-should-be-your-long-term-investment-62f92d2759d9?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/62f92d2759d9</guid>
            <category><![CDATA[ar-vr]]></category>
            <category><![CDATA[fusion-energy]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[edge-computing]]></category>
            <category><![CDATA[robotics]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Wed, 10 Sep 2025 06:34:43 GMT</pubDate>
            <atom:updated>2025-09-10T06:34:43.047Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[OpenAI acquires data analytics platform for $1.1B, Atlassian enters into Agentic Browser war]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@dinakar17/openai-acquires-data-analytics-platform-for-1-1b-atlassian-enters-into-agentic-browser-war-6501be7dd08e?source=rss-367199783266------2"><img src="https://cdn-images-1.medium.com/max/1456/0*pyvSIkPVTKmX5LkY.png" width="1456"></a></p><p class="medium-feed-snippet">OpenAI acquires Statsig for a staggering $1.1B. Atlassian bets big on agentic browser future as it acquires the browser company for&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@dinakar17/openai-acquires-data-analytics-platform-for-1-1b-atlassian-enters-into-agentic-browser-war-6501be7dd08e?source=rss-367199783266------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@dinakar17/openai-acquires-data-analytics-platform-for-1-1b-atlassian-enters-into-agentic-browser-war-6501be7dd08e?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/6501be7dd08e</guid>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[atlassian]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Mon, 08 Sep 2025 13:26:51 GMT</pubDate>
            <atom:updated>2025-09-08T13:26:51.538Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[The AI Gold Rush Just Hit Its First $1.5B]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@dinakar17/the-ai-gold-rush-just-hit-its-first-1-5b-8e0cde548836?source=rss-367199783266------2"><img src="https://cdn-images-1.medium.com/max/2600/1*AoF1gB_s1mg89u0HUAjuRQ.png" width="2816"></a></p><p class="medium-feed-snippet">Why this landmark copyright settlement might be the &#x201C;Napster moment&#x201D; that reshapes how AI companies handle creative content&#x200A;&#x2014;&#x200A;and what it&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@dinakar17/the-ai-gold-rush-just-hit-its-first-1-5b-8e0cde548836?source=rss-367199783266------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@dinakar17/the-ai-gold-rush-just-hit-its-first-1-5b-8e0cde548836?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e0cde548836</guid>
            <category><![CDATA[anthropic-claude]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[openai]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Mon, 08 Sep 2025 04:37:21 GMT</pubDate>
            <atom:updated>2025-09-08T04:37:21.571Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[The art of visualization is a kick-ass]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@dinakar17/the-art-of-visualization-is-a-kick-ass-9c3ce9b165bf?source=rss-367199783266------2"><img src="https://cdn-images-1.medium.com/max/1920/1*qEliCbdP7YiXfx2AyItxYQ.jpeg" width="1920"></a></p><p class="medium-feed-snippet">How can we understand a complex topic (how GPT works, for example) intuitively?</p><p class="medium-feed-link"><a href="https://medium.com/@dinakar17/the-art-of-visualization-is-a-kick-ass-9c3ce9b165bf?source=rss-367199783266------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@dinakar17/the-art-of-visualization-is-a-kick-ass-9c3ce9b165bf?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c3ce9b165bf</guid>
            <category><![CDATA[feynman-technique]]></category>
            <category><![CDATA[gpt-5]]></category>
            <category><![CDATA[visualization]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Sat, 09 Aug 2025 06:34:53 GMT</pubDate>
            <atom:updated>2025-08-09T06:36:42.468Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Time to have ExpertTalk instead of AstroTalk]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">Making decisions with the right information in mind is crucial for humans</p><p class="medium-feed-link"><a href="https://medium.com/@dinakar17/time-to-have-experttalk-instead-of-astrotalk-3b9583bb6c95?source=rss-367199783266------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@dinakar17/time-to-have-experttalk-instead-of-astrotalk-3b9583bb6c95?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b9583bb6c95</guid>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[astronomy]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Tue, 05 Aug 2025 08:39:42 GMT</pubDate>
            <atom:updated>2025-08-05T08:39:42.818Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Let’s solve coding problems using the First Principles Approach: Three Sum (Problem 6)]]></title>
            <link>https://medium.com/@dinakar17/lets-solve-coding-problems-using-the-first-principles-approach-three-sum-problem-6-75385ff3c47d?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/75385ff3c47d</guid>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[dsa-courses]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Thu, 02 Jan 2025 18:36:57 GMT</pubDate>
            <atom:updated>2025-01-02T18:39:01.848Z</atom:updated>
            <content:encoded><![CDATA[<p>This problem was asked by Microsoft.</p><p>Given an array of numbers and a number k, determine if there are three entries in the array which add up to the specified number k. For example, given [20, 303, 3, 4, 25] and k = 49, return true as 20 + 4 + 25 = 49.</p><p>What could be the naive approach to solving this problem?</p><h4>Approach #1</h4><ol><li>For every element, traverse through all the <strong>subsequent </strong>elements, and for every subsequent element traverse their subsequent elements respectively.</li><li>Find the triplet that satisfies the given condition.</li></ol><pre>#include&lt;bits/stdc++.h&gt;<br><br>using namespace std;<br>bool threeSum(vector &lt; int &gt; &amp; nums, int target) {<br>  int n = nums.size();<br>  for (int i = 0; i &lt; n; i++) {<br>    for (int j = i + 1; j &lt; n; j++) {<br>      for (int k = j + 1; k &lt; n; k++) {<br>        if (nums[i] + nums[j] + nums[k] == target) return true;<br>      }<br>    }<br>  }<br>  return false;<br>}<br>int main() {<br>  vector &lt; int &gt; nums {<br>    20,<br>    303,<br>    3,<br>    4,<br>    25<br>  };<br>  int target = 49;<br>  cout &lt;&lt; threeSum(nums, target);<br>  return 0;<br>}<br><br>// Time Complexity: O(n³)<br>// Space Complexity: O(n)</pre><h4>Optimization #1</h4><p>Traversing through the array for every element and from there the problem is a two-sum problem. Whatever the optimizations we can apply to a <a href="https://medium.com/gitconnected/lets-solve-coding-problems-using-the-first-principles-approach-two-sum-part-1-705a7c38298f">two-sum</a> we can use them here.</p><p>What are the optimizations that one can do to a <a href="https://medium.com/gitconnected/lets-solve-coding-problems-using-the-first-principles-approach-two-sum-part-1-705a7c38298f">2-sum</a> problem?</p><ol><li>Sort the array and use the two-pointers approach</li></ol><pre>bool threeSum(vector &lt; int &gt; &amp; nums, int target) {<br>  int n = nums.size();<br>  sort(nums.begin(), nums.end());<br>  for (int i = 0; i &lt; n; i++) {<br>    int j = i + 1, k = n - 1;<br>    while (j &lt; k) {<br>      if (nums[i] + nums[j] + nums[k] == target) return true;<br>      else if (nums[i] + nums[j] + nums[k] &gt; target) k--;<br>      else j++;<br>    }<br>  }<br>  return false;<br>}<br><br>// Time Complexity: O(n^2) (Since O(nlogn + n^2) = O(n^2))<br>// Space Complexity: O(1)</pre><p>2. Use an unordered_map to find the second pair in the array.</p><pre>bool threeSum(vector &lt; int &gt; &amp; nums, int k) {<br>  int n = nums.size();<br>  for (int i = 0; i &lt; n; i++) {<br>    unordered_map &lt; int, int &gt; m;<br>    for (int j = i + 1; j &lt; n; j++) {<br>      if (m.find(k - nums[j] - nums[i]) != m.end()) {<br>        cout &lt;&lt; nums[i] &lt;&lt; &quot; &quot; &lt;&lt; nums[j] &lt;&lt; &quot; &quot; &lt;&lt; nums[m[k - nums[j] - nums[i]]] &lt;&lt; endl;<br>        return true;<br>      }<br>      m[nums[j]] = j;<br>    }<br>  }<br>  return false;<br>}<br><br>// Time Complexity: O(n^2)<br>// Space Complexity: O(n)</pre><p>If you’ve experienced solving two-sum in all the possible ways then you would’ve understood the above approach and optimizations without confusion. However, the following problem is a challenging one.</p><p>Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.</p><p>Notice that the solution set <strong>must not contain duplicate triplets (</strong><a href="https://leetcode.com/problems/3sum/">Source</a>)</p><h4>Approach #2</h4><ol><li>Follow Approach #1</li><li>Remove the duplicates from the answer.</li></ol><pre>class Solution {<br>public:<br>    bool isDuplicate(vector&lt;vector&lt;int&gt;&gt; ans, vector&lt;int&gt; nums){<br>        for(int i = 0; i &lt; ans.size(); i++) if(ans[i] == nums) return true;<br>        return false;<br>    }<br>    vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) {<br>        int n = nums.size();<br>        vector&lt;vector&lt;int&gt;&gt; ans;<br><br>        for(int i = 0; i &lt; n; i++){<br>            for(int j = i+1; j &lt; n; j++){<br>                for(int k = j+1; k &lt; n; k++){<br>                    if(nums[i] + nums[j] + nums[k] == 0){<br>                        if(!isDuplicate(ans, {nums[i], nums[j], nums[k]}))<br>                        ans.push_back({nums[i], nums[j], nums[k]});<br>                    }<br>                }<br>            }<br>        }<br>        return ans;<br>    }<br>};<br><br>// This doesn&#39;t work</pre><p>Using the above solution we cannot solve the problem. Do you know why? Just think about it for a while.</p><p>We cannot compare the vectors containing the same elements but in an improper order. How can we put them in a proper order? Add sort(nums.begin(), nums.end() at the beginning.</p><p>Time Complexity: O(n⁴) — three nested loops O(n³) and for each triplet, isDuplicate function takes O(n) to check all previous triplets</p><p>Space Complexity: O(n) (For storing the answer for deduplication purposes)</p><h4>Optimization #2</h4><p>How in the hell can we optimize this solution?</p><p>Removing duplicates from the array removes a lot of potential solutions. So we simply cannot remove duplicates.</p><p>Sorting doesn’t work. Why? Maybe sorting does work if implemented in the right way. Without sorting I feel there’s no way to handle duplicates elegantly. After all, we used the sorting approach ( Approach #2 ) to get rid of duplicates.</p><p>Once sorted I think we may need to consider only one of the multiple elements for the initial loop. No, it doesn’t work. This essentially means you’re not considering the duplicate elements.</p><p>Right now I’m struggling with the inclusion of the duplicates in my answer. No matter the optimized approach (two-pointers and unordered map mentioned earlier) I use, the duplicates are still prevalent and I find no way to get rid of them other than scanning for the existing ones in the main answer.</p><p>Let’s think about this problem from a mathematical perspective. I need to find three numbers whose sum is equal to zero. Let’s say the three numbers are represented as num1, num2, and num3.</p><p>num1 + num2 + num3 = 0</p><p>Either two of them can be equal but not all of them can be of equal value.</p><pre>class Solution {<br>public:<br>    vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) {<br>        int n = nums.size();<br>        sort(nums.begin(), nums.end());<br>        vector&lt;vector&lt;int&gt;&gt; ans;<br>        int i = 0;<br>        while (i &lt; n) {<br>            if (i != 0 &amp;&amp; nums[i] == nums[i - 1])<br>                i++;<br>            else {<br>                int j = i + 1, k = n - 1;<br>                while (j &lt; k &amp;&amp; j &lt; n) {<br>                    if (nums[i] + nums[j] + nums[k] == 0) {<br>                        ans.push_back({nums[i], nums[j], nums[k]});<br>                        j++;<br>                        k--;<br>                        while (j &lt; n - 1 &amp;&amp; nums[j] == nums[j - 1])<br>                            j++;<br>                        while (k &gt; 0 &amp;&amp; nums[k] == nums[k + 1])<br>                            k--;<br>                    } else if (nums[i] + nums[j] + nums[k] &gt; 0)<br>                        k--;<br>                    else<br>                        j++;<br>                }<br>            }<br>            i++;<br>        }<br>        return ans;<br>    }<br>};</pre><p>I played with the following example by arranging it in a sorted order to come up with the above approach and also with the help of the two-pointer method optimization I implemented previously.</p><pre>Example 1:<br><br>Input: nums = [-1,0,1,2,-1,-4]<br>Output: [[-1,-1,2],[-1,0,1]]</pre><p>Seems like this doesn’t work as well. But this is an entry point for us to solve the problem in the first place and consolidate the idea that it is indeed possible to avoid duplicates without the need to filter the duplicates by including them in the answer.</p><p>We just need to focus on incrementing the pointers in the right way. Increment the pointers in such a way that we can avoid duplicates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N06cHyVbzlSCmVHMKmjYdg.jpeg" /><figcaption>Ways to avoid duplicates in a Three Sum Problem</figcaption></figure><p>Now can you go through the previous solution and figure out where I have made the error?</p><p>As it turned out I incremented the pointer i way too much than required. Here’s the correct approach.</p><pre>class Solution {<br>public:<br>    vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) {<br>        int n = nums.size();<br>        sort(nums.begin(), nums.end());<br>        vector&lt;vector&lt;int&gt;&gt; ans;<br>        int i = 0;<br>        while (i &lt; n) {<br>            if (i != 0 &amp;&amp; nums[i] == nums[i - 1]) {<br>              // Eat 5-star. Do nothing.<br>            } else {<br>                int j = i + 1, k = n - 1;<br>                while (j &lt; k &amp;&amp; j &lt; n) {<br>                    if (nums[i] + nums[j] + nums[k] == 0) {<br>                        ans.push_back({nums[i], nums[j], nums[k]});<br>                        j++;<br>                        k--;<br>                        while (j &lt; n &amp;&amp; nums[j] == nums[j - 1])<br>                            j++;<br>                        while (k &gt;= 0 &amp;&amp; nums[k] == nums[k + 1])<br>                            k--;<br>                    } else if (nums[i] + nums[j] + nums[k] &gt; 0)<br>                        k--;<br>                    else<br>                        j++;<br>                }<br>            }<br>            i++;<br>        }<br>        return ans;<br>    }<br>};<br><br>// Time Complexity: O(n^2) - O(nlogn) for storing the array + O(n^2) for traversing the array twice<br>// Space Complexity: O(1) (Excluding the space for storing the answer)</pre><p>That’s it from this article. I hope you learned something useful. See you in another. Don’t forget to follow and clap to see similar informative articles in your feed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75385ff3c47d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s solve coding problems using the First Principles Approach: Count Inversions (Problem 5)]]></title>
            <link>https://levelup.gitconnected.com/lets-solve-coding-problems-using-the-first-principles-approach-count-inversions-problem-5-99b13e00bbb1?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/99b13e00bbb1</guid>
            <category><![CDATA[dsa-courses]]></category>
            <category><![CDATA[merge-sort]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Thu, 02 Jan 2025 18:33:17 GMT</pubDate>
            <atom:updated>2025-06-04T09:11:13.128Z</atom:updated>
            <content:encoded><![CDATA[<p>This problem was asked by Google.</p><p>We can determine how “out of order” an array A is by counting the number of inversions it has. Two elements A[i] and A[j] form an inversion if A[i] &gt; A[j] but i &lt; j. That is, a smaller element appears after a larger element. (edit)</p><p>Given an array, count the number of inversions it has. Do this faster than O(N²) time.</p><p>You may assume each element in the array is distinct.</p><p>For example, a sorted list has zero inversions. The array [2, 4, 1, 3, 5] has three inversions: (2, 1), (4, 1), and (4, 3). The array [5, 4, 3, 2, 1] has ten inversions: every distinct pair forms an inversion.</p><h4>Approach</h4><p>If you’re familiar with optimized sorting algorithms for an array this problem would have an opening for you to think through.</p><p>I’m particularly talking about the MergeSort Algorithm. If you’ve hand-drawn the logic of the Merge Sort Algorithm then you would find your mind doing some serious thinking regarding this problem.</p><p>Let’s implement the MergeSort Algorithm by hand and then through code.</p><p>What’s the logic behind the MergeSort algorithm?</p><ol><li>Divide the array into two halves. [picture here]</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*IcNnby-s_KF-v1K1F8fUCA.jpeg" /><figcaption>Figure 1</figcaption></figure><p>2. Repeat the process until you have just one element in the array and nothing to split.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*ib6-J0x319eUQfnVixXYNQ.jpeg" /><figcaption>Figure 2</figcaption></figure><p>3. Merged the two halves in a sorted fashion by comparing left and right half elements in a linear fashion and rewriting the parent node accordingly.</p><p>4. Repeat the process from bottom to top which essentially sorts your array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J65NDjXbJyhr0USshsPSqg.gif" /><figcaption>Figure 3: MergeSort Algorithm HandDrawn logic (No changes to the halves that are already sorted)</figcaption></figure><p>If you want to know how you can draw and write recursive algorithms with ease refer to the following blog.</p><p><a href="https://levelup.gitconnected.com/visualize-write-recursive-algorithms-with-ease-f505f63aa987">Visualize/Write Recursive Algorithms with Ease</a></p><p>It’s time to write the algorithm.</p><pre>#include&lt;bits/stdc++.h&gt;<br>using namespace std;<br><br>void mergeSort(vector&lt;int&gt;&amp; nums, int left, int right){<br>  // base case (can&#39;t divide one single element)<br>  if(left &gt;= right) return;<br>  <br>  int mid = (left + right) / 2;<br>  <br>  mergeSort(nums, left, mid);<br>  mergeSort(nums, mid+1, right);<br>  <br>  // merge left half and the right half<br>  vector&lt;int&gt; leftArr; // for storing the left half<br>  vector&lt;int&gt; rightArr; // for storing the right half<br>  vector&lt;int&gt; mergedArr; // for storing the merged left and right half in a sorted fashion<br>  <br>  for(int i = left; i &lt;= mid; i++) leftArr.push_back(nums[i]);<br>  for(int i = mid+1; i &lt;= right; i++) rightArr.push_back(nums[i]);<br>  <br>  // compare the leftArr and rightArr elements<br>  int i = 0, j = 0;<br>  <br>  while(i &lt; leftArr.size() &amp;&amp; j &lt; rightArr.size()){<br>    if(leftArr[i] &gt; rightArr[j]){<br>      // This is an inversion<br>      mergedArr.push_back(rightArr[j]);<br>      j++;<br>    } else{<br>      // already sorted<br>      mergedArr.push_back(leftArr[i]);<br>      i++;<br>    }<br>  }<br>  <br>  // push the left overs<br>  while(i &lt; leftArr.size()){<br>    mergedArr.push_back(leftArr[i]);<br>    i++;<br>  }<br>  <br>  while(j &lt; rightArr.size()){<br>    mergedArr.push_back(rightArr[j]);<br>    j++;<br>  }<br>  <br>  // change the actual nums<br>  for(int i = left; i &lt;= right; i++){<br>    nums[i] = mergedArr[i-left];<br>  }<br>}<br><br>int main(){<br>  vector&lt;int&gt; nums {5, 4, 3, 2, 1};<br>  <br>  for(int i : nums) cout &lt;&lt; i &lt;&lt; &quot; &quot;;<br>  <br>  cout &lt;&lt; endl;<br>  <br>  mergeSort(nums, 0, nums.size()-1);<br>  <br>  for(int i : nums) cout &lt;&lt; i &lt;&lt; &quot; &quot;;<br>  <br>}</pre><p>Time Complexity: O(nlogn) ( logn implies the time taken for the recursive function calls to end and O(n) is the time complexity for our function to execute)</p><p>Space Complexity: O(n) (The space we’ve created for the leftArr, rightArr, and mergedArr )</p><p>Think about how you would count the inversions present in our array according to the steps that the above logic is implemented through.</p><p>Hint: Draw Figure 3 by your hand explaining the logic to yourself aloud.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*qYJf-LkTPXHGxPDEgM7SeA.jpeg" /></figure><p>Get it? The inversion implies when you’re trying to bring elements in the right half over the left half elements.</p><p>So for a given, right half element (rightArr[j]) we need to figure the number of left half elements (leftArr.size() — i)left to be compared to bring the right half element to before them. This accumulates the number of inversions we need.</p><pre>#include&lt;bits/stdc++.h&gt;<br>using namespace std;<br><br>void mergeSort(vector&lt;int&gt;&amp; nums, int left, int right, int&amp; count){<br>  // base case (can&#39;t divide one single element)<br>  if(left &gt;= right) return;<br>  <br>  int mid = (left + right) / 2;<br>  <br>  mergeSort(nums, left, mid, count);<br>  mergeSort(nums, mid+1, right, count);<br>  <br>  // merge left half and the right half<br>  vector&lt;int&gt; leftArr; // for storing the left half<br>  vector&lt;int&gt; rightArr; // for storing the right half<br>  vector&lt;int&gt; mergedArr; // for storing the merged left and right half in a sorted fashion<br>  <br>  for(int i = left; i &lt;= mid; i++) leftArr.push_back(nums[i]);<br>  for(int i = mid+1; i &lt;= right; i++) rightArr.push_back(nums[i]);<br>  <br>  // compare the leftArr and rightArr elements<br>  int i = 0, j = 0;<br>  <br>  while(i &lt; leftArr.size() &amp;&amp; j &lt; rightArr.size()){<br>    if(leftArr[i] &gt; rightArr[j]){<br>      // This is an inversion<br>      mergedArr.push_back(rightArr[j]);<br><br>      count += (leftArr.size() - i);<br>      j++;<br>    } else{<br>      // already sorted<br>      mergedArr.push_back(leftArr[i]);<br>      i++;<br>    }<br>  }<br>  <br>  // push the left overs<br>  while(i &lt; leftArr.size()){<br>    mergedArr.push_back(leftArr[i]);<br>    i++;<br>  }<br>  <br>  while(j &lt; rightArr.size()){<br>    mergedArr.push_back(rightArr[j]);<br>    j++;<br>  }<br>  <br>  // change the actual nums<br>  for(int i = left; i &lt;= right; i++){<br>    nums[i] = mergedArr[i-left];<br>  }<br>}<br><br>int main(){<br>  vector&lt;int&gt; nums {2, 4, 1, 3, 5};<br>  <br>  int count = 0;<br>  <br>  for(int i : nums) cout &lt;&lt; i &lt;&lt; &quot; &quot;;<br>  <br>  cout &lt;&lt; endl;<br>  <br>  mergeSort(nums, 0, nums.size()-1, count);<br>  <br>  cout &lt;&lt; &quot;Number of Inversions: &quot; &lt;&lt; count &lt;&lt; endl;<br>  <br>  for(int i : nums) cout &lt;&lt; i &lt;&lt; &quot; &quot;;<br>  <br>}</pre><p>Time Complexity: O(nlogn)</p><p>Space Complexity: O(n)</p><p>Time and Space Complexity remain the same.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99b13e00bbb1" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/lets-solve-coding-problems-using-the-first-principles-approach-count-inversions-problem-5-99b13e00bbb1">Let’s solve coding problems using the First Principles Approach: Count Inversions (Problem 5)</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s solve coding problems using the First Principles Approach: Markov Chain (Problem 4)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/lets-solve-coding-problems-using-the-first-principles-approach-markov-chain-problem-4-fce39237ed34?source=rss-367199783266------2"><img src="https://cdn-images-1.medium.com/max/600/1*9BCe_z9m8NAAUgLtMmIfDQ.png" width="600"></a></p><p class="medium-feed-snippet">This problem was asked by Google.</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/lets-solve-coding-problems-using-the-first-principles-approach-markov-chain-problem-4-fce39237ed34?source=rss-367199783266------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/lets-solve-coding-problems-using-the-first-principles-approach-markov-chain-problem-4-fce39237ed34?source=rss-367199783266------2</link>
            <guid isPermaLink="false">https://medium.com/p/fce39237ed34</guid>
            <category><![CDATA[dsa-courses]]></category>
            <category><![CDATA[markov-chains]]></category>
            <dc:creator><![CDATA[Dinakar]]></dc:creator>
            <pubDate>Tue, 31 Dec 2024 20:40:16 GMT</pubDate>
            <atom:updated>2024-12-31T20:40:16.711Z</atom:updated>
        </item>
    </channel>
</rss>