<?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 Sayantan on Medium]]></title>
        <description><![CDATA[Stories by Sayantan on Medium]]></description>
        <link>https://medium.com/@sayantanghosh011?source=rss-59ba9109de5f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*n7_sGsZBBhCXvNT8sHtrQg.jpeg</url>
            <title>Stories by Sayantan on Medium</title>
            <link>https://medium.com/@sayantanghosh011?source=rss-59ba9109de5f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 08:53:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sayantanghosh011/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[Minimize the Heights →GFG]]></title>
            <link>https://medium.com/@sayantanghosh011/minimize-the-heights-gfg-ab5d05413b3d?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab5d05413b3d</guid>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[minimize]]></category>
            <category><![CDATA[gfg]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Wed, 12 Oct 2022 17:30:32 GMT</pubDate>
            <atom:updated>2022-10-12T17:30:32.362Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UuTcEGwZ5ZuMccj3plT3hg.png" /></figure><p>The above snapshot is the optimal O(n logn) solution to the problem.</p><p>Let me quickly elucidate what’s going on →</p><p>Here the problem is to find out the <strong>minimum </strong>possible difference between the height of the shortest and tallest towers after having modified each tower by either increasing or decreasing by k.</p><p>Now here we follow a strategic algorithm. First we sort the entire array and then we initialise the ans variable with the difference between the first tower and the last tower, we copy the the height of 0th tower in smallest variable and height of the n-1th tower in largest variable. We traverse the array starting from 1 till n-1 now as we have sorted the array the height of the tower goes on increasing as we move further till the end (ascending order sorting). While traversing, we increase the nums[i-1] by k and decrease the nums[i] by k and we find the minimal value everytime. Similarly we increase the 0th tower by k and decrease the n-1th tower height by k and we find the maximal value. At last we change the ans variable value with the new minimum between ans(initial) and largest-smallest.</p><p>By the end of the function we return the result as ans.</p><p>This is a hard DSA problem as mentioned in GFG but still if you read this article closely I hope you will understand the approach flawlessly :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab5d05413b3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React re-rendering feature can cause a huge hit in performance.]]></title>
            <link>https://medium.com/@sayantanghosh011/react-re-rendering-feature-can-cause-a-huge-hit-in-performance-5931cbdb961d?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5931cbdb961d</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[optimisation]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Wed, 31 Aug 2022 18:34:34 GMT</pubDate>
            <atom:updated>2022-08-31T18:34:34.729Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VZTi2BNMKKK2HcqC9QcFSw.png" /></figure><p>The above code is a ReactJs code displaying a counter on the page where a user can go and click the button to increase the counter by 1.</p><p>Now the real problem is everytime the user clicks the button on the user experience side the user wont notice any significant problem as they can only observe the counter increasing on every click ,but the real problem arrives when we see the console of the browser and we see the “clicked” text re-rendering every time the counter increases which is not at all good and while building larger applications the web application can slow down and hit a performance issue. React scans the whole dom tree and checks whether there is any change and it compares it with it’s another virtual dom and finally make changes into the real dom. Any change occurs in any node of the tree the React scans and re-renders the whole tree instead of just renderingthe exact portion. This is a drawback,but there is nothing to worry the developers came up with the idea of the hooks in the recent functional component in React.</p><p>Now they introduced something called useEffect hook →</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lvGAQXPzcTuVIo9i1eDcCQ.png" /></figure><p>This is the modified version of the code where we wrapped up the console.log(“clicked”) part with useEffect hook…</p><p>Now if you go and check the console the clicked message will only render once when the page loads as we have provided an emty array dependency(dependencies basically tells react when to render which component) Pretty cool right. Hence optimised :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nlytf1ObJ_5bvJSzmScZEQ.png" /></figure><p>We can also perform this using React useMemo() hook . This hook is used to memoize the particular wrapped up portion so that any time change occurs it stores it as a cache and do not re-render multiple times.</p><p>Pretty cool concept right?I guess I can make my point clear.</p><h3>😉</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5931cbdb961d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Jump Game → Leetcode]]></title>
            <link>https://medium.com/@sayantanghosh011/jump-game-leetcode-eded24faf20d?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/eded24faf20d</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[optimisation]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Thu, 25 Aug 2022 07:00:59 GMT</pubDate>
            <atom:updated>2022-08-25T07:00:59.084Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gPNho-L3-EiP94oYGa7a-w.png" /></figure><p>The above solution is the O(n) solution of the leetcode problem.</p><p>The problem states to find whether we can reach the end of the array or not.Here I have solved it using the valley peak method resulting in optimised solution.</p><p>Let me elucidate what’s going on-</p><p>Lets say there is an array of [2,2,1,4], here we can reach the end by making 2 jumps from the beginning reaching 1 and then making 1 jump reaching the end. Here each value represents the max possible jumps we can make to reach the end.In the above code we scan the whole array using the for loop and we set the reach variable with the maximum an index value can reach we replace it with new value everytime and we return false if this value becomes&lt;the current index position else we return true at the end of the code.</p><p>Like say for an example: [2,1,0,4].Here we cannot reach the end because when the index reachesat 2nd position having 0 the reach will be 2 and the moment the index reaches the end i.e i=3 →3&gt;reach it will return false.</p><p>Pretty cool approach. Thanks to tech dose for some support :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eded24faf20d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kth Largest Element in a array without Sorting]]></title>
            <link>https://medium.com/@sayantanghosh011/kth-largest-element-in-a-array-without-sorting-a389761d52d1?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a389761d52d1</guid>
            <category><![CDATA[priority-queue]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[max-heap]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Tue, 26 Jul 2022 17:35:20 GMT</pubDate>
            <atom:updated>2022-07-26T17:35:20.037Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZHHh-xhSggN8G-oHWdbqBw.png" /></figure><p>The above technique is used to find the kth largest element in array without sorting ,thus reducing complexity.</p><p>This technique uses the Max Heap algorithm using PriorityQueue. The PriorityQueue adds the element in a reverseOrder where a while loop is implemented and it runs till k becomes 0 . The queue polls elements till k times and returns the peek element at the last which is the kth largest element.</p><p>Pretty cool technique and you can use it in solving various complex DSA problems thus reducing time complexity afterall.</p><p>Do try this code in your IDE’s</p><p>Till then keep on optimising codes :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a389761d52d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rat in a Maze -I]]></title>
            <link>https://medium.com/@sayantanghosh011/rat-in-a-maze-i-3d26037fadf2?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d26037fadf2</guid>
            <category><![CDATA[dfs]]></category>
            <category><![CDATA[rat-in-a-maze]]></category>
            <category><![CDATA[gfg]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Mon, 25 Jul 2022 20:16:35 GMT</pubDate>
            <atom:updated>2022-07-25T20:16:35.474Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dhbh6f4pQYZD0f0_7R1stw.png" /></figure><p>The above code snippet is all about finding the all possible paths that the rat can take to reach from source to destination. The approach used here is simple Dfs approach and the time complexity of the problem is O(4^(n²)) which is kinda acceptable :)</p><p>Let me quickly elucidate what’s going on ,</p><p>This is kinda the same problem like the unique path problem in leetcode. If you haven’t checked it yet I advise you to first check the code and explanation provided by me in my account . The problem statement says the rat can only traverse through the grids having 1 as value so the base case becomes if the grid is 0 and the coordinates either reach out of bound or less than 0. We again check if it reaches the boundary then only we add the string in the arraylist . Now the destination is the boundary (given). You might be thinking why string ? The output should be the path direction the rat takes to reach the destination so we pass a string either “U”,”L”,”R”,”D” all four direction via dfs calls and those get added in the ArrayList when it reaches the destination.</p><p>We need to find all the possible paths the rat can take to reach the destination</p><p>We make four dfs calls as there are four directions.We call the dfs function in the other function and return the ArrayList . At last we print all the values or rather directions stored in the array list.</p><p>It was a simple DFS problem. Thanks to GFG for some reference though.</p><p>Being a novice in DSA(advanced) hurts :(</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d26037fadf2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Longest Consecutive Subsequence]]></title>
            <link>https://medium.com/@sayantanghosh011/longest-consecutive-subsequence-f8f8e4b4d2d0?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8f8e4b4d2d0</guid>
            <category><![CDATA[gfg]]></category>
            <category><![CDATA[optimisation]]></category>
            <category><![CDATA[lcs]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Thu, 21 Jul 2022 19:09:37 GMT</pubDate>
            <atom:updated>2022-07-21T19:09:37.559Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Nl0hOo5mxXJQu6NqSjeUA.png" /><figcaption>Native Approach</figcaption></figure><p>The above code snippet is the brute force approach of finding the longest consecutive subsequence. Now the time complexity of the above snippet is 0(nlogn). Now you might be thinking why log n?</p><p>Lets focus on something the loop runs till n-2 and the log n part occurs due to the sorting technique.</p><p>Let us quicky see the optimised solution ,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gUYqaaMFLR7EvLzpKGicaA.png" /><figcaption>Optimised Solution</figcaption></figure><p>We declared a boolean array . Intially filling it with false . We loop through the array and we set the indexes of the dummy boolean array that equals the element of the given array with true.</p><p>So we check when the the elements present in the array arr[] equals the index of dummy[] then we set the values of those indexes with true.</p><p>Now we start another loop that goes till the max that is the size of the dummy array -1, and we check if the values in the indexes of dummy are true then we set counter ++ else we reset counter to 0. Lastly we find the max counter.</p><p>Our objective was to eliminate the n part in 0(nlogn) that we did successfully by creating another dummy array of different size. Instead of n we loop till the size of the dummy array-1.</p><p>Pretty cool approach . Just analyze it once before trying it out in your editors :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8f8e4b4d2d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Print all the possible permutations of a String]]></title>
            <link>https://medium.com/@sayantanghosh011/print-all-the-possible-permutations-of-a-string-fb9c8877bd82?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb9c8877bd82</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[string]]></category>
            <category><![CDATA[permutations]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Wed, 20 Jul 2022 18:09:41 GMT</pubDate>
            <atom:updated>2022-07-20T18:09:41.259Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jeHrZFm4RZfy79bnRXuCiw.png" /></figure><p>The above code snippet is helping is to find all the possible permutations of a given string.</p><p>Okay let me elucidate what’s happening ,</p><p>Given a string =”abc” , all possible permutations are :</p><p>cba<br>bca<br>bac<br>cab<br>acb<br>abc</p><p>Okay so first let us understand the recursive tree,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsnS6d3WeTN4F4c1Itv2FQ.png" /></figure><p>Here 2 is the unprocessed part(ie.the actual string initailly) and 1is the processed part</p><p>We can see for every processed part there are k subprocesses ,where k =processed+1</p><p>The base condition of the recursion will be when the unprocessed becomes empty then it will return the processed part as mention in the bottom of the diagram.</p><p>The for loop will run fro 0 to 2 (3 iterations →height of the tree)</p><p>We fix the first character and we use the recursion technique call the function with String first(0,ith index)+the first character+String second(ith index to length) as the processed argument and up.substring[1] → the rest of the string as the unprocessed part.</p><p>At last the program gives all the permutations(processed part everytime it reaches base condition).</p><p>Take your time and analyze the concept that I have tried to implement :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb9c8877bd82" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unique Paths -II →Leetcode]]></title>
            <link>https://medium.com/@sayantanghosh011/unique-paths-ii-leetcode-ab918d5026db?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab918d5026db</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[dfs]]></category>
            <category><![CDATA[optimisation]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Tue, 19 Jul 2022 17:55:31 GMT</pubDate>
            <atom:updated>2022-07-19T17:55:31.986Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pYgj0uwlVX3Vs0uhNZrDEg.png" /><figcaption>Unique Paths-II</figcaption></figure><p>The above code snippet is to find the number of possible unique paths the robot can take to reach the bottom right corner.</p><p>The above implementation is done using dfs technique ,and further optimised using Memoization(2d DP)→ top-down approach.</p><p>Okay, now let me quickly elucidate what’s happening</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*yYreOWUlBQPJUXGu1NqyNw.png" /></figure><p>Okay as we can see in the above picture , the robot is in position (0,0) and the obstacle is in middle of the 3x3 grid.</p><p>Now the main catch is that theposition of the obstacle will be marked as 1 and other positions to 0. If we provide the above grid as input the output will be 2 as there will only 2 ways the robot can reach its’s destination without hitting the obstacle that is:</p><ol><li>Right-&gt;Right-&gt;Down-&gt;Down</li><li>Down-&gt;Down-&gt;Right.</li></ol><p>A simple note : The robot can only move down and right.</p><p>Now keeping all these in mind if we go to the code snippet ,the dfs function implements the total dfs calls and the technique returning the total possible ways the robot can reach to the destination . We first provide the base case that checking that if grid length(rowise) and grid length(column wise )==0 then we return 1, if they are &lt;0 then we return 0 and lastly we check that the position of the robot is never 1 (hitting the obstacle). We pass an 2D array to memoize the solution by storing all the sub-problems for optimization so that anytime it reaches to same solution it returns it. Then we provide the dfs calls using recursive technique two times as the robot can only move down and right direction. We return the total of two calls and store it in dp[][] array.</p><p>The time complexity of the above program is O(m*n)</p><p>There you go! Problem number is 63 is hence solved. 😃</p><p>Keep on optimising codes.😎</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab918d5026db" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Largest Sum Contiguous Sub-Array]]></title>
            <link>https://medium.com/@sayantanghosh011/largest-sum-contiguous-sub-array-99d6eac29998?source=rss-59ba9109de5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/99d6eac29998</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[kadanes-algorithm]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Sayantan]]></dc:creator>
            <pubDate>Fri, 24 Jun 2022 18:57:52 GMT</pubDate>
            <atom:updated>2022-06-24T18:57:52.197Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ch3WW6DInqkdcOqDnS08_A.png" /></figure><p>The above snippet is the code of finding the Largest Sum Contiguous Sub-Array of a given array. The above implementation is done using Kadane’s algorithm ,the optimized solution which is having O(n) time complexity.</p><p>Let me quickly elucidate what’s goin on -</p><p>Given an array nums as a parameter to the function maxSub() in which we have to find and return the maximum sum of the contiguous elements in the array . We initialize maxSum with the minimum possible value,and currentSum with zero. Next we start a for loop for the array and we check if the value of currentSum is less than zero then we set the value of currentSum with the current value of the array at i-th index and else if the value of currentSum &gt;0 then we keep on calculating the sum of the contiguous elements in the array . Next up we set the value of maxSum with the maximium value between the maxSum and currentSum ,here we use Math.max function( this function compares values and returns the max value).Then we return the maxSum.</p><p>A very important thing that should be kept in mind the Kadane algorithm doesnt work normally on an array with only negative numbers ,like lets say:[-1,-2,-5] . If we provide this type of input the result should always be -1 but by using kadane’s algorithm it is observed giving 0 as output as we used to do if(currentSum&lt;0)currentSum=0 which gonna give 0 everytime it checks for value less than 0 .So to avoid this we have to tweak some values and set currentSum to a[i] at that moment when it finds value&lt;0. Well thanks to LeetCode for making me find this by going the testcases. 😌</p><p>Next Lets compare this with the Brute-Force method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYij75-fja9-WwTFMT4B4Q.png" /></figure><p>This is the Brute-Force technique of the particular problem . The implementation and the approach is little different . Here we are using nested for-loops and we are traversing through the array. For i=n-1 , kth loop runs n-1 times .</p><p>Time Complexity Analysis:</p><p>The Brute-Force method gives us a complexity of O(n²) whereas the Kadane’s algorithm gives us the complexity of O(n). Hence Optimal solution is obtained 😃</p><p>There you go !!</p><p>Happy coding :)</p><p>Keep optimizing codes 😎</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99d6eac29998" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>