<?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 Rayhan Chowdhury on Medium]]></title>
        <description><![CDATA[Stories by Rayhan Chowdhury on Medium]]></description>
        <link>https://medium.com/@rayhanchowdhury?source=rss-a15ddbe15f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*4KLdV4i6MirgItYJ</url>
            <title>Stories by Rayhan Chowdhury on Medium</title>
            <link>https://medium.com/@rayhanchowdhury?source=rss-a15ddbe15f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:14:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rayhanchowdhury/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[Pizza from Spongebob [editorial]]]></title>
            <link>https://medium.com/@rayhanchowdhury/pizza-from-spongebob-editorial-ab2e4ad41a2?source=rss-a15ddbe15f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab2e4ad41a2</guid>
            <category><![CDATA[math]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[competitive-programming]]></category>
            <category><![CDATA[counting]]></category>
            <dc:creator><![CDATA[Rayhan Chowdhury]]></dc:creator>
            <pubDate>Thu, 18 Apr 2019 19:06:45 GMT</pubDate>
            <atom:updated>2019-04-18T19:06:45.209Z</atom:updated>
            <content:encoded><![CDATA[<h4>Problem link: <a href="https://toph.co/p/pizza-from-spongebob">https://toph.co/p/pizza-from-spongebob</a></h4><p><strong>Simple description:</strong> You have an M×N grid. You have to find the number of different rectangles within this grid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*wPOOcRlQk3sVfFwSgJDC6w.png" /><figcaption>There are 9 orthogonal and 1 diagonal (red colored) rectangles.</figcaption></figure><h4><strong>Solution:</strong></h4><p>First, we break down all rectangles in two types:</p><ol><li>orthogonal: rectangles whose arms are parallel to X-axis or Y-axis.</li><li>diagonal: rectangles whose arms are diagonal to X-axis and Y-axis.</li></ol><h4>Counting orthogonal rectangles:</h4><p>Well, that’s easy. Let’s take this 7×6 grid for an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*6AlRQW1mrbFWHbhz3FqEhQ.png" /></figure><p>Every time we can cut a new sub-segment from either <strong>AB</strong> segment or <strong>AD</strong> segment, we can construct a new orthogonal rectangle within this grid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*J0V0pDjw__7ZC4SX2aREvA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*4fsOiQYMncoxG_D3JRZwvQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*wgFxpc_5yi9oPXVA4A6TXw.png" /></figure><pre>Therefore, <br>number of orthogonal rectangles = number of different sub-segments that segment [0,M] has × number of different sub-segments that segment [0,N] has</pre><p>Number of different sub-segments for segment [0,N] <br>= in how many different ways we can choose two end points for the sub-segment<br>= ᴺ⁺¹C ₂<br>= ((N +1)×N)/2</p><pre>∴ number of orthogonal rectangles in M×N grid = ᴹ⁺¹C₂ × ᴺ⁺¹C₂</pre><h4>Counting diagonal rectangles:</h4><p>First, let’s think about those diagonal rectangles which couldn’t fit in grids smaller than M×N.<br>Let’s think about this diagonal rectangle in 9×6 grid:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*2kMpOUSQg92QjSNIOCHRVA.png" /></figure><p>here, we can see that,<br>DI/AJ = DL/AI = 4<br>It may seem like a coincident but it’s not.</p><pre>Let AJ = x and AI = y<br>∴ DI = N-y and DL = M-x <br>∴ (N-y).y = (M-x).x </pre><p>It can be proven that this property must hold for all diagonal rectangles that take the full grid to fit. Then again, different rectangles will have at least one of x or y different.</p><pre>∴ number of fit diagonal rectangles = number of (x,y) pairs that satisfies [ (N-y).y = (M-x).x ] this equation</pre><p>Now, the tricky part; how to calculate this count efficiently?</p><pre>Let, (N-y).y = (M-x).x = Q<br>∴ x and y both divides Q<br>∴ M = x + (Q/x)<br>∴ N = y + (Q/y)</pre><p>So, let’s think it this way: <br>first, we pick a number Q. Then we calculate all possible pair of <strong>(M, N)</strong> for this Q &lt;we take all divisors of Q and take them as x to get <strong>M</strong> and take them as y to get <strong>N</strong>&gt;, and then we add 1 to those <strong>(M, N) </strong>grid’s diagonal rectangle count for this Q.<br>We do this for all possible values of Q.</p><pre>int maxQ = maxM * maxN;</pre><pre>for(int i=1; i&lt;= maxQ; i++) {<br>    for(int j=i, k=1; j&lt;= maxQ; j+=i, k++) {</pre><pre>        if(i+k&gt;maxN || i+k&gt;maxM) {<br>            break;<br>        }<br>        M_N_list[j].push_back(i+k);<br>    }<br>}</pre><pre>for(int i=0; i&lt;= maxQ; i++) {</pre><pre>    for(int j=0; j&lt;M_N_list[i].size(); j++) {<br>        for(int k=0; k&lt;M_N_list[i].size(); k++) {</pre><pre>            int M = M_N_list[i][j];<br>            int N = M_N_list[i][k];</pre><pre>            if(N != M) {<br>                diagonal_count[M][N]++;<br>            }<br>        }<br>    }<br>}</pre><p>This is the c++ implementation of the given idea. You should notice that we avoided the N == M case during the calculation. That’s because we want to avoid over counting. We can calculate this case in a separate loop by an O(1) formula.</p><pre>for(int i=2; i&lt;= maxN &amp;&amp; i&lt;=maxM; i++) {<br>    diagonal_count[i][i] = ((i-1)&lt;&lt;1) - ((i%2)^1);<br>}</pre><p>Where does this formula come from?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fmAOfT8Sk3-BXDE4kfseKg.jpeg" /><figcaption>think…think…think</figcaption></figure><p>Now that we have the count for fit diagonal rectangles in our hand, we can calculate the count for all diagonal rectangles inside the [M×N] grid. This requires some recursion, a little bit of inclusion-exclusion and a whole lot of rectangle love like Sponge.</p><pre>total_diagonal_rectangles[M][N] = <br>      total_diagonal_rectangles[M-1][N] <br>    + total_diagonal_rectangles[M-1][N] <br>    - total_diagonal_rectangles[M-2][N];</pre><pre>for(int i=1; i&lt;=N; i++) {<br>    total_diagonal_rectangles[M][N] += (N-i+1)*diagonal_count[M][i];<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*D7DFVIGWTsOusaW35thjSA.jpeg" /><figcaption>Wait a minute! We can optimize this last loop, can’t we?</figcaption></figure><h4>Complexity</h4><p>Had the pre-calculation and the optimization been done in proper manner, the complexity would be :<br>O( maxQ *(number of divisor of maxQ which are &lt; maxM/2 and maxN/2)² )</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab2e4ad41a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript’s Promise : from a java programmer’s perspective]]></title>
            <link>https://medium.com/@rayhanchowdhury/javascripts-promise-from-a-java-programmer-s-perspective-1a9276294935?source=rss-a15ddbe15f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a9276294935</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[Rayhan Chowdhury]]></dc:creator>
            <pubDate>Sat, 15 Dec 2018 20:54:54 GMT</pubDate>
            <atom:updated>2018-12-15T21:08:16.668Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript’s Promise : from a java programmer’s perspective</h3><h3>Background</h3><p>The world is swarming with codes written in functional programming way. Anywhere we look at, any modern technology we interact with, built in functional way. Looking underneath, we see these tons of lambda functions; sitting there, being assigned, calling one another, recurring, twisting, changing shapes, changing usage, changing their entire purpose as they wish; looks like some evil serpent created by the darkest sorcery.</p><p>For people like me who just graduated in CS or still in undergrad; have some moderate understanding in Object Oriented way, but no knowledge in web technology or likes of JavaScript; its seems overwhelming to just look at those kind of codes. Only one thing comes in mind: <em>is this even logical?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*wdvauFxe9lqoZLFUOIO6eg.jpeg" /></figure><p>A few days ago I said enough is enough and started myself digging into nodejs to try and make a meaning out of it. I took help from some of my friends who know these and looking into there code I stumbled upon the term <em>“promise”.</em></p><blockquote>Hey! I heard this before!!</blockquote><p>…stopping them in the middle of their explanation. Now, I have some experience in android development and several times I needed to use a class called <em>“</em><a href="https://developer.android.com/reference/android/os/AsyncTask"><em>AsyncTask</em></a><em>”</em>. If this is unknown to you, you can look into the documentation which explains this really good. So, this so called <em>“promise” </em>seemed similar with AsyncTask and I googled a bit to know this better.</p><h3>Prerequisite: Lambda in java</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*7ZQOwpvbXkA0lUyAqgHIsA.jpeg" /></figure><p>I have done 4 or 5 android projects and all of the had atleast one thing in common: <strong>Buttons</strong>.</p><p>A cliche in android projects. Love it, hate it as much you want; but there is no escaping from it. There comes a <strong><em>setOnClickListener</em></strong> every time you put a button in your app.</p><p>Now, the <em>setOnClickListener</em> method of <strong>Button</strong> class takes one <em>interface</em> as parameter: the <strong><em>OnClickListener</em></strong> interface of the <strong>View</strong> class.</p><p>Keep in mind that this is a single method interface. Has only one method:</p><pre>public interface OnClickListener {<br>    void onClick(View v);<br>}</pre><p>the OnClickListener definition. The implementation follows:</p><pre>button.setOnClickListener(new View.OnClickListener() {<br>    @Override<br>    public void onClick(View v) {<br>        Toast.makeText(MainActivity.this,<br>            &quot;Hey! you clicked me!!&quot;,Toast.LENGTH_LONG).show();<br>    }<br>});</pre><p>Very simple. Makes sense, right?</p><p>During my recent most project, I curiously updated the java version to 8 and all those simple looking OnClickListener implementations started giving warnings!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*9i5o3FwXnNC2BfGys1B3QA.png" /><figcaption>what the hell?</figcaption></figure><p>It asked me to convert the interface implementation into lambda.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*a5DIpPqziD7xe-Bl3PlfsA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*exRc5dy4_F7aNJxKwc8f7Q.png" /><figcaption>but I obliged, and…</figcaption></figure><p>…now I get it. Lambda is simply a single method interface. And it makes sense too! If an interface has only one method, it is redundant to mention its name or its method’s name every time we implement it. We just need the parameters and the method body (implementation code). And the syntax of the lambda does this job perfectly.</p><p><a href="https://medium.freecodecamp.org/learn-these-4-things-and-working-with-lambda-expressions-b0ab36e0fffc">Here</a> is a very good article about this. I am not gonna explain it more.</p><h3>Back to the story</h3><p>I was searching for articles that explain this “promise” thing in more depth and I came across <a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261">this</a> article. It explains here perfectly about what “promise” does and how it works using this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d6e6c1c4ccb9c9daf4f00a9d83e15cb/href">https://medium.com/media/1d6e6c1c4ccb9c9daf4f00a9d83e15cb/href</a></iframe><p>It is the data flow of these two lines that really jumbled my mind. I understood the article perfectly; about how it works; but I couldn’t do it by own if I’d given another task regarding “promise”.</p><p>So I took my time, converted the <strong>lambda</strong>s back to java interfaces and finally figured it out what was happening.</p><h3>Converting the lambdas’</h3><p>From the above two lines, anyone can identify 3 big lambdas’ by following the <strong><em>=&gt; </em></strong>signs.</p><p>Lets take the last one first:</p><blockquote><strong><em>() =&gt; console.log(‘Hello!’)</em></strong></blockquote><p>and convert this into java interface. The only method of the resultant interface won’t have any parameter. so it goes like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/785d01ab96235492bf98a71b02aabf67/href">https://medium.com/media/785d01ab96235492bf98a71b02aabf67/href</a></iframe><p>and then the outer lambda of the first line:</p><blockquote><strong><em>time =&gt; new Promise(</em></strong><em>(resolve) =&gt; setTimeout(resolve, time)</em><strong><em>)</em></strong></blockquote><p>It take only one parameter (time). And we can get hunch from the name of the parameter that the type of this parameter will be <em>long.</em></p><p>This lambda is assigned to the variable named <em>wait. wait </em>later performs as a “promise”; so we can say that this lambda returns a Promise (another class we will have to create).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5d52ebb1a0194cc755abc8ea703729a/href">https://medium.com/media/f5d52ebb1a0194cc755abc8ea703729a/href</a></iframe><p>and now the inner lambda:</p><blockquote>(resolve) =&gt; setTimeout(resolve, time)</blockquote><p>now this is a tricky one. This one is used inside of Promise class’s constructor. So the <em>resolve </em>parameter it takes; comes from the Promise that is being constructed.</p><p>And lets try to guess what this setTimeout method will do. We can gather from its name that it will spend the it has been given; in some background thread; and then will try say that things are resolved.</p><p>But how will it convey the message all the way to the Promise? Should the method return the message? Nah…someone will have to sit out the whole time for setTimeout to be finished and get the result from it. There will be no point running this in background thread. Foreground thread will still have to wait.</p><p>What do we do?</p><blockquote>Situation like this; what we really need here is a callback =&gt; an interface =&gt;a lambda</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*D7DFVIGWTsOusaW35thjSA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*osuaq33svrLvr3rxAGsE0A.jpeg" /></figure><p>We can observe this again</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d6e6c1c4ccb9c9daf4f00a9d83e15cb/href">https://medium.com/media/1d6e6c1c4ccb9c9daf4f00a9d83e15cb/href</a></iframe><p>Wait a minute! There is another parameter which has been ignored this whole time! Ah..yes. The <em>resolve </em>parameter. This could be our savior callback. Till now we have treated the parameters as some variable like objects but they can be lambdas’ too! And this <em>resolve </em>buddy actually is an lambda. As it’s usage is only to notify the promise about the state of the task, it can be as simple as this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12eba26540eb31317125eb2e92eb70a5/href">https://medium.com/media/12eba26540eb31317125eb2e92eb70a5/href</a></iframe><p>And going back to the third lambda:</p><blockquote>(resolve) =&gt; setTimeout(resolve, time)</blockquote><p>we can now rest assured that the corresponding interface will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ea9e1ee3b551af9d220baf4ea8b24e5/href">https://medium.com/media/8ea9e1ee3b551af9d220baf4ea8b24e5/href</a></iframe><p>Now that we have converted all the lambdas’ into interfaces, lets see what full implementation will look like:</p><p><em>&lt;N.B. I have avoided the threading mechanism to keep this only about data flow. Original promise implementation will differ in many ways&gt;</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f128160479b8875bf82ffed35a9c4dac/href">https://medium.com/media/f128160479b8875bf82ffed35a9c4dac/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fc2d5f82d4b1f72427cd41803c9e1c1/href">https://medium.com/media/5fc2d5f82d4b1f72427cd41803c9e1c1/href</a></iframe><p>During the constructor call, promise will only save the ResloverInterface to use it later. When the then method gets called it saves the given ThenCallback and invokes the saved ResolverInterface; and then the real work begins. After the work is done, the PromiseStateInterface named resolve gets invoked and it invokes the saved ThenCallback. ThenCallback performs the task given for post execution (printing the “Hello!” in this case).</p><p>projects github link: <a href="https://github.com/mainstring/PromiseLearningInJava">https://github.com/mainstring/PromiseLearningInJava</a></p><p>This example only covers the <strong><em>resolve</em></strong> case of Promise. With <strong><em>reject</em></strong>, it will be more complex but the concept of the data flow remains pretty much the same.</p><p>I myself is new to this javascript culture. So, in case of any discrepancy, please let me know.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QAAOcbY4OwEV-r5FSrahkA.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a9276294935" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>