<?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 Gaurav Gupta on Medium]]></title>
        <description><![CDATA[Stories by Gaurav Gupta on Medium]]></description>
        <link>https://medium.com/@gauravit1996?source=rss-4189ebd432e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mqiKIp0bVhi1gPQl</url>
            <title>Stories by Gaurav Gupta on Medium</title>
            <link>https://medium.com/@gauravit1996?source=rss-4189ebd432e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 11:09:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gauravit1996/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[Prefix Sum Algorithm]]></title>
            <link>https://medium.com/@gauravit1996/prefix-sum-algorithm-cffdde0ccdb4?source=rss-4189ebd432e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/cffdde0ccdb4</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[prefix-sum]]></category>
            <dc:creator><![CDATA[Gaurav Gupta]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 08:43:23 GMT</pubDate>
            <atom:updated>2020-06-08T12:30:24.782Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7XR62SkLxrQw6fVO" /><figcaption>Photo by <a href="https://unsplash.com/@antoine1003?utm_source=medium&amp;utm_medium=referral">Antoine Dautry</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hey folks, As you can see today’s topic is <strong>Prefix Sum Algorithm.</strong></p><p>Another most important topic of Array through which<strong> range query problems </strong>are being solved<strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JwmSpvlNg5m8w-YJ" /><figcaption>Photo by <a href="https://unsplash.com/@31gatanes?utm_source=medium&amp;utm_medium=referral">Gui Avelar</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s see an example,</p><p>There is n number of kids, and these kids love to eat chocolates. well, what’s so special about these kids, as each kid love to eat chocolate. But again, Mother didn’t allow them to eat the chocolate 😅. So at the end of the day, she went to the shop and asked the shopkeeper from how many chocolates did this range of kids ate in a day?</p><p>So, let&#39;s say there are 10 kids.</p><p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</p><p>At 10:00 a.m, some of the kids brought chocolates for themselves.</p><p>let&#39;s say 1, 2, 3, 4, 5 kids or we can say kids from 1 to 5 have brought 3 chocolates, and ate them.</p><p>again at 1:30 p.m, kids range from 4 to 8 brought 7 chocolates and enjoyed.</p><p>Now kids at night, ranged from 6 to 9 brought 1 chocolate, and ate them.</p><p>Now what? It’s their sleep time 🤔. and they slept.</p><p>But her mother is very worried and as her responsibility, she wants to know which kids have eaten how many chocolates the whole day.</p><p>So, she went to the shopkeeper and asked her how many chocolates did the kids ate?</p><p>The shopkeeper is smart and always keeps updating the account book and he knew but there is a long queue of the customer and he also wants to attend the other customer. So how can he respond back to his mother and handle all customers as quickly as possible?</p><p>I hope you got the problem 🙎 ‍with the mother and shopkeeper problem.</p><p>Let’s help them.</p><p>again we will see two solutions, a basic and then an efficient way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FUh1KuqMoD4UEDiz" /><figcaption>Photo by <a href="https://unsplash.com/@jayclark?utm_source=medium&amp;utm_medium=referral">Jay Clark</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>so, as the shopkeeper is maintaining the record of kid’s visit.</p><p>what he can do is to update the cost each time from the beginning of the day.</p><p>So,</p><p><strong>shopkeeper= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; Don’ sneak in the book please. 😋</strong></p><p><strong>so, as we know at 10:00 a.m 1 to 5 children came and took 3 chocolates.</strong></p><p><strong>So, shopkeeper= [3, 3, 3, 3, 3, 0, 0, 0, 0, 0]; updates his book</strong></p><p><strong>again those cute kids came at 1:30 p.m and took 7 chocolates.</strong></p><p><strong>shopkeeper= [3, 3, 3, 10, 10, 7, 7, 7, 0, 0];</strong></p><p><strong>and the last group at night came and took 1 chocolate and now final time the shopkeeper updates his records as shopkeeper= [3, 3, 3, 10, 10, 7, 8, 8, 1, 0];</strong></p><p>Let’s say we have this list and mother asked random questions.</p><p><strong>tell me, Ramesh, kids from range 1 to 4 ate how many chocolates and now shopkeeper is started adding the numbers, and gave the result as 19.</strong></p><p><strong>again she asked kids from range 5 to 7 and again the shopkeeper start counting and gave result as 25.</strong></p><p>and more and more.</p><p>But this took time and some of his customers went away.</p><p>The shopkeeper is sad as this approach is taking too much time.</p><p>So, he wants some other technique. Let’s help this man.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xxVsgPK1DrYIqYG8" /><figcaption>Photo by <a href="https://unsplash.com/@austinchan?utm_source=medium&amp;utm_medium=referral">Austin Chan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So, now what the shopkeeper will do instead of adding up again and again he will only update some entries and in the add apply the algorithm as this man is smart. “<strong>Prefix Sum Algorithm</strong>”</p><p>let’s see how, as we now know the problem so, let&#39;s see the shopkeeper way…</p><p>so the shopkeeper is having this result-</p><p><strong>shopkeeper= [3, 3, 3, 10, 10, 7, 8, 8, 1, 0];</strong></p><p>but now before the mother starts asking the question. The Shopkeeper knows the algorithm and starts managing it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*kJuyMrGzh9MEy3LXC2NL9w.jpeg" /></figure><p>can you see the pattern? The Shopkeeper have the register and I hope you also have it.</p><p>So, why wait? Take out your pen and match the result.</p><p><strong>shopkeeper= [3, 6, 9, 19, 29, 36, 44, 52, 53, 53];</strong></p><p>Did you also get the same?</p><p>If yes! 🥳 congrats you have learned the first part of the algorithm and those who are just reading please enjoy the reading.</p><p>Now, the mother has many questions and she starts with her query.</p><p><strong>tell me, Ramesh, kids from range 1 to 4 ate how many chocolates and now the shopkeeper instead of adding directly check the number 4 kid and gave the result as 19 which is correct as above mentioned.</strong></p><p><strong>again she again asked kids from range 5 to 7? and by applying some calculation</strong></p><p>Total chocolates = kid at 7 subtract by kid at 4.</p><p>but why this equation?</p><p>ok, let’s again break this</p><p>can you tell me kids from 1 to 7 ate how many and yes result is 44.</p><p>but we want to know the result from 5 to 7. Please don&#39;t afraid. It’s easy peasy</p><p>Total(1, 7)= chocolate(1,4)+chocolate(5, 7).</p><p>let’s get back to the 9th class, <strong>x= y+z</strong>. students, please find z?</p><h3><strong>z= x-y.</strong></h3><p>Easy? let&#39;s look at above equation now.</p><p><strong>chocolate(5, 7)= Total(1, 7)- chocolate(1,4)</strong> = <strong>25</strong></p><p>and her mother gets all the answers and the shopkeeper is happy too as he can now attend more customers now.</p><p>Pretty long 🤯?</p><p>But the concept will always remain in your mind.</p><p>Let’s look at the code of shopkeeper which he contains in his brilliant mind.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*C2tVqeR84_rZ5_x8" /><figcaption>Photo by <a href="https://unsplash.com/@clemhlrdt?utm_source=medium&amp;utm_medium=referral">Clément H</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><pre>public static void main (String[] args) throws java.lang.Exception<br> {<br> int[] shopkeeper= new int[]{3, 3, 3, 10, 10, 7, 8, 8, 1, 0};<br> int tracker= 0, result= 0;<br> <br> int[][] motherQuestions= new int[][]{<br> {1, 4},<br> {5, 7},<br> {1, 9},<br> {2, 9}<br> };<br> <br> for(int i= 0; i&lt; shopkeeper.length; i++){<br> shopkeeper[i]+= tracker;<br> tracker= shopkeeper[i];<br> }<br> <br> for(int i= 0; i&lt; shopkeeper.length; i++){<br> System.out.print(shopkeeper[i]+” “);<br> }<br> <br> System.out.println();<br> <br> for(int i= 0; i&lt; motherQuestions.length; i++){<br> int fromWhichKid= motherQuestions[i][0];<br> int untillWhichKid= motherQuestions[i][1];<br> if(fromWhichKid-2&lt;0){<br> result= shopkeeper[untillWhichKid-1];<br> }else{<br> result= shopkeeper[untillWhichKid-1]-shopkeeper[fromWhichKid-2];<br> }<br> System.out.println(“Result- “+ result);<br> }<br> <br> }</pre><p>Time Complexity- for searching the particular query is O(1). and for n query, we have O(n) time complexity.</p><p>So, that’s it. The shopkeeper is now happy and after this prolonged reading, I think you should also grab some chocolate 😃.</p><p>I hope you like the article. Please clap, share, and follow me for more amazing posts like this.</p><p>Please ignore grammar errors, I am a coder, not an English teacher. 😁</p><p>For the next time, bye-bye.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cffdde0ccdb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Two Pointers Technique]]></title>
            <link>https://medium.com/@gauravit1996/two-pointers-technique-2f5ac7cd7190?source=rss-4189ebd432e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f5ac7cd7190</guid>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[techniques]]></category>
            <dc:creator><![CDATA[Gaurav Gupta]]></dc:creator>
            <pubDate>Fri, 05 Jun 2020 04:42:06 GMT</pubDate>
            <atom:updated>2020-06-05T05:24:18.038Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xTQi6NqtIKLi2NQb" /><figcaption>Photo by <a href="https://unsplash.com/@amani_nation?utm_source=medium&amp;utm_medium=referral">Amani Nation</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s discuss one of the most important techniques of Array named <strong>Two Pointers Technique.</strong></p><p>what does this mean?</p><p>Let&#39;s forget the computer jargon and try to relate it with a real-world scenario.</p><p>for example-</p><p>Let’s take there are n shops and in one of the shop crime scenes happened but don&#39;t know in which it happens?</p><p>Now Sherlock is in a hurry and with the help of Watson, He wants to find the shop quickly.</p><p>Now, what will Sherlock do?</p><p>Sherlock is smart and he finds a certain way of solving this problem</p><ol><li>He told Watson to go to the last shop and sherlock will start from the beginning and at this rate they will find the shop in half time. (opposite-directional pointer)</li><li>Another way, He asked Watson that he will check shop number 1 and you check shop number 2 i.e Watson can skip or jump from one to another. (Equi-Directional Pointer)</li></ol><p>wow, Sherlock is now able to go to his date quickly.</p><p>So, I hope you will now get the idea of the <strong>Two Pointers Technique</strong>.</p><p>Let’s Elaborate this now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YjofPfFDvlkz3yKN" /><figcaption>Photo by <a href="https://unsplash.com/@jimmy_p?utm_source=medium&amp;utm_medium=referral">jimmy pantzy</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>The idea here is to iterate two different parts of the array simultaneously to get the answer faster.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*N0V72vLyxp4nIZwS" /><figcaption>Photo by <a href="https://unsplash.com/@benwhitephotography?utm_source=medium&amp;utm_medium=referral">Ben White</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s take one simple code example to illustrate this concept.</p><p>Reverse the String.</p><p>we will swap the last and first character of string using two pointers technique.</p><pre><strong>public</strong> <strong>void</strong> <strong>reverse(String</strong> str<strong>)</strong> <strong>{</strong><br>    <strong>int</strong> i <strong>=</strong> 0<strong>,</strong> j <strong>=</strong> str<strong>.</strong>length() <strong>-</strong> 1<strong>;</strong><br>    <strong>while</strong> <strong>(</strong>i <strong>&lt;</strong> j<strong>)</strong> <strong>{</strong><br>        swapTheCharacters<strong>(</strong>str<strong>,</strong> i<strong>,</strong> j<strong>);</strong><br>        i<strong>++;</strong><br>        j<strong>--;</strong><br>    <strong>}</strong><br><strong>}</strong></pre><p>Let’s go through this example.</p><p>swapTheCharacters() function will swap the characters so that easy part is up to you.</p><p>what we have done is that we are iterating over the string and keeping two pointers <strong>I </strong>and <strong>J</strong> in the opposite direction.</p><p>“i” will move in forwarding direction and “j” will move in a backward direction and hence we will keep going like this until a situation came where “<strong>i&lt;j</strong>”.</p><p>This code shows the opposite directional Two pointers.</p><p>I hope you like this article. Share and feel free to clap and Don’t forget to subscribe.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A7GmzfuUtZySxwjQ" /><figcaption>Photo by <a href="https://unsplash.com/@hannynaibaho?utm_source=medium&amp;utm_medium=referral">Hanny Naibaho</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f5ac7cd7190" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Square Root Of An Integer]]></title>
            <link>https://medium.com/@gauravit1996/square-root-of-an-integer-8c79c9f7bbd6?source=rss-4189ebd432e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c79c9f7bbd6</guid>
            <category><![CDATA[tips]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[linear-search]]></category>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Gaurav Gupta]]></dc:creator>
            <pubDate>Thu, 04 Jun 2020 00:37:58 GMT</pubDate>
            <atom:updated>2020-06-04T06:38:22.966Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*E8VNekm7zQA67dC73xE47Q.png" /></figure><p>In this article, I will try to define algorithms through which we can easily find the square root of a number.</p><p>Well, there are many ways to find the square root of an integer.</p><p><strong>Easy Way</strong></p><p>You have to iterate a loop just like a <strong>Linear Search</strong> and till when?</p><p>You have to move till you reach a condition where i*i&gt;n. STOP</p><p>now we got an integer whose square is greater than n.</p><p>So what should we do? well, just subtract 1.</p><p>and we got it, we found the number not precisely but yes this integer square is always less than and equal to n.</p><p>Code Given Below :-)</p><blockquote>public static int sqrt(int n) {</blockquote><blockquote>int i= 0;</blockquote><blockquote>if(n==0|| n==1){</blockquote><blockquote>return n;</blockquote><blockquote>}</blockquote><blockquote>for(i= 2; i&lt;= n; i++){</blockquote><blockquote>if(i*i&gt;n){</blockquote><blockquote>break;</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>return i-1;</blockquote><blockquote>}</blockquote><p>#Take care of 0 and 1 base cases.</p><p><strong>Complexity:</strong></p><p>Time — O(sqrt n), Space — O(n)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*8NTsQyEzw1TXJo9i-uZtsg.jpeg" /></figure><p><strong>Better Way</strong></p><p>Let&#39;s try to think this in a different way Ok. what we have done above is to iterate a loop till we find a number whose square is greater than the given integer. Can we do better? Think for 3 sec.</p><p>1, 2, 3, Time Over</p><p>Let’s see.</p><p>So! just see I have used a linear search in above approach. Is there any better approach than linear search…</p><p><strong>Binary Search.</strong></p><p>Yes, Using Binary Search we can make our iteration till half only.</p><p>Time Complexity- O(log n)</p><p>what’s the idea?</p><p>well, just use the Binary Search Algo to find the middle element and check</p><p>whether mid*mid==n , if Yes works then return mid.</p><p>else check below code snippet</p><blockquote>public static long binarySqrt(long n){</blockquote><blockquote>long start= 1;</blockquote><blockquote>long end= n;</blockquote><blockquote>long mid= 0;</blockquote><blockquote>long ans= 0;</blockquote><blockquote>if(n==0||n==1){</blockquote><blockquote>return n;</blockquote><blockquote>}else{</blockquote><blockquote>while(start&lt;=end){</blockquote><blockquote>mid= start+((end-start)/2);</blockquote><blockquote>if(mid*mid==n){</blockquote><blockquote>return mid;</blockquote><blockquote>}else if((mid*mid)&gt;n){</blockquote><blockquote>end= mid-1;</blockquote><blockquote>}else{</blockquote><blockquote>start= mid+1;</blockquote><blockquote>ans= mid;</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>return ans;</blockquote><blockquote>}</blockquote><p>That’s it, folks.</p><p>I hope you like the article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c79c9f7bbd6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>