<?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 Saumya Rai on Medium]]></title>
        <description><![CDATA[Stories by Saumya Rai on Medium]]></description>
        <link>https://medium.com/@saunya.rai2010?source=rss-9f2801b46fa4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*jUb7rz8PZKpd-AH4</url>
            <title>Stories by Saumya Rai on Medium</title>
            <link>https://medium.com/@saunya.rai2010?source=rss-9f2801b46fa4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:25:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saunya.rai2010/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[HOW TO FIND THE LONGEST SUBSTRING WITHOUT REPEATING ELEMENTS? ]]></title>
            <link>https://medium.com/@saunya.rai2010/how-to-find-the-longest-substring-without-repeating-elements-c6a2a58682f4?source=rss-9f2801b46fa4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6a2a58682f4</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[sliding-windows]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[hashing]]></category>
            <dc:creator><![CDATA[Saumya Rai]]></dc:creator>
            <pubDate>Sun, 01 Aug 2021 09:20:44 GMT</pubDate>
            <atom:updated>2021-08-01T09:21:44.837Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hU8DBB9HwlhsTxIOcm6SJg.png" /></figure><h3>Problem Statement:</h3><blockquote>The question that I’ll be using to explain this is available on Leetcode.</blockquote><blockquote>You can click on the link below to read the problem statement.</blockquote><blockquote><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">https://leetcode.com/problems/longest-substring-without-repeating-characters/</a></blockquote><h4>The prerequisites for solving this problem are basic knowledge of String Manipulation, Sliding Window Technique as well as Hashing.</h4><p>The simplest approach is to form all the possible substrings and track the length of the longest substring with unique characters but in this case the time complexity would be O(N²) or O(N³). We have to find a solution that requires much lesser time that is less than O(N²).</p><p>Are you thinking what I am thinking 🤔? Yes, this is a classic example of a problem that can be solved using the legendary technique — <em>Sliding Window Algorithm</em>.</p><blockquote>Lets see what the question demands once again!</blockquote><blockquote>The input will be a string containing characters which may or may not be repeated. The task is to find the longest substring (a part of the given string) having all distinct characters.</blockquote><blockquote>For example, in the given string <strong>s= Engineer , </strong>the valid strings are<strong> Engin , ngine , gin , ine , er , En , Engi , </strong>etc. But what we have to find is the longest non-repeating substring. So the answer would be<strong> 5 </strong>(length of string <strong>Engin</strong>).</blockquote><blockquote>Note: <strong>Engine</strong> is not a valid string because it has two e’s in it and <strong>Enginr</strong> is not a valid string because it is a subsequence not a substring.</blockquote><blockquote>Apart from above, we are given constraints on the size of string and that the string only contains English letters, digits, symbols and spaces.. There can be many substrings as outputs but we have to return only the longest among them.</blockquote><h4><strong>Following is an idea of the approach that we will be using-</strong></h4><ul><li>First of all, we will declare a dictionary d<em> </em>that will help us in maintaining the characters of the string and we initialize every key to 0.</li><li>We take two pointers start and end<em> </em>and initialize them to -1 to maintain the window.</li><li>We declare another variable ans<em> </em>to keep the track of the maximum length.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o3M--_1MtEQTPMl-UZUxQw.png" /></figure><p><strong>Scan the string from left to right one character at a time.</strong></p><ol><li>If the character has not been encountered before i.e., not present in the dictionary d then we will add it and increment the end index. The ans will be the maximum of end-start and existing ans. Till now the count of each character in the window is 1.</li><li>If the character has been encountered before, i.e., present in the dictionary d, we will break the loop and start another loop with condition while start&lt;end and we will keep on incrementing the start and we will remove the character at start index of the string.</li><li>If the count of character at start becomes 1, then we break the loop because that means the count of every character in the window will be 1.</li><li>Steps #1 , #2 , #3 are moving the window.</li></ol><p>Let us take an example now s = “abcabcbb”</p><ul><li>Initially , the dictionary would be {‘a’: 0, ‘b’: 0, ‘c’: 0}</li><li>Now we keep on incrementing character count till end=2, start=-1 and ans=3 and dictionary d={‘a’: 1, ‘b’: 1, ‘c’: 1}</li><li>As soon as end reached 3, the dictionary becomes d={‘a’: 2, ‘b’: 1, ‘c’: 1} and we break the loop.</li><li>Now we run the second inner while loop and decrement the character count at start=0 , that is, ‘a’.</li><li>Now the dictionary becomes d={‘a’: 1, ‘b’: 1, ‘c’: 1} once again and start=0, end=3 and ans=3 and we break the loop.</li><li>Now the iteration goes to the first inner while loop and the value of dictionary becomes d={‘a’: 1, ‘b’: 2, ‘c’: 1} and end=4, start=0 and ans=3.</li><li>Now we run the second inner while loop and decrement the character count at start=1 , that is, ‘a’.</li><li>Now the dictionary becomes d={‘a’: 1, ‘b’: 1, ‘c’: 1} once again and start=1, end=4 and ans=3 and we break the loop.</li><li>We keep on repeating this process until both the flag values are False.</li></ul><p><strong><em>I shall now be sharing the code that I had used to implement this problem.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lKuqggZhRg_71CgUlViMKQ.png" /><figcaption>Code</figcaption></figure><p><em>Now let’s analyze the Time Complexity and Space Complexity.</em></p><ul><li><em>We are scanning the string from left to right only once, hence the Time complexity will be O(N)</em></li><li><em>You might be thinking the Space Complexity would be O(N) as well but you might be wrong here!!!!</em></li><li><em>We are only maintaining a dictionary for maximum 256 characters(English letters, digits, symbols and spaces), hence the space used is constant and the Space Complexity would be O(1).</em></li></ul><blockquote><strong><em>That’s all folks. I hope now you will be able to solve this problem and feel free to share your thoughts on this.</em></strong></blockquote><blockquote><strong><em>Till next time……Happy Coding 😄 and Keep Learning 🙏!</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6a2a58682f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to do Binary Search in a sorted 2D-Array and Search for the Target Element?]]></title>
            <link>https://medium.com/@saunya.rai2010/how-to-do-binary-search-in-a-sorted-2d-array-and-search-for-the-target-element-d9c8a917fa05?source=rss-9f2801b46fa4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9c8a917fa05</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Saumya Rai]]></dc:creator>
            <pubDate>Tue, 27 Jul 2021 08:52:08 GMT</pubDate>
            <atom:updated>2021-07-27T09:29:06.266Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Binary Search in a 2D Sorted Array" src="https://cdn-images-1.medium.com/max/540/1*ABVXI5_ET1rylSy1n4f0Vg.png" /></figure><h3>Problem Statement:</h3><blockquote>The question that I’ll be using to explain this is available on Leetcode.</blockquote><blockquote>You can click on the link below to read the problem statement.</blockquote><blockquote><a href="https://leetcode.com/problems/search-a-2d-matrix/">https://leetcode.com/problems/search-a-2d-matrix/</a></blockquote><p>The first thing that comes to your mind after reading the problem is that how can we apply Binary Search to a 2D sorted array? I would highly recommend you to first read how to implement the Binary Search method on a 1D List and then only proceed ahead.</p><h4>Since the array is already sorted along the rows as well as columns, thus we can do binary search to locate the exact position of target element and treat the 2D array as a flattened sorted list.</h4><p>For example, take the following array :</p><p>[1, 2, 4]<br>[8, 16, 32]<br>[64, 128, 256]</p><p>Now when we convert it into a 1D list it becomes into the following array:</p><p>[1, 2, 4, 8, 16, 32, 64, 128, 256]</p><p>If we simply do Binary Search in this array the time taken would be O(log m* log n), where m is the number of rows and n is the number of columns.</p><blockquote>Note: We are not converting it into a 1D-array, instead we are just treating it like a 2D-array.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*CDJ8RtvXGKHSwMZb0FuAGw.gif" /></figure><p>Now, first lets find out how to access the elements of the 2D matrix using the position of elements in the same 1D array.</p><p>For example, we need to find the position of <strong>32 in the 2D matrix. </strong>The position of <strong>32 in the 1D array is 5. </strong>So now how can we use this position 5 to find the exact position of this element in the given 2D-array. We can come up with a formula. The position of 32 in 2D array is [1,2].</p><p>So can we write 5=1*length of each row + 2, where length of each row is 3.</p><p>Similarly, for the element 256, we can write 8=2*3 +2</p><h3>It looks like this formula works well.</h3><blockquote>We can write position in 1D array=[float value of (position / n ) , position % n] Here n is the length of each row.</blockquote><p>Now, lets come up with the python code to implement this program.</p><blockquote><strong>Note: I shall be using Python language so that this concept is easy to understand for any person using any language. Easy to read code should be the priority.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*ey5E7M_9YF5j8gTCDaYcTw.png" /></figure><p>No library function has been used while implementing this code just pure Binary Search method.</p><h3><strong>Happy Learning!!! </strong>😄</h3><p>Any suggestions on how to optimize the code are welcomed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9c8a917fa05" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>