<?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 Mayank Kumar Shaw on Medium]]></title>
        <description><![CDATA[Stories by Mayank Kumar Shaw on Medium]]></description>
        <link>https://medium.com/@mayank141shaw?source=rss-bd1be18dbc3f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yhsJz7KLZwNBpUjO0Q6AdQ.png</url>
            <title>Stories by Mayank Kumar Shaw on Medium</title>
            <link>https://medium.com/@mayank141shaw?source=rss-bd1be18dbc3f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:18:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mayank141shaw/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[DSA Decoded | Day 27: Combination Sum]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-27-combination-sum-88e7f9f471a1?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/88e7f9f471a1</guid>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[backtracking]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Tue, 12 May 2026 06:04:17 GMT</pubDate>
            <atom:updated>2026-05-12T06:04:17.881Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Fzkvp8ieh8C41h0n" /><figcaption>Photo by <a href="https://unsplash.com/@hempcrew?utm_source=medium&amp;utm_medium=referral">HempCrew</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/combination-target-sum/question">Combination Sum - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>You are given an array of <strong>distinct</strong> integers nums and a target integer target. Your task is to return a list of all <strong>unique combinations</strong> of nums where the chosen numbers sum to target.</p><p>The <strong>same</strong> number may be chosen from nums an <strong>unlimited number of times</strong>. Two combinations are the same if the frequency of each of the chosen numbers is the same, otherwise they are different.</p><p>You may return the combinations in <strong>any order</strong> and the order of the numbers in each combination can be in <strong>any order</strong>.</p><p><strong>Example 1:</strong></p><pre>Input:<br>nums = [2,5,6,9]<br>target = 9<br><br>Output: [[2,2,5],[9]]</pre><p><strong>Constraints:</strong></p><ul><li>All elements of nums are <strong>distinct</strong>.</li><li>1 &lt;= nums.length &lt;= 20</li><li>2 &lt;= nums[i] &lt;= 30</li><li>2 &lt;= target &lt;= 30</li></ul><p><strong>Intuition:</strong></p><p>Since all numbers are distinct and can be reused infinitely, to not exhaust we sort the numbers and then, use backtracking to choose an index and reduce the target at every step, until target is 0.</p><p>Every branch is just choosing a value in the step and tracking a path sum.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*M7ZMrJpytf3EKoqqSgl5lA.png" /></figure><p><strong>Solution:</strong></p><pre>class Solution {<br>    public List&lt;List&lt;Integer&gt;&gt; combinationSum(int[] nums, int target) {<br>        Arrays.sort(nums);<br>        List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;();<br>        Stack&lt;Integer&gt; s = new Stack&lt;&gt;();<br>        dfs(0, nums, target, s, res);<br>        return res;<br>    }<br><br>    public void dfs(int i, int[] nums, int target, Stack&lt;Integer&gt; s, List&lt;List&lt;Integer&gt;&gt; res){<br>        if(i &gt;= nums.length || target&lt;0) return;<br><br>        if(target == 0){<br>            res.add(new ArrayList&lt;&gt;(s));<br>            return;<br>        }<br><br>        for(int k=i; k&lt;nums.length; k++){<br>            if( target - nums[k] &gt;= 0 ){<br>                s.push(nums[k]);<br>                dfs(k, nums, target - nums[k], s, res);<br>                s.pop();<br>            }<br>        }<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(2^n) , we traverse all combinations until target is satisfied.</p><p>Space : O(n) , we use stack for path tracking.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88e7f9f471a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 26: Median From Data Stream]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-26-median-from-data-stream-67daf0ca367a?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/67daf0ca367a</guid>
            <category><![CDATA[lists]]></category>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[hard]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Mon, 11 May 2026 06:37:22 GMT</pubDate>
            <atom:updated>2026-05-11T06:37:22.657Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sFPalhm_Yg4D1AQP" /><figcaption>Photo by <a href="https://unsplash.com/@the_bracketeer?utm_source=medium&amp;utm_medium=referral">Hendrik Cornelissen</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/find-median-in-a-data-stream/question">Find Median From Data Stream - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>The <a href="https://en.wikipedia.org/wiki/Median"><strong>median</strong></a> is the middle value in a sorted list of integers. For lists of <em>even</em> length, there is no middle value, so the median is the <a href="https://en.wikipedia.org/wiki/Mean">mean</a> of the two middle values.</p><p>For example:</p><ul><li>For arr = [1,2,3], the median is 2.</li><li>For arr = [1,2], the median is (1 + 2) / 2 = 1.5</li></ul><p>Implement the MedianFinder class:</p><ul><li>MedianFinder() initializes the MedianFinder object.</li><li>void addNum(int num) adds the integer num from the data stream to the data structure.</li><li>double findMedian() returns the median of all elements so far.</li></ul><p><strong>Example 1:</strong></p><pre>Input:<br>[&quot;MedianFinder&quot;, &quot;addNum&quot;, &quot;1&quot;, &quot;findMedian&quot;, &quot;addNum&quot;, &quot;3&quot; &quot;findMedian&quot;, &quot;addNum&quot;, &quot;2&quot;, &quot;findMedian&quot;]<br><br>Output:<br>[null, null, 1.0, null, 2.0, null, 2.0]<br><br>Explanation:<br>MedianFinder medianFinder = new MedianFinder();<br>medianFinder.addNum(1);    // arr = [1]<br>medianFinder.findMedian(); // return 1.0<br>medianFinder.addNum(3);    // arr = [1, 3]<br>medianFinder.findMedian(); // return 2.0<br>medianFinder.addNum(2);    // arr[1, 2, 3]<br>medianFinder.findMedian(); // return 2.0</pre><p><strong>Constraints:</strong></p><ul><li>-100,000 &lt;= num &lt;= 100,000</li><li>findMedian will only be called after adding at least one integer to the data structure.</li></ul><p><strong>Intuition:</strong></p><p>Since a median is always the middle value of the sorted list, for odd length its the middle one and even length, its the average for the middle two.</p><p>Using this logic, we keep the stream always divided in two parts, first contains the sorted first half, and second the other greater half. In such scenario, we need to get the max of first half and minimum of second half. For such a case we can use two heaps, max heap for smaller list and min heap for larger list, which would give use max of smallers and min of larger ones respectively.</p><p>A simple workflow is :</p><ol><li>Keep two heaps, max heap for smaller list, and min heap for larger list.</li><li>By default add incoming values to smaller half first.</li><li>If the difference in size of both list is greater than 1, (first heap is allowed to have 1 more item, for handling odd length cases), then in order to balance the division of list items, we move the max of lower heap, to larger heap.</li><li>Also a case could occur, where the top of first half is larger than top of second half, breaking the sorted order. To fix this simply, swap the heads of two heaps.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g7H83mVnCf_ja7YDtYUWSg.png" /></figure><p><strong>Solution:</strong></p><pre>class MedianFinder {<br>    private Queue&lt;Integer&gt; minHeap, maxHeap;<br>    private int size;<br><br>    public MedianFinder() {<br>        this.minHeap = new PriorityQueue&lt;&gt;((a,b) -&gt; b-a);<br>        this.maxHeap = new PriorityQueue&lt;&gt;((a,b) -&gt; a-b);    <br>    }<br>    <br>    public void addNum(int num) {<br>        this.minHeap.add(num);<br>        if( this.minHeap.size() - this.maxHeap.size() &gt; 1 ){<br>            this.maxHeap.add( this.minHeap.poll() );<br>        }<br>        if( !this.maxHeap.isEmpty() &amp;&amp; this.maxHeap.peek() &lt; this.minHeap.peek() ){<br>            this.minHeap.poll();<br>            this.minHeap.add( this.maxHeap.poll() );<br>            this.maxHeap.add(num);<br>        }<br>    }<br>    <br>    public double findMedian() {<br>        if(this.minHeap.size() &gt; this.maxHeap.size())<br>            return this.minHeap.peek();<br>        else<br>            return ((double)this.minHeap.peek() + this.maxHeap.peek()) / 2;<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(nlogn) , we use priority queues with self sorting.</p><p>Space : O(n) , we use two queues.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67daf0ca367a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 25: Merge K Sorted Linked Lists]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-25-merge-k-sorted-linked-lists-55ee20c14dd0?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/55ee20c14dd0</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[merging]]></category>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[hard]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Tue, 05 May 2026 07:30:33 GMT</pubDate>
            <atom:updated>2026-05-05T07:30:33.609Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*m8q1Z9e6m6L9u3nx" /><figcaption>Photo by <a href="https://unsplash.com/@makeupartist60?utm_source=medium&amp;utm_medium=referral">Susan Kirsch</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/merge-k-sorted-linked-lists/question">Merge K Sorted Linked Lists - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>You are given an array of k linked lists lists, where each list is sorted in ascending order.</p><p>Return the <strong>sorted</strong> linked list that is the result of merging all of the individual linked lists.</p><p><strong>Example 1:</strong></p><pre>Input: lists = [[1,2,4],[1,3,5],[3,6]]<br><br>Output: [1,1,2,3,3,4,5,6]</pre><p><strong>Constraints:</strong></p><ul><li>0 &lt;= lists.length &lt;= 1000</li><li>0 &lt;= lists[i].length &lt;= 100</li><li>-1000 &lt;= lists[i][j] &lt;= 1000</li></ul><p><strong>Intuition:</strong></p><p>A simple workflow is :</p><ol><li>Keep an auxillary listnode that is going to be used as resulting list.</li><li>Merge every list with this auxillary list.</li><li>Merge when node in list2 is ≤ the current node in auxillary node. Else move forward.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*M_7NwIvIO1cQdeZcFP_sLw.png" /></figure><p><strong>Solution:</strong></p><pre>/**<br> * Definition for singly-linked list.<br> * public class ListNode {<br> *     int val;<br> *     ListNode next;<br> *     ListNode() {}<br> *     ListNode(int val) { this.val = val; }<br> *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }<br> * }<br> */<br><br>class Solution {<br>    public ListNode mergeKLists(ListNode[] lists) {<br>        ListNode res = null;<br><br>        for(ListNode head : lists){<br>            res = merge(res, head);<br>        }<br><br>        return res;<br>    }<br><br>    public ListNode merge(ListNode a, ListNode b){<br>        if(a == null) return b;<br>        if(b == null) return a;<br><br>        ListNode prev = null, temp = null;<br>        ListNode t1 = a, t2 = b;<br>        <br>        while(t2 != null &amp;&amp; t1 != null){<br>            if(t2.val &lt;= t1.val){ // insert bw prev and t1<br>                temp = t2.next;<br><br>                if(prev == null){<br>                    prev = t2;<br>                    t2.next = t1;<br>                    a = prev;   // head of main list<br>                }else{<br>                    prev.next = t2;<br>                    t2.next = t1;<br>                    prev = t2;<br>                }<br><br>                t2 = temp;<br>            }else{<br>                prev = t1;<br>                t1 = t1.next;<br>            }<br>        }<br><br>        if(t2 != null){<br>            prev.next = t2;<br>        }<br><br>        return a;<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(n^2) , we do linear merging for all lists.</p><p>Space : O(1) , we use two pointers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55ee20c14dd0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 24: Reorder Linked List]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-24-reorder-linked-list-ae38870ea81a?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae38870ea81a</guid>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[medium]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Sun, 03 May 2026 06:47:08 GMT</pubDate>
            <atom:updated>2026-05-03T06:47:08.111Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NiSlla2bidSF7cOG" /><figcaption>Photo by <a href="https://unsplash.com/@rocinante_11?utm_source=medium&amp;utm_medium=referral">Mick Haupt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/reorder-linked-list/question">Reorder Linked List - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>You are given the head of a singly linked-list.</p><p>The positions of a linked list of length = 7 for example, can intially be represented as:</p><p>[0, 1, 2, 3, 4, 5, 6]</p><p>Reorder the nodes of the linked list to be in the following order:</p><p>[0, 6, 1, 5, 2, 4, 3]</p><p>Notice that in the general case for a list of length = n the nodes are reordered to be in the following order:</p><p>[0, n-1, 1, n-2, 2, n-3, ...]</p><p>You may not modify the values in the list’s nodes, but instead you must reorder the nodes themselves.</p><p><strong>Example 1:</strong></p><pre>Input: head = [2,4,6,8]<br><br>Output: [2,8,4,6]</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= Length of the list &lt;= 1000.</li><li>1 &lt;= Node.val &lt;= 1000</li></ul><p><strong>Intuition:</strong></p><p>A simple workflow is :</p><ol><li>Find the mid node of the list and break the list in two.</li><li>Reverse the second list.</li><li>Merge the list2 in alternating positions in list1</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z68w5oyvhnQ3GMEizDN9Lg.png" /></figure><p><strong>Solution:</strong></p><pre>/**<br> * Definition for singly-linked list.<br> * public class ListNode {<br> *     int val;<br> *     ListNode next;<br> *     ListNode() {}<br> *     ListNode(int val) { this.val = val; }<br> *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }<br> * }<br> */<br><br>class Solution {<br>    public void reorderList(ListNode head) {<br>        if(head == null || head.next == null) return;<br><br>        // partition<br>        ListNode slow = head, fast = head.next;<br>        while(fast!=null &amp;&amp; fast.next!=null){<br>            slow= slow.next;<br>            fast = fast.next.next;<br>        }<br><br>        // reverse the second part<br>        ListNode t2 = reverse(slow.next);<br>        slow.next = null; // broke the chain in two<br><br>        // merge t2 after every node in t1<br>        ListNode t1 = head, temp = null;<br><br>        while(t2 != null){<br>            temp = t2.next;<br>            t2.next = t1.next;<br>            t1.next = t2;<br><br>            t1 = t2.next;<br>            t2 = temp;<br>        }<br><br>    }<br><br>    private ListNode reverse(ListNode head){<br>        if(head == null || head.next == null) return head;<br><br>        ListNode prev = null, cur = head, next = head.next;<br>        while(cur != null){<br>            cur.next = prev;<br>            prev = cur;<br>            cur = next;<br>            if(next != null) next = next.next;<br>        }<br><br>        return prev;<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(n) , we use linear search.</p><p>Space : O(1) , we use two pointers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae38870ea81a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 23: Search in Rotated Sorted Array]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-23-search-in-rotated-sorted-array-d2052f5eabdf?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2052f5eabdf</guid>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[medium]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Fri, 01 May 2026 06:27:48 GMT</pubDate>
            <atom:updated>2026-05-01T06:27:48.795Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uFAzb-hCZY60Fh1W" /><figcaption>Photo by <a href="https://unsplash.com/@rydewerff?utm_source=medium&amp;utm_medium=referral">Ryan DeWerff</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/find-target-in-rotated-sorted-array/question">Search in Rotated Sorted Array - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>You are given an array of length n which was originally sorted in ascending order. It has now been <strong>rotated</strong> between 1 and n times. For example, the array nums = [1,2,3,4,5,6] might become:</p><ul><li>[3,4,5,6,1,2] if it was rotated 4 times.</li><li>[1,2,3,4,5,6] if it was rotated 6 times.</li></ul><p>Given the rotated sorted array nums and an integer target, return the index of target within nums, or -1 if it is not present.</p><p>You may assume all elements in the sorted rotated array nums are <strong>unique</strong>,</p><p>A solution that runs in O(n) time is trivial, can you write an algorithm that runs in O(log n) time?</p><p><strong>Example 1:</strong></p><pre>Input: nums = [3,4,5,6,1,2], target = 1<br><br>Output: 4</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= nums.length &lt;= 1000</li><li>-1000 &lt;= nums[i] &lt;= 1000</li><li>-1000 &lt;= target &lt;= 1000</li><li>All values of nums are <strong>unique</strong>.</li><li>nums is an ascending array that is possibly rotated.</li></ul><p><strong>Intuition:</strong></p><p>Since the array is sorted, we can use binary search to find the break point, i.e, the number that is smaller than the number to its left.</p><p>So we use left pointer as our answer, i,e. when left subarray is sorted we jump l = mid + 1 , else we jump r = mid . We continue this till the condition l&lt;r . By the end of the loop, the nums[l] will be the answer.</p><p>This way we find the pivot, that breaks the array in two sorted lists. Now we simply compare the target and find in which sub list it will probably exist.</p><p>Based on that we shrink our search window, and use binary search to find the target, and if not found return -1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UTcsHpSwPdsq8B0ScyudYw.png" /></figure><p><strong>Solution:</strong></p><pre>class Solution {<br>    public int search(int[] nums, int target) {<br>        if(nums[0] &lt;= nums[nums.length-1]) <br>            return search(nums, 0, nums.length-1, target);<br>        <br>        // pivot for boundary seperation<br>        int l=0, r=nums.length-1, mid=0;<br>        while(l&lt;r){<br>            mid = l + (r-l) / 2;<br><br>            if(nums[mid] &lt; nums[r])<br>                r = mid;<br>            else<br>                l = mid + 1;<br>        } <br><br>        // boundary and target check<br>        if(nums[0] &lt;= target &amp;&amp; target &lt;= nums[l-1])<br>            return search(nums, 0, l-1, target);<br>        else<br>            return search(nums, l, nums.length-1, target);<br>    }<br><br>    public int search(int[] nums, int l, int r, int t){<br>        int mid = 0;<br>        while(l&lt;=r){<br>            mid = l + (r-l) / 2;<br><br>            if(nums[mid] == t)<br>                return mid;<br>            else if(nums[mid] &gt; t)<br>                r = mid - 1;<br>            else <br>                l = mid + 1;<br>        }<br>        return -1;<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(logn) , we use binary search.</p><p>Space : O(1) , we use two pointers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2052f5eabdf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 22: Find Minimum in Rotated Sorted Array]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-22-find-minimum-in-rotated-sorted-array-3044ea49f858?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3044ea49f858</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[medium]]></category>
            <category><![CDATA[binary-search]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Thu, 30 Apr 2026 06:06:13 GMT</pubDate>
            <atom:updated>2026-04-30T06:06:13.660Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*odynsSzBKKlKYkt7" /><figcaption>Photo by <a href="https://unsplash.com/@ssszy?utm_source=medium&amp;utm_medium=referral">Zhiyuan Sun</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/find-minimum-in-rotated-sorted-array/question">Find Minimum in Rotated Sorted Array - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>You are given an array of length n which was originally sorted in ascending order. It has now been <strong>rotated</strong> between 1 and n times. For example, the array nums = [1,2,3,4,5,6] might become:</p><ul><li>[3,4,5,6,1,2] if it was rotated 4 times.</li><li>[1,2,3,4,5,6] if it was rotated 6 times.</li></ul><p>Notice that rotating the array 4 times moves the last four elements of the array to the beginning. Rotating the array 6 times produces the original array.</p><p>Assuming all elements in the rotated sorted array nums are <strong>unique</strong>, return the minimum element of this array.</p><p>A solution that runs in O(n) time is trivial, can you write an algorithm that runs in O(log n) time?</p><p><strong>Example 1:</strong></p><pre>Input: nums = [3,4,5,6,1,2]<br><br>Output: 1</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= nums.length &lt;= 1000</li><li>-1000 &lt;= nums[i] &lt;= 1000</li></ul><p><strong>Intuition:</strong></p><p>Since the array is sorted, we can use binary search to find the break point, i.e, the number that is smaller than the number to its left.</p><p>So we use left pointer as our answer, i,e. when left subarray is sorted we jump l = mid + 1 , else we jump r = mid . We continue this till the condition l&lt;r . By the end of the loop, the nums[l] will be the answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UTcsHpSwPdsq8B0ScyudYw.png" /></figure><p><strong>Solution:</strong></p><pre>class Solution {<br>    public int findMin(int[] nums) {<br>        int n = nums.length;<br>        if(nums[0] &lt; nums[n-1]) return nums[0]; // already sorted<br><br>        int l=0, r=n-1, mid=0;<br>        while(l&lt;r){<br>            mid = l + (r-l)/2;<br>            if(nums[mid] &lt; nums[r])<br>                r = mid;<br>            else <br>                l = mid + 1;<br>        }<br><br>        return nums[l];<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(logn) , we use binary search.</p><p>Space : O(1) , we use two pointers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3044ea49f858" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 21: Minimum Window Substring]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-21-minimum-window-substring-02e6682435cb?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/02e6682435cb</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[sliding-window-algorithm]]></category>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[hard]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Wed, 29 Apr 2026 06:48:48 GMT</pubDate>
            <atom:updated>2026-04-29T06:48:48.296Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5cEdzNc4KcAB_QPd" /><figcaption>Photo by <a href="https://unsplash.com/@i_bekkers?utm_source=medium&amp;utm_medium=referral">Isaiah B</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/minimum-window-with-characters/question">Minimum Window Substring - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>Given two strings s and t, return the shortest <strong>substring</strong> of s such that every character in t, including duplicates, is present in the substring. If such a substring does not exist, return an empty string &quot;&quot;.</p><p>You may assume that the correct output is always unique.</p><p><strong>Example 1:</strong></p><pre>Input: s = &quot;OUZODYXAZV&quot;, t = &quot;XYZ&quot;<br><br>Output: &quot;YXAZ&quot;</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= s.length &lt;= 1000</li><li>1 &lt;= t.length &lt;= 1000</li><li>s and t consist of uppercase and lowercase English letters.</li></ul><p><strong>Intuition:</strong></p><p>We need to maintain a dynamic window whic would contain atleast all the characters of the target, i.e, the freq of the target characters must be greater than or equals in the current window counters.</p><p>We must include every character, expansion , which we check whether it contains atleast all the target character counts. If so, then we shrink by moving the left pointer forward, adn update result by keeping the shortest substring found. We keep shrinking as long as the count satisfies. Once it doesn’t, then we continue expanding again.</p><p>This continues until we reached end of string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*Lnr94B1DLv-0qFe4LpXufQ.png" /></figure><p><strong>Solution:</strong></p><pre>class Solution {<br>    public String minWindow(String s, String t) {<br>        if(t.length() &gt; s.length()) return &quot;&quot;;<br><br>        Map&lt;Character, Integer&gt; target = new HashMap&lt;&gt;();<br>        String res = &quot;&quot;;<br><br>        // map target counts<br>        for(char c : t.toCharArray())<br>            target.put(c, target.getOrDefault(c, 0) + 1);<br><br>        // dynamic window run<br>        int l=0;<br>        Map&lt;Character, Integer&gt; count = new HashMap&lt;&gt;();<br>        for(int r=0; r&lt;s.length(); r++){<br>            // include rth<br>            char cur = s.charAt(r);<br>            count.put(cur, count.getOrDefault(cur, 0) + 1);<br><br>            // shrink till freq is valid<br>            while(l&lt;=r &amp;&amp; isValid(count, target)){<br>                res = ( res.equals(&quot;&quot;) || res.length() &gt; (r-l+1) ) ? <br>                    s.substring(l, r+1) : res; <br>                char cl = s.charAt(l);<br>                count.put(cl, count.get(cl) - 1);<br>                l++;<br>            }<br>        }<br><br>        return res;<br>    }<br><br>    private boolean isValid(Map&lt;Character, Integer&gt; count, Map&lt;Character, Integer&gt; target){<br>        for(Map.Entry&lt;Character, Integer&gt; en : target.entrySet()){<br>            if( count.getOrDefault( en.getKey(), 0 ) &lt; en.getValue() )<br>                return false;<br>        }<br><br>        return true;<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(n) , for every character we possibly do a max freq count.</p><p>Space : O(n) , we use hashmap.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=02e6682435cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 20: Longest Repeating Character Replacement]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-20-longest-repeating-character-replacement-4f0a4f140cd0?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f0a4f140cd0</guid>
            <category><![CDATA[medium]]></category>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Tue, 28 Apr 2026 06:44:06 GMT</pubDate>
            <atom:updated>2026-04-28T06:44:06.157Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hEI7tJhoaIzqVpD3" /><figcaption>Photo by <a href="https://unsplash.com/@polarmermaid?utm_source=medium&amp;utm_medium=referral">Anne Nygård</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/longest-repeating-substring-with-replacement/question">Longest Repeating Character Replacement - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>You are given a string s consisting of only uppercase english characters and an integer k. You can choose up to k characters of the string and replace them with any other uppercase English character.</p><p>After performing at most k replacements, return the length of the longest substring which contains only one distinct character.</p><p><strong>Example 1:</strong></p><pre>Input: s = &quot;AAABABB&quot;, k = 1<br><br>Output: 5</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= s.length &lt;= 1000</li><li>0 &lt;= k &lt;= s.length</li></ul><p><strong>Intuition:</strong></p><p>The longest window is considered to be one that is formed of window = max character count + non max char count , and contains atmost k non max characters that are replaceable.</p><p>That would mean window — max_char_count ≤ k should satisfy in order to be a valid substring.</p><p>Thus for every character we include in the substring, until the above condition is valid. When it becomes valid, we shrink the window from the left until the window is valid again, and the expansion loop continues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*ApX3Pk1mKh1K7dl07EMxGw.png" /></figure><p><strong>Solution:</strong></p><pre>class Solution {<br>    public int characterReplacement(String s, int k) {<br>        int[] f = new int[26];<br>        int l=0, max=0;<br>        for(int r=0; r&lt;s.length(); r++){<br>            f[s.charAt(r)-&#39;A&#39;]++;<br><br>            // shrink, since we have more non max chars than k<br>            while( r-l+1 - maxFreq(f) &gt; k ){<br>                f[s.charAt(l)-&#39;A&#39;]--;<br>                l++;<br>            }<br><br>            max = Math.max(max, r-l+1);<br>        }<br>        return max;<br>    }<br><br>    private int maxFreq(int[] f){<br>        int freq=f[0];<br>        for(int i=0; i&lt;26; i++){<br>            freq = Math.max(freq, f[i]);<br>        }<br>        return freq;<br>    } <br>}        </pre><p><strong>Complexity:</strong></p><p>Time : O(26n) , for every character we possibly do a max freq count.</p><p>Space : O(1) , we use freq map.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f0a4f140cd0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 19: Longest Substring with Atleast K Repeating Characters]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-19-longest-substring-with-atleast-k-repeating-characters-3f6f60aa2dd0?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f6f60aa2dd0</guid>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[sliding-window-algorithm]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Sat, 25 Apr 2026 08:20:06 GMT</pubDate>
            <atom:updated>2026-04-25T08:20:06.251Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zlvV4RS0YX9btq8C" /><figcaption>Photo by <a href="https://unsplash.com/@adriansiaril?utm_source=medium&amp;utm_medium=referral">Adrian Siaril</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/description/?envType=problem-list-v2&amp;envId=sliding-window">Longest Substring with At Least K Repeating Characters - LeetCode</a></p><p><strong>Problem Statement:</strong></p><p>Given a string s and an integer k, return <em>the length of the longest substring of</em> s <em>such that the frequency of each character in this substring is greater than or equal to</em> k.</p><p>if no such substring exists, return 0.</p><p><strong>Example 1:</strong></p><pre>Input: s = &quot;aaabb&quot;, k = 3<br>Output: 3<br>Explanation: The longest substring is &quot;aaa&quot;, as &#39;a&#39; is repeated 3 times.</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= s.length &lt;= 10^4</li><li>s consists of only lowercase English letters.</li><li>1 &lt;= k &lt;= 10^5</li></ul><p><strong>Intuition:</strong></p><p>To find the longest substring with atleast k repeating characters, we have to avoid the characters that are less than k times. That means, those characters wont be a part of the solution string.</p><p>We can use those characters as pivot points, and divide the problem into two parts; left and right, and solve them recursively as sub-problems (Divide and Conquer). Ultimately, we would get to a sub-string which has all characters freqeunct atleast k. That would become solution to he subproblem.</p><p>For every left and right substring, we compare both results, and return the max length recursively, till we get a global solution.</p><p>Even though we slide, we slide to find a range ending with pivot, then this pivot is used to solve the left and right subproblems seperately.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*XgraSJj_XHilIzpgT1DFpQ.png" /></figure><p><strong>Solution:</strong></p><pre>class Solution {<br>    public int longestSubstring(String s, int k) {<br>        int n=s.length();<br>        if(n &lt; k) return 0; // if any string is less than k, not possible<br><br>        int[] f = new int[26];<br>        for(int j=0; j&lt;n; j++){<br>            f[ s.charAt(j) -&#39;a&#39;]++;<br>        }<br><br>        int i=0;<br>        while(i&lt;n){     // find valid pivot<br>            char c = s.charAt(i);<br>            if( f[ c-&#39;a&#39; ]!=0 &amp;&amp; f[c-&#39;a&#39;]&lt;k )<br>                break;<br>            i++;<br>        }<br><br>        // never found the pivot, if the current sub problem is valid<br>        if(i &gt;= n)<br>            return n;<br><br>        // divide into two subproblems,<br>        // dont solve previous problems, again <br>        int left = i==0 ? 0 : longestSubstring(s.substring(0, i), k);<br>        int right = i==n-1 ? 0 : longestSubstring(s.substring(i+1), k);<br><br>        return Math.max(left, right);<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(nlogn) , the string is traversed linearly, but recusrion is invlolved.</p><p>Space : O(n) , we use counters.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f6f60aa2dd0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA Decoded | Day 18: Container with Most Water]]></title>
            <link>https://medium.com/@mayank141shaw/dsa-decoded-day-18-container-with-most-water-66dd25632d40?source=rss-bd1be18dbc3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/66dd25632d40</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[sad]]></category>
            <dc:creator><![CDATA[Mayank Kumar Shaw]]></dc:creator>
            <pubDate>Tue, 21 Apr 2026 15:29:35 GMT</pubDate>
            <atom:updated>2026-04-21T15:29:35.774Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Dbt93db4dZr3lis1" /><figcaption>Photo by <a href="https://unsplash.com/@skstrannik?utm_source=medium&amp;utm_medium=referral">Sergej Karpow</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>“This is a running series of posts, where I solve DSA problems and try to explain them as thoroughly as possible for anyone to understand. Usually I’ll be using </em><strong><em>JAVA</em></strong><em> for solving all problems”</em></p><p>The link to this question is provided below</p><p><a href="https://neetcode.io/problems/max-water-container/question">Container With Most Water - NeetCode</a></p><p><strong>Problem Statement:</strong></p><p>You are given an integer array heights where heights[i] represents the height of the <em>ith</em> bar.</p><p>You may choose any two bars to form a container. Return the <em>maximum</em> amount of water a container can store.</p><p><strong>Example 1:</strong></p><pre>Input: height = [1,7,2,5,4,7,3,6]<br><br>Output: 36</pre><p><strong>Constraints:</strong></p><ul><li>2 &lt;= height.length &lt;= 1000</li><li>0 &lt;= height[i] &lt;= 1000</li></ul><p><strong>Intuition:</strong></p><p>To find the area, we fix two bars with two pointers left and right and find the area between it as (r-l) * min(height[l], height[r]) . To maximize the area, we shrink the indexes to maximize the heights, which would in turn increase our area.</p><p>Update the pointer with the lower height, to increase its height and overall area. Continue till left and right pointers don’t collide.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*Ydc24MKLexylgRWiVIe2OQ.png" /></figure><p><strong>Solution:</strong></p><pre>class Solution {<br>    public int maxArea(int[] heights) {<br>        int max = 0;<br>        int l=0, r = heights.length-1;<br><br>        while(l&lt;r){<br>            int area = (r-l) * Math.min(heights[l], heights[r]);<br>            max = Math.max(max, area);<br>            <br>            // shrink<br>            if(heights[l] &gt; heights[r]) r--;<br>            else l++;<br>        }<br><br>        return max;<br>    }<br>}</pre><p><strong>Complexity:</strong></p><p>Time : O(n) , we traverse the list linearly.</p><p>Space : O(1) , we use two pointers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66dd25632d40" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>