<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="http://anudhyan.org/feed.xml" rel="self" type="application/atom+xml" /><link href="http://anudhyan.org/" rel="alternate" type="text/html" /><updated>2023-01-11T00:02:39+00:00</updated><id>http://anudhyan.org/feed.xml</id><title type="html">A Glimpse of Heaven</title><subtitle>notes on life, learning and optimization.</subtitle><entry><title type="html">A Problem About Stacking Boxes</title><link href="http://anudhyan.org/2018/06/18/stacking-boxes.html" rel="alternate" type="text/html" title="A Problem About Stacking Boxes" /><published>2018-06-18T02:16:01+00:00</published><updated>2018-06-18T02:16:01+00:00</updated><id>http://anudhyan.org/2018/06/18/stacking-boxes</id><content type="html" xml:base="http://anudhyan.org/2018/06/18/stacking-boxes.html">&lt;p&gt;This post is about the solution to a algorithm puzzle involving stacking boxes. This is an
original problem proposed by me which appeared in the CMI Online Programming Contest, 2013, hosted
on HackerRank. Even though I came up with this problem independently, given that it has an intuitive
formulation and an easy solution I wouldn’t be surprised if it appeared somewhere else before.&lt;/p&gt;

&lt;p&gt;For the complete problem statement including a motivating story, constraints on parameters and
example instances please check out the problem titled ‘Boxes’ in the
&lt;a href=&quot;/assets/doc/cmi_opc_problems.pdf&quot;&gt;complete list of problems&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;problem-statement&quot;&gt;Problem Statement&lt;/h2&gt;

&lt;p&gt;The problem statement can be summarized as follows:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;We are given $N$ rectangles with integer widths and heights $(W_1, H_1), (W_2, H_2) \cdots
, (W_N, H_N)$.  Box $i$ can be stacked on top of box $j$ if $W_i &amp;lt; W_j$.  However, it is
also possible to ‘rotate’ either box by swapping the tuple $ (W_i, H_i)$. What is the most
number of boxes that can be stacked on top of each other?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Formally, in order to stack box $i$ on top of box $j$, we need $ X_i &amp;lt; X_j $ where
$ X_i \in \{W_i, H_i\} $ and $ X_j \in \{W_j, H_j\} $. We want to find the longest sequence
$ \{ i_1, i_2, \ldots , i_k\}$ where each $ i_r \in [N] $ such for each $ i_r $, we have a
fixed $ X_{i_r} \in \{ W_{i_r}, H_{i_r} \} $.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is one problem instance illustrated. Given rectangles $ 1 \times 3, 2 \times 1 $ and
$ 1 \times 2 $, we can rotate the first rectangle and create the stack $ 1 \times 2 \prec 2
\times 1 \prec 3 \times 1 $.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/boxes1.png&quot; alt=&quot;Boxes (1x3),(2x1),(1x2) Illustrated&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;some-greedy-strategies-that-dont-work&quot;&gt;Some Greedy Strategies that Don’t Work&lt;/h2&gt;

&lt;p&gt;Here are some natural greedy algorithms which would yield suboptimal solutions. These are helpful
in understanding some of the nuances of the problem.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Greedy Strategy 1&lt;/strong&gt;: Choose the box with the largest width (or height) and place it at the bottom.
Break ties arbitrarily. And continue picking the next largest box that can be placed on top of the
current stack finally discarding any boxes that didn’t get picked.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Greedy Strategy 2&lt;/strong&gt; : Choose the box with the largest width (or height) but break ties such that
the box with the smaller other dimension gets selected.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Coming up with counterexamples to strategies 1 and 2 are left as exercises to the reader. :)&lt;/p&gt;

&lt;h2 id=&quot;first-steps-what-if-rotation-wasnt-allowed&quot;&gt;First Steps: What if Rotation Wasn’t Allowed&lt;/h2&gt;

&lt;p&gt;To make some initial progress, let’s tackle a much simpler version of the problem. Imagine that
rotation is not allowed for the boxes. Now, we just have to choose the sequence of boxes so that
the following inequality holds: $ W_{i_1} &amp;lt; W_{i_2} &amp;lt; \cdots W_{i_k} $. The answer in this case
is obvious: it’s the number of distinct widths among $ N $ boxes.&lt;/p&gt;

&lt;p&gt;Armed with this simple observation, we can rephrase the original problem. For each box $(W, H)$,
we have to select either the width $(W)$ or the height $ (H) $, so that the number of distinct
numbers is maximized.&lt;/p&gt;

&lt;p&gt;From this formulation, we can immediately make some progress. If either $W$ or $H$ does
not appear as a dimension for another box, (say, $H$) then we can select $H$, add this to our
final count, and forget about this box. Thus, we can assume that now we only have boxes where both
$W$ and $H$ appear as widths or heights of other boxes.&lt;/p&gt;

&lt;p&gt;It becomes clear where the difficulty lies, making a choice ($W$ or $H$) means that choosing the
same number for another box wouldn’t make sense. Hence, somehow we have to quantify which one of
$W$ or $H$ is less ‘supported’ by other boxes.&lt;/p&gt;

&lt;h2 id=&quot;a-graphical-representation&quot;&gt;A Graphical Representation&lt;/h2&gt;

&lt;p&gt;Now that we have some structure in our problem, it may be helpful to use the language of graph
theory.&lt;/p&gt;

&lt;p&gt;Consider a bipartite graph, where the numbers are on the left hand side and boxes are on the right
hand side. Each box $(W, H)$ on the RHS has exactly two edges, one to Node $W$ and another
to Node $H$.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/boxes_graph.png&quot; alt=&quot;Corresponding Bipartite Graph for Boxes (1x3),(2x1),(1x2) Illustrated&quot; height=&quot;360px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In case $W = H$, we can just delete the box, remove the node $W (=H)$ from the graph and add
+1 to our objective. Also, if a number $X$ has only one edge corresponding to a single box, say
$(X, Y)$ then we can also take this box in our solution and delete the box and the number from
the graph. Thus, we can assume that every number on the left side has degree at least 2.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Assumption 1&lt;/strong&gt;: Every node (number) on the LHS has degree at least 2. Every node (box) on the RHS
has degree exactly 2.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The task now becomes a classical problem, finding a
&lt;a href=&quot;https://en.wikipedia.org/wiki/Matching_(graph_theory)&quot;&gt;maximum matching in a bipartite graph&lt;/a&gt;. This
graph, however, has special structure which makes finding one easier and faster than the general
case.&lt;/p&gt;

&lt;h2 id=&quot;no-numbers-left-behind&quot;&gt;No Numbers Left Behind&lt;/h2&gt;

&lt;p&gt;Now we show that, surprisingly, all remaining numbers can be successfully matched with a box. If
you saw this coming: kudos! If not, I’d suggest trying to prove this yourself before reading ahead.
:)&lt;/p&gt;

&lt;p&gt;This can be solved very quickly using
&lt;a href=&quot;https://en.wikipedia.org/wiki/Hall%27s_marriage_theorem&quot;&gt;Hall’s Marriage Theorem&lt;/a&gt;.
This is a neat theorem which says that you can find a matching saturating the LHS if every
subset $S$ on the LHS is incident on at least $|S|$ nodes on the RHS.&lt;/p&gt;

&lt;p&gt;The application is as follows. Consider any subset $S$ of the number on the LHS, then the number
of edges emanating from it must be at least $2|S|$, using &lt;em&gt;Assumption 1&lt;/em&gt;. Since every node on
the RHS has degree exactly $2$, it must be incident on at least $(2|S|)/2 = |S|$ node on the RHS.&lt;/p&gt;

&lt;p&gt;We can also prove this without invoking Hall’s Marriage Theorem. We prove by contradiction that a
maximum matching must include all nodes on the LHS.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/boxes_match.png&quot; alt=&quot;Increasing the size of the matching via Augmenting Paths&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Proof.&lt;/em&gt; Suppose we have the optimum matching M which does not include a node $X_0$ on the LHS.
Let $X_0$ be incident to box $B_0$. If $B_0$ is not matched with any node in matching
$ M $, then we can add the edge $X_0 \rightarrow B_0$ to $M$, increasing its cardinality
(contradiction!).&lt;/p&gt;

&lt;p&gt;Hence, $B_0$ must be matched with a number, say $X_1$. Since $X_1$ has degree at least $2$, it should
be incident to another box $B_1 (\neq B_0)$. In turn, $B_1$ would be matched with a number on the
LHS. If not, we can augment to add edges $(X_0 \rightarrow B_0)$ and $(X_1 \rightarrow B_1)$ and remove
$(X_1 \rightarrow B_0)$, increasing size of $M$ (contradiction!).&lt;/p&gt;

&lt;p&gt;We continue building our path in the same way:
$X_0 \rightarrow B_0 \rightarrow X_1 \rightarrow B_1 \rightarrow X_2 \rightarrow B_2 \cdots$.
At step $i$ we will find a number $X_i$ because box $B_{i - 1}$ will have a matching edge
$(B_{i - 1} \rightarrow X_i)$, or otherwise we can increase the size of the matching by alternating the
matched edges. This number $X_i$ won’t be the same as $X_0 \cdots X_{i - 1}$ because the earlier
$X_j$’s were found by matching with other $B_j$’s, or it was $X_0$ which is unmatched. Next,
$X_i$ should have another unmatched edge to $B_i$ ($\textit{deg}(X_i) \geq 2$). This $B_i$ won’t be
the same as previous $B_j$’s because previous $B_j$’s had exactly two edges and all these edges
were included in the path.&lt;/p&gt;

&lt;p&gt;Since we cannot continue building a path forever in a finite graph, the only conclusion is that $M$
is not a maximum matching. Hence, any maximum matching must include $X_0$. This concludes the proof. 
∎&lt;/p&gt;

&lt;h2 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;/h2&gt;

&lt;p&gt;We can now come up with an efficient algorithm to find the maximum
stack. After constructing the graph, we proceed as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;As long as numbers have degree 1, we can include them in the solution and delete its
corresponding box (along with the box’s edge to the other number.)&lt;/li&gt;
  &lt;li&gt;When there are no numbers with degree 1, we can include all numbers in our solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The complexity of this solution is $\mathcal{O}(N \log N)$.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defaultdict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deque&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# create bipartite graph from nums to boxes
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;degree&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;num_to_box&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defaultdict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;box&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;degree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;num_to_box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;stack_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# count of the stack we build
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;deleted_boxes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# keep track of the boxes we include in the stack
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# leaves are the nums adjacent to a single box
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;leaves&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deque&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;degree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# keep including nums from 'leaves' in our stack
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;leaves&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;leaf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;leaves&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# it's possible the leaf's box was deleted
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;degree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leaf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;stack_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# yep, we can include it in the stack!
&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# delete the leaf's supporting box since it was included
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_to_box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leaf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted_boxes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# this box was already deleted
&lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;deleted_boxes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boxes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;degree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;degree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# we might have created a new leaf
&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;leaves&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# all remaining numbers can be included since degree of each number is &amp;gt;= 2
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# and boxes all have degree 2, and we can get a matching saturating the LHS
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;stack_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;degree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack_size&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><summary type="html">This post is about the solution to a algorithm puzzle involving stacking boxes. This is an original problem proposed by me which appeared in the CMI Online Programming Contest, 2013, hosted on HackerRank. Even though I came up with this problem independently, given that it has an intuitive formulation and an easy solution I wouldn’t be surprised if it appeared somewhere else before.</summary></entry><entry><title type="html">Hello, World!</title><link href="http://anudhyan.org/meta/2018/02/02/hello-world.html" rel="alternate" type="text/html" title="Hello, World!" /><published>2018-02-02T06:40:29+00:00</published><updated>2018-02-02T06:40:29+00:00</updated><id>http://anudhyan.org/meta/2018/02/02/hello-world</id><content type="html" xml:base="http://anudhyan.org/meta/2018/02/02/hello-world.html">&lt;p&gt;Here’s to starting this blog with the archetypal introductory &lt;a href=&quot;http://en.wikipedia.org/wiki/%22Hello,_World!%22_program&quot;&gt;greeting&lt;/a&gt; in computer programming.&lt;/p&gt;

&lt;p&gt;My goals for starting this blog are twofold. Firstly, I want to understand bits of computer science and math better by writing short posts explaining what I learnt. After all, teaching
is the &lt;a href=&quot;https://lifehacker.com/the-feynman-technique-helps-you-study-faster-and-retain-1790501936&quot;&gt;best&lt;/a&gt; way to learn.&lt;/p&gt;

&lt;p&gt;Secondly, I want to improve my writing skills by practicing regularly, and I’m hoping that
having a blog would provide extra motivation to write.&lt;/p&gt;

&lt;p&gt;Here is my previous &lt;a href=&quot;http://theprocrastinator.wordpress.com&quot;&gt;attempt&lt;/a&gt; at blogging, from many years ago.&lt;/p&gt;

&lt;p&gt;I hope to have a long-running blog, full of interesting posts!&lt;/p&gt;</content><author><name></name></author><category term="meta" /><summary type="html">Here’s to starting this blog with the archetypal introductory greeting in computer programming.</summary></entry></feed>