<?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 Supratick Mondal on Medium]]></title>
        <description><![CDATA[Stories by Supratick Mondal on Medium]]></description>
        <link>https://medium.com/@sonumondal5006?source=rss-338553e28ab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Dc14oa9QeAANjSuv</url>
            <title>Stories by Supratick Mondal on Medium</title>
            <link>https://medium.com/@sonumondal5006?source=rss-338553e28ab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 01:54:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sonumondal5006/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[Search in Rotated Sorted Array]]></title>
            <link>https://medium.com/@sonumondal5006/search-in-rotated-sorted-array-d6d49e77beec?source=rss-338553e28ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6d49e77beec</guid>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Supratick Mondal]]></dc:creator>
            <pubDate>Fri, 22 May 2026 04:43:52 GMT</pubDate>
            <atom:updated>2026-05-22T04:43:52.759Z</atom:updated>
            <content:encoded><![CDATA[<p>Here is the first Blog, might not be the best. But a small try to teach me and others.</p><p>Understanding the problem statement :</p><p>We have been given an array that is sorted and rotated, and a target.<br>We have to return the index of the target if present. If not we have to return -1.</p><pre>nums = [ 4 , 5 , 6 , 7 , 0 , 1 , 2 , 3 ]<br>idx  =   0   1   2   3   4   5   6   7<br>target = 1<br><br>ans = 5</pre><p>So , We just need to understand the story and the summary to solve the problem .</p><p>Easy way is that , we can do a linear search !! Easy way right .<br>Visit each index and find whether it is present or not .</p><pre>int ans = -1;<br><br>for(int i = 0 ; i &lt; nums.size() ; i++){<br>       <br>     if(target == nums[i]){<br>        ans = i;<br>        }<br>}<br><br>return ans;</pre><p>Time Complexity : O(N)</p><p>Wait what? We did something called Linear Search. Visit each index even when we know the target. Okay !!</p><p>How can we improve this “ <strong>Linear Search</strong> ”</p><p>We have one, what?</p><ul><li>Binary Search !!</li></ul><p>What does it do ??</p><p>Reduces the time Complexity first of all.</p><p>Reduces by log(N) times.</p><p>Can we see some code in action :</p><pre>int left = 0 ;<br>int right = nums.size()-1;<br>int ans = -1;<br><br>while(left &lt;= right){<br><br>  int mid = left + (right - left)/2;<br><br>   if(nums[mid] == target){<br>      ans = mid;<br>      break;<br>    }else if(nums[mid] &lt; target){<br>      left = mid + 1;<br>    }else{<br>      right = mid - 1; <br>    }<br><br>}</pre><p>So, this is Binary Search, 1 min. I didn’t get what you just wrote.</p><p>Understand the storyline:</p><ul><li>If my answer is equal to the target, then this becomes my answer.</li><li>If I am finding much before my target, then I should move towards the target, which is to the right.</li><li>If I am finding much later than the target means I exceed the target, so I should move to the left.</li></ul><p>Lets see by code example :</p><pre>nums = [ 1 , 2  , 3 , 4 , 5 ]<br>idx  =   0   1    2   3   4  <br><br>left = 0;<br>right = 4;<br>mid = 2 <br><br>Suppose target is 1 : <br><br>We exceeded the target : nums[mid] = nums[2] = 3 <br>So we move towards left : right = mid -1 = 2 -1 = 1 <br><br>Suppose target is 4 : <br>We are searching much before the target : nums[mid] = nums[2]= 3 ;<br>Now , 3 &lt; 4 so we should search towards the right : <br>left = mid+1;</pre><p>So , is the problem Over !! 😅 No this is for the binary Search Only . <br>Is this not enough for this problem : <strong>NO !!</strong></p><p>I have a doubt : we know <strong>mid = (left + right)/2</strong>;</p><p>It’s so simple !! why we need ?</p><p><strong>mid = start + (end-start)/2;</strong></p><p>In simple terms, if I say 1st method will cause an overflow for large arrays or may sometimes be out of bound. Which will give you red errors.<br>2nd method keeps you in a range where it will not be out of bound.</p><p>Why did we write break? After getting the ans ??</p><p>Since we don’t want to loop again and again. We provided the condition : (left ≤ right) until and unless left exceeds right, the loop will not broke.</p><pre><br>nums = [1 , 2 , 3 , 4]<br><br>target = 3 ; <br><br>Suppose you get the ans at index = 2 ; <br><br>left = 2 <br>right = 2<br>ans = 2 ;<br><br>But it will loop again : <br>left &lt;= right <br><br>Hence we should break the loop once we get the answer . </pre><p>Go of all the doubts :</p><p>Binary Search is for sorted array only . But it is also rotated ? How to solve this then ??<br>Well, we have what? <br>Rotated array as this : [ 3 , 4 ,5 ,6 , 0 ,1 ,2 ]<br>Can we just? What?</p><p>Make a binary search on sorted parts? <br>What do you mean by that?<br>Yeah, like binary search on this :</p><pre>Binary search on this two parts : <br>[3 , 4 , 5 ]<br>[1,2]</pre><p>How do we get the pivot index? That is the smallest element.</p><p>Story Line :</p><p>What happens in a sorted Array? <br>left &lt; mid &lt; right</p><p>What happens in a rotated sorted array? <br> 3, 4,5, 1, 2</p><p>mid is 5 &gt; right (2)<br>It means a chance to get the smaller element to the right.</p><p>What do we do? <br>left = mid + 1;</p><p>left becomes: nums[mid+1] = nums[3] = 1 , Yeah I got the smallest</p><p>But that ‘s where it doesn’t stop : <br>Now left = 3 &amp; right = 4 &amp; mid = 7/2 =3</p><p>nums[3] = 1 which is smaller probably this could be my ans and we don’t want to miss it . What we do ?</p><p>right = mid ;</p><p>Then left == right Here we break the condition while(left &lt; right )</p><p>Story points :</p><p>if my arr[mid] &gt; arr[right] probably my answer should be towards right since it is smaller . Move towards smaller : left = mid+1;</p><p>if(arr[mid] &lt; arr[right])</p><ul><li>it means we are in the sorted order of the list . Probably mid could be my answer but we also look for better answer so we make ;</li><li>right = mid this time</li><li>not right = mid-1; // Else might be we skip the answer .</li></ul><pre>int pivot(vector&lt;int&gt;&amp; nums , int start , int end){<br>        <br>        int ans = -1;<br><br>    while(start &lt; end){<br><br><br>        int mid = start + (end - start)/2;<br><br>        if(nums[mid] &gt; nums[end]){<br>            start = mid + 1;<br>        }else{<br>            end = mid;<br>        }<br>    }<br><br>    return end; or // return start both will work since they become equal on the answer<br>}</pre><p>Please comment . Should I need to improve my Explanation full code :</p><pre>class Solution {<br>public:<br><br>    int BS(vector&lt;int&gt;&amp; nums, int target , int start , int end){<br>         <br>        int ans = -1 ;<br><br>        while(start &lt; end){<br>             <br>            int mid = start + (end - start)/2;<br><br>            if(nums[mid] == target){<br>                ans = mid;<br>                break; // if you got the result still searching : TLE<br>            }else if(nums[mid] &lt; target){<br>                start = mid+1;<br>            }else{<br>                end = mid;<br>            }<br><br>        }<br><br>        return ans;<br><br>    }<br><br>    int pivot(vector&lt;int&gt;&amp; nums , int start , int end){<br>        <br>        int ans = -1;<br><br>    while(start &lt; end){<br><br><br>        int mid = start + (end - start)/2;<br><br>        if(nums[mid] &gt; nums[end]){<br>            start = mid + 1;<br>        }else{<br>            ans = mid;<br>            end = mid;<br>        }<br>    }<br><br>    return end;<br>}<br><br><br>    int search(vector&lt;int&gt;&amp; nums, int target) {<br>        <br>        int start = 0;<br>        int n = nums.size();<br><br>        int pvtInd = pivot(nums,0,n-1);<br><br>        int left_Ind = BS(nums,target,0,pvtInd-1);<br><br>        if(left_Ind != -1){<br>            return left_Ind;<br>        }<br><br>        int right_Ind = BS(nums,target,pvtInd,n-1);<br><br>        if(right_Ind != -1){<br>            return right_Ind;<br>        }<br><br>        return -1;<br><br>    }<br>};</pre><p>Thanks for giving it a read .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6d49e77beec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>