<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://sharpnife.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://sharpnife.github.io/" rel="alternate" type="text/html" /><updated>2023-03-19T20:14:42+00:00</updated><id>https://sharpnife.github.io/feed.xml</id><title type="html">sharpnife’s blog</title><subtitle>Welcome to my blog, here I’d be sharing all sorts of weird ideas, algorithms, solutions to various kinds of problems, life lessons and much more…!</subtitle><entry><title type="html">Horse Race</title><link href="https://sharpnife.github.io/Horse-Race/" rel="alternate" type="text/html" title="Horse Race" /><published>2023-03-17T19:44:27+00:00</published><updated>2023-03-17T19:44:27+00:00</updated><id>https://sharpnife.github.io/Horse-Race</id><content type="html" xml:base="https://sharpnife.github.io/Horse-Race/">&lt;!-- &lt;h1&gt; Horse Race &lt;/h1&gt; --&gt;
&lt;p&gt;&lt;i&gt;This blog is part of the &lt;a href=&quot;/problem-of-the-month/&quot;&gt;POTM series.&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Shelly has a beautiful, muscular horse named as “The Big Dawg”, and she is invited to the annual equestrianism competition. &lt;br /&gt;
Being a smart person, she does not want to waste time going for competitions where she does not have any chance of winning, unfortunately she is very busy at the moment completing her farm project, and she turned for your help. Can you help Shelly out?
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;/assets/horse.jpeg&quot; alt=&quot;Big Dawg&quot; /&gt;
&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The problem goes like this…&lt;/p&gt;

&lt;p&gt;
There are $N$ horses taking part in the competition. Each horse has a certain strength, denoted by a positive integer. A horses' strength is a unique integer between $1$ and $N$, and each of the horse is assigned a unique track number between $1$ and $N$. 
&lt;br /&gt;
&lt;br /&gt;
The competition is divided into $\log_2 N$ rounds (It's guaranteed that $\log_2 N$ is a positive integer). 
&lt;br /&gt;
The rounds are a bit atypical, in the $i$&lt;sup&gt;th&lt;/sup&gt; round, the horse at track number $1$ and $2$ would compete against each other, and $3 \, vs \, 4$, $5 \, vs \, 6$, ... and so on.&lt;br /&gt;
If $K$ horses qualified for the $i^{th}$ round, then there will be $K / 2$ winners and $K / 2$ losers, and the winners move on to the $(i+1)^{th}$ round. The last round will be played against $2$ horses, and one of them would be crowned the champion. 
&lt;br /&gt;
&lt;br /&gt;
The winning criteria for each round is one of the two possibilities, either the stronger horse(horse which has the higher strength) defeats the weaker horse, or vice-versa.
&lt;br /&gt;
Fortunately for the equestrians, the winning conditions of each round is given before the tournament starts, and is in the form of a binary string, i.e. strings which have either &quot;$1$&quot; or &quot;$0$&quot;(like &quot;$101$&quot;).&lt;br /&gt;
If the $i^{th}$ letter is $1$, then the stronger horse wins the race, and if the $i^{th}$ letter is $0$ the weaker horse takes the win.
&lt;br /&gt;
&lt;br /&gt;
Given this information, can you figure out if Shelly could likely be crowned as the champion?&lt;sub&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;/a&gt;&lt;/sub&gt;

&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Before diving into the solution, I’d like the reader to take some time off to savor the problem and try cracking it.&lt;/p&gt;
&lt;p&gt;
It's advised to not look at the hint until you've tried everything you can.
&lt;details&gt; 
 &lt;summary&gt; Hint &lt;/summary&gt;
Shelly wants to win the tournament that means she has to win the last round. Think backtracking.
&lt;/details&gt;
&lt;!-- TODO: hints? --&gt;
&lt;br /&gt;
&lt;h3&gt; &lt;u&gt;Solution&lt;/u&gt; &lt;/h3&gt;
&lt;br /&gt;
&lt;br /&gt;
We can make a couple of basic observations, right off the bat: 
&lt;br /&gt;
&lt;br /&gt;
1. Shelly wants to win the tournament, so her horse has to win &lt;b&gt;every&lt;/b&gt; round. &lt;br /&gt;
2. Between two horses, only &lt;b&gt;relative&lt;/b&gt; strength matters.
&lt;/p&gt;

&lt;p&gt;Let’s characterize every other horse’ strength relative to Big Dawg’s(Shelly’s horse) strength. 
&lt;br /&gt;
Let $B$ be for horses which have “bigger” strength and $S$ for horses with “smaller” strength, and let Big Dawg’s strength be as $x$.&lt;/p&gt;

&lt;p&gt;What more can we do? Hmm, we want Shelly to win the tournament, so she must win the last round. Let’s backtrack our way from the end of the tournament to the start and see what goes on.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
Let’s go through an example, given:-&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;N = 8 (number of horses/equestrians)
winning condition = &quot;001&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;There will be $3$ rounds in this tournament …&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;For the $3rd$ round, the winning condition is $1$, i.e., the horse with the “bigger” strength wins.
&lt;br /&gt;
So for Shelly to win the $3rd$ round, her horse must fight with a horse with smaller strength.&lt;/p&gt;

&lt;p&gt;$3rd$ round: 
&lt;br /&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;      x   S   - 3rd round (x vs S)
       \ /
        x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;Let’s see what happens for the $2nd$ round, winning condition is $0$, so horse with smaller strength wins.&lt;/p&gt;

&lt;p&gt;$2nd$ round:
&lt;br /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   x    B  S    O - 2nd round (x vs B, S vs O)
    \  /    \  / 
      x      S   - 3rd round
       \    /
         x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Shelly must fight with a “bigger” horse to win the round. 
&lt;br /&gt;
Note that the $S$ horse in the $3rd$ round, must win the $2nd$ round to reach the $3rd$ round. The $S$ horse has an advantage though, since it’s smaller it can fight against $B$ or $S$(another horse with smaller relative strength) and in both cases the smaller horse wins. It does not matter which horse amongst the smaller ones win, we just want one of the small ones to win. We denote such a horse with $O$ to signify either a bigger or smaller horse.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;For the $1st$ round, the winning condition is $0$, so the smaller horse wins.
&lt;br /&gt;
&lt;br /&gt;
$1st$ round:
&lt;br /&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; x    B  B    B  S    O  O    O - 1st round (x vs B, B vs B, S vs O, O vs O)
  \  /    \  /    \  /    \  /
    x       B       S       O - 2nd round
    \    /           \    / 
       x               S   - 3rd round
       \              /
         \          /
           \      /
              x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Notice that for the $O$ in the $2nd$ round, in the $1st$ round the opponent for $O$ is also $O$ as it does not matter who wins the round and similary for the $S$. But for $B$, it has to fight against any other bigger horse in the $1st$ round to guarantee its place in the $2nd$ round.
&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;
There are a few observations we can make from the simulation we just did: 
&lt;br /&gt;
&lt;br /&gt;

1. In the first round we end up with $3 Bs$ and $1 S$ ($Os$ don't matter), which implies that for Shelly to win the tournament there must be atleast $3$ equesterians who have horses with bigger strength than Shelly's horse and atleast $1$ equestrian with a weaker horse. &lt;br /&gt;
&lt;br /&gt;
2. When the winning condition is $0$ for the $i^{th}$ round, we can see that in the round $(i - 1)$: 
&lt;br /&gt;
&lt;br /&gt;
    - For $x$ to win, $x \, vs \, B$ always has to happen &lt;br /&gt;
    - For $B$ to win, $B \, vs \, B$ always has to happen &lt;br /&gt;
    - For $S$ to win, $S \, vs \, O$ &lt;br /&gt;
       ($S$ will always win so opponent does not matter)
&lt;br /&gt;
&lt;br /&gt;
3. Similary for the winning condition $1$, following happens: &lt;br /&gt;
    - For $x$ to win, $x \, vs \, S$ always has to happen &lt;br /&gt;
    - For $B$ to win, $B \, vs \, O$ &lt;br /&gt;
       ($B$ will always win so opponent does not matter) &lt;br /&gt;
    - For $S$ to win, $S \, vs \, S$ always has to happen  &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Looks like we get a new $B$ in the $(i - 1)^{th}$ round whenever the winning condition is $0$.
&lt;br /&gt;
We can write it formally as…&lt;/p&gt;

&lt;p&gt;\(F(b) = F(b - 1) + F(b - 1) + 1\)
&lt;br /&gt;
\(F(b) = 2 \cdot F(b - 1) + 1\)
&lt;br /&gt;
&lt;br /&gt;
Where $F(b)$ is the number of $B$s at a particular round having winning condition as $0$, and $F(b - 1)$ is the number of $Bs$ at the previous round which had the same winning condition. 
&lt;br /&gt;
Since, each $B$ in the previous round would produce a new $B$, we get $2\cdot F(b - 1)$ and the $1$ is for the $B$ which is produced by $x$.
&lt;br /&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; x    B  B    B  S    O  O    O - 1st round -&amp;gt; b = 2, F(b) = 2 * F(b - 1) + 1 = 2 * F(1) + 1 = 3
  \  /    \  /    \  /    \  /
    x       B       S       O - 2nd round -&amp;gt; b = 1, F(b) = 2 * F(b - 1) + 1 = 2 * F(0) + 1 = 1
    \    /           \    / 
       x               S   - 3rd round -&amp;gt; b = 0, F(0) = 0
       \              /
         \          /
           \      /
              x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
If $F(0) = 0$, then
\(F(b) = 2^b - 1, \forall b \in \mathbf{N}\)&lt;/p&gt;

&lt;p&gt;It follows that, for Shelly to win the tournament: the total number of $Bs$ required is atleast $2^b - 1$, where $b$ stands for the number of rounds having winning condition as $0$.
And similarly for $S$, the total number of $Ss$ required is $2^s - 1$, where $s$ stands for the number of rounds having winning condition as $1$.
&lt;br /&gt;
&lt;br /&gt;
Hence, Shelly can win the tournament if there are atleast $2^b - 1$ stronger horses and $2^s - 1$ weaker horses.
&lt;br /&gt;
&lt;br /&gt;
So for the given winning condition $001$, Shelly must have atleast $2^2 - 1 = 3$ stronger horses and $2^1 - 1 = 1$ weaker horse. 
From which it follows that, if Big Dawg’s strength is one of $\set{2, 3, 4, 5}$ then Shelly can possibly win the race!&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
Bonus problem: What is the expected probability of Shelly winning the tournament?&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;
&lt;p id=&quot;1&quot;&gt;
[1] - The problem is a variant of a &lt;a href=&quot;https://codeforces.com/problemset/problem/1767/D&quot;&gt;codeforces problem.&lt;/a&gt;
&lt;/p&gt;</content><author><name></name></author><summary type="html">Equestrianism</summary></entry><entry><title type="html">Problem of the month series</title><link href="https://sharpnife.github.io/problem-of-the-month/" rel="alternate" type="text/html" title="Problem of the month series" /><published>2022-06-26T13:51:50+00:00</published><updated>2022-06-26T13:51:50+00:00</updated><id>https://sharpnife.github.io/problem-of-the-month</id><content type="html" xml:base="https://sharpnife.github.io/problem-of-the-month/">&lt;!-- &lt;h1&gt; &lt;u&gt; Problem of the month series &lt;/u&gt; &lt;/h1&gt; --&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Starting from this month, i.e. June 2022, I’m planning to post a series of blogs on one of the most interesting problem which I would have solved during that month. The problems(atleast for now) would be of an algorithmic nature, in the sense that the solution to those problems would involve coming up with some logical steps to solve it. 
&lt;br /&gt;
And as always, all my dear readers are encouraged to try the problems themselves before looking at the solution. :D
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Update(28-01-2023)&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;I got occupied with other things in life and work, got addicted to playing league of legends(started one-tricking Rek'sai), and I didn't(or couldn't) make time to solve problems, much of my free time was spent on YT and league. Also another big reason for procrastinating was, I was trying to crack a certain hard problem, I made a bit of progress but then I quickly hit a roadblock, and I was stubborn and adamant to solve that particular problem over trying other new ones, which made me lose time. Since I wasn't making progress in my chosen problem, it demotivated me and I eventually stopped thinking about it. In retrospective, I probably should've been more flexible and tried other problems, which probably would've given me new ideas to crack the other one. 
Anyways, this is life, we fail sometimes, we succeed sometimes, but what matters is that we stand up and keep moving forward(and also learn from your past mistakes, so you don't repeat them). 
 &lt;/p&gt;
&lt;p&gt;So, with new energy and enthusiasm, I’m restarting this intiative from this year(2023). Onwards conqueror!&lt;/p&gt;</content><author><name></name></author><summary type="html"></summary></entry><entry><title type="html">First word in the dictionary</title><link href="https://sharpnife.github.io/First-word-in-the-dictionary/" rel="alternate" type="text/html" title="First word in the dictionary" /><published>2022-06-26T13:51:50+00:00</published><updated>2022-06-26T13:51:50+00:00</updated><id>https://sharpnife.github.io/First-word-in-the-dictionary</id><content type="html" xml:base="https://sharpnife.github.io/First-word-in-the-dictionary/">&lt;!-- &lt;h1&gt; &lt;u&gt; First word in the dictionary &lt;/u&gt; &lt;/h1&gt; --&gt;
&lt;p&gt;&lt;i&gt;This blog is part of the &lt;a href=&quot;/problem-of-the-month/&quot;&gt;POTM series.&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;

&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;/h2&gt;

&lt;p&gt;Given two strings find the first word occuring in the dictionary made from the given strings.&lt;sub&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;/a&gt;&lt;/sub&gt;
A word is said to be in the dictionary if it can be formed using the following steps:
&lt;br /&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1. Pick one of the first characters from either of the strings.
2. Append it to the end of the word(which we're creating).
3. Delete that character from the string.
4. Repeat the above steps until all the characters are taken.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Example: 
String A = &quot;JA&quot;
String B = &quot;RA&quot;
Dictionary = [&quot;JARA&quot;, &quot;JRAA&quot;, &quot;RAJA&quot;, &quot;RJAA&quot;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Amongst the words in the dictionary, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;JARA&quot;&lt;/code&gt; is the first word in the dictionary.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
Can you think of a way to find the first word in the dictionary given two arbitrary strings?
&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;

&lt;p&gt;The naive way to find the first word is to generate all possible words, sort the list and take the first word.
But that’s boring and time consuming, let’s think of a simpler way to find it. 
&lt;br /&gt;
&lt;br /&gt;
One trivial(yet important) observation is that, we always have atmost two choices when picking the $i^{th}$ letter and it’s always better to take the letter which comes first in the alphabetical order. 
&lt;br /&gt;
For example, let’s say the choice is between $A$ and $C$, if we take $A$ at this position, all the subsequent letters which would be taken would come after $A$ and since all the words with $A$ would come before all the words with $C$, we took the best possible letter at the $i^{th}$ position.
&lt;br /&gt;
So, whenever we have a choice between two dissimilar letters we always take the one which comes first in the alphabetical order. 
&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;But what about when we’ve a choice between two $As$? 
&lt;br /&gt;
Well in that case, we’ve to “cheat” a little bit, we’d go down both the strings and look at the position(let’s call it the $j^{th}$ position), where we hit the first pair of dissimilar letters and take the $A$ from the string which has the least of the two values at the $j^{th}$ position, and if there’s no such $j$ we can pick $A$ from either of the strings.&lt;sub&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;/a&gt;&lt;/sub&gt;
&lt;br /&gt;
&lt;br /&gt;
It’s like when we hit a crossroad and we don’t know which way to go, so we step out of the car walk a few metres and ask one of the villagers which road to take.
The reason this technique works, is because when we hit the first choice of dissimilar letters we took the fastest route to that position.
&lt;br /&gt;
We repeat the above given algorithm as long as there are letters in the strings, and our algorithm terminates when we’ve no more choices left and at this point we would have successfully generated the first word in the dictionary. :D 
&lt;br /&gt;&lt;/p&gt;

&lt;details&gt;
&lt;summary&gt; Code(in Haskell)&lt;/summary&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;get_first_word&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;get_first_word&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;get_first_word&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;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&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;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; 
                     &lt;span class=&quot;kr&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_first_word&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                     &lt;span class=&quot;kr&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; 
                     &lt;span class=&quot;kr&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_first_word&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;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt; 
                     &lt;span class=&quot;kr&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peek_forward&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ys&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;kr&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_first_word&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                           &lt;span class=&quot;kr&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_first_word&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;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;peek_forward&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&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;n&quot;&gt;peek_forward&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&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;n&quot;&gt;peek_forward&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&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;peek_forward&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;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&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;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
                           &lt;span class=&quot;kr&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
                           &lt;span class=&quot;kr&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peek_forward&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Example : &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- get_first_word &quot;JA&quot; &quot;RA&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- &quot;JARA&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/details&gt;

&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;

&lt;p id=&quot;1&quot;&gt;
[1] - The problem is a slight variation of the &lt;a href=&quot;https://www.hackerrank.com/challenges/morgan-and-a-string/problem&quot;&gt;hackerrank problem.&lt;/a&gt;
&lt;/p&gt;
&lt;p id=&quot;2&quot;&gt;
[2] - We can formally prove this using the exchange argument which I'd like to leave as an exercise for the reader.
&lt;/p&gt;</content><author><name></name></author><summary type="html">Which is the first word?</summary></entry><entry><title type="html">Why you should hire me</title><link href="https://sharpnife.github.io/why-you-should-hire-me/" rel="alternate" type="text/html" title="Why you should hire me" /><published>2022-06-02T13:08:40+00:00</published><updated>2022-06-02T13:08:40+00:00</updated><id>https://sharpnife.github.io/why-you-should-hire-me</id><content type="html" xml:base="https://sharpnife.github.io/why-you-should-hire-me/">&lt;p&gt;“The second half of a man’s life is made up of nothing but the habits he has acquired during the first half.” 
&lt;br /&gt;
- Fyodor Dostoevsky&lt;/p&gt;

&lt;p&gt;Learning and being curious are the bread and butter for being good at anything.
The technical skills one needs can be learned and forgotten quite easily, but a man’s attitude towards the problems he faces is what sets him apart from his fellow men. And a man’s attitude is formed through his habits and beliefs, and it’s a very hard thing to change ones’ core self!
&lt;br /&gt;
&lt;br /&gt;
Keeping that in mind, I’d like to go over a few of the character traits that I have developed thanks to the environment I was crafted in.&lt;/p&gt;

&lt;h1&gt; Passionate Learner &lt;/h1&gt;

&lt;p&gt;I was fortunate enough to have parents who stressed on the importance of self study, which implanted in me the love for discovery, exploration and problem solving, and the need and importance for deeper understanding. As a result, in my senior high school’s final exam I was one of the top scoring student amongst my fellow schoolmates most of whom had private tutors/coaching classes. 
The learning skils that I had acquired during my school times translated very well into my college as well as professional life where I’ve been assigned projects on technologies that I’ve never heard/used and it surprises my colleagues when I tell them that this was my first time working on xyz. :P
&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;

&lt;details&gt; 
 &lt;summary&gt; Story &lt;/summary&gt;
  Recently I was involved in a project where the client wanted to include a payment system into their website, our team decided to implement the system using stripe. I was a shadow resource in the project initially, and my job was to create a POC(proof of concept) for the proposed solution. 
&lt;br /&gt;
As I didn't have any experience with stripe, well actually I had never even heard of stripe, I decided to read through the docs, API specifications and study the stripe system(which btw is one of the best api docs I've read, kudos to the stripe team!). 
&lt;br /&gt;
After I got some understanding of how stripe works it was clear that the proposed system was not going to be a good long term solution(it was a plugin which was maintained by a third party) and had few other drawbacks, so I proposed a simpler system using Stripe Checkout where most of the heavy work(plus the checkout UI) would be handled by stripe itself.
&lt;br /&gt;
At the end it came out wonderfully, the client was very pleased with how the
payment flow worked and my PM was very surprised knowing that this was my first time working with stripe. :P
&lt;/details&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h1&gt; Obsessive Problem Solver &lt;/h1&gt;

&lt;p&gt;It’s not uncommon for my family to see me staring at the ceiling for long periods of time, which mostly signals that I’m working on a problem, destructuring it and finding clues, patterns to solve it. It’s one of my favorite activities!
The kick that one gets from solving a problem is almost like a spiritual experience, especially if it was a challenging problem.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h1&gt; Loves Challenges &lt;/h1&gt;

&lt;p&gt;This shouldn’t come as a surprise if you’ve read the first two points.
&lt;br /&gt;
I have a competitive programming background, I used to spend quite a lot of time doing CP during my college days, practicing hard problems and thinking about them when I’m doing something else(nightly walks, during lectures, talking a dump etc).
&lt;br /&gt;
CP kind of shaped my personality in some sense, it gave me the confidence and the desire to seek and solve hard problems. Ofcourse the more defined and clear the problem is the more enjoyable it is to find a solution. It’s sort of like trekking but with a dishevelled map.&lt;/p&gt;</content><author><name></name></author><category term="misc" /><summary type="html">“The second half of a man's life is made up of nothing but the habits he has acquired during the first half.” - Fyodor Dostoevsky</summary></entry><entry><title type="html">Years of “Experience”</title><link href="https://sharpnife.github.io/years-of-experience/" rel="alternate" type="text/html" title="Years of “Experience”" /><published>2022-06-02T13:08:40+00:00</published><updated>2022-06-02T13:08:40+00:00</updated><id>https://sharpnife.github.io/years-of-experience</id><content type="html" xml:base="https://sharpnife.github.io/years-of-experience/">&lt;p&gt;It never makes much sense to me when I see hiring posts requiring $X$ years of experience on something, some framework, some tech. One could have years of experience and still be incompetent to provide good, efficient solutions. 
People are different, some people can understand a given system very fast and so their just $1$ year of “experience” would probably be equal to someone else’s $5$ years of experience. And also the quality of the experience matters, let’s say someone said they have $5$ years of experience doing C++ but all they did was maintain the company’s codebase, fixing trivial bugs and such while the other guy with the same amount of experience built multiple low-level scalable applications and systems. 
&lt;br /&gt;
The richness of the experience of the latter exceeds than that of the former. 
&lt;br /&gt;
Quality over quantity.&lt;/p&gt;</content><author><name></name></author><category term="misc" /><summary type="html">It never makes sense to me when I see...</summary></entry><entry><title type="html">Champernowne constant</title><link href="https://sharpnife.github.io/champernowne-constant/" rel="alternate" type="text/html" title="Champernowne constant" /><published>2022-06-02T13:08:40+00:00</published><updated>2022-06-02T13:08:40+00:00</updated><id>https://sharpnife.github.io/champernowne-constant</id><content type="html" xml:base="https://sharpnife.github.io/champernowne-constant/">&lt;!-- &lt;h1&gt;&lt;u&gt;Champernowne constant&lt;/u&gt;&lt;/h1&gt; --&gt;

&lt;p&gt;Champernowne constant(in base $10$) is : $0.12345678910…$
&lt;br /&gt;
One can see the pattern quite easily, it’s a real number with the fractional part as the concatenation of successive integers.
&lt;br /&gt;
Suppose we want to find the $N^{th}$ digit of this infinite decimal expansion, what can we do?&lt;sub&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;/a&gt;&lt;/sub&gt;
&lt;br /&gt;
I’d highly recommend the reader to think about the problem before reading further.&lt;/p&gt;

&lt;p&gt;
The simplest way would be to iterate over the integers while keeping track of the number of digits so far.

&lt;details&gt;
&lt;summary&gt;Code&lt;/summary&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find_Nth_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cur&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;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&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;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;true&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_of_digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&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;cur&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;else&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;get_ith_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;num&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;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/details&gt;
&lt;/p&gt;

&lt;p&gt;
This approach isn't too favorable when $N$ becomes huge, say, $N &amp;gt; 10^{18}$. 
&lt;br /&gt;
If we look at the natural numbers we can see a pattern which we can exploit to create a better algorithm.
&lt;br /&gt;
&lt;br /&gt;
The first $9$ numbers have $1$ digit each. &lt;br /&gt;
The next $90$ numbers have $2$ digits each. &lt;br /&gt;
The next $900$ numbers have $3$ digits each. &lt;br /&gt;
... &lt;br /&gt;
And so on. &lt;br /&gt;
&lt;br /&gt;

We can write it out as a polynomial, $F(D)$. &lt;br /&gt;

$F(D)$ = number of digits upto $10 ^ D - 1$ &lt;br /&gt;
        i.e integers having &amp;lt;= $D$ digits
&lt;br /&gt;
&lt;br /&gt;
$F(D) = (9 \times \!1) + (90 \times 2) \: + ... \: + (9 \times \! 10 ^ {(D - 1)} \times D)$
&lt;br /&gt;
&lt;br /&gt;
We can binary search on the value of $D$ and then binary search again(if needed) to find the Nth digit amongst the $(D + 1)^{th}$ digit numbers. This reduces the complexity of the algorithm by a lot, and we have something in the form of $O(\log N)$ time complexity.
&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;
&lt;p id=&quot;1&quot;&gt;
[1] - The problem is a slight variation of the &lt;a href=&quot;https://projecteuler.net/problem=40&quot;&gt;project euler problem.&lt;/a&gt;
&lt;/p&gt;</content><author><name></name></author><category term="algorithms," /><category term="maths" /><summary type="html">0.123456789...</summary></entry><entry><title type="html">What is Life?</title><link href="https://sharpnife.github.io/what-is-life/" rel="alternate" type="text/html" title="What is Life?" /><published>2017-10-22T13:08:40+00:00</published><updated>2017-10-22T13:08:40+00:00</updated><id>https://sharpnife.github.io/what-is-life</id><content type="html" xml:base="https://sharpnife.github.io/what-is-life/">&lt;!-- &lt;h1&gt;&lt;u&gt; What is Life? &lt;/u&gt;&lt;/h1&gt; --&gt;

&lt;p&gt;It all started with a Big Bang, that’s the opening title of the famous comedy-nerdy show “The Big Bang Theory”. But that’s how the universe was born with a — Big Bang (atleast according to scientists). And the universe is expanding, but what about us? We come to this earth from our mother’s womb, filled with curiosity around pondering on what’s going around us. At around age 4 or 5 we are sent to schools, then college, then we become the slaves of the modern-wage system doing a typical 9 to 5 jobs.&lt;/p&gt;

&lt;p&gt;Slowly and slowly, this natural curiosity which was in us, initially, dies within us because we begin to accept things around us as it is. And the charms of money and fame try to embrace our lives. Rarely, do we stop and think- What am I doing? Where do I come from? What is the meaning of life?&lt;/p&gt;

&lt;p&gt;All these questions bothered me from my teenage life. Some say that happiness is life’s purpose, some others say relationships are life’s real purpose, some say that doing God’s will is life’s purpose.&lt;/p&gt;

&lt;p&gt;But to me, I guess the life’s real purpose is to be curious and continue searching the truth in everything. Just continue to be curious, look at the stars and wonder about their origin, look at the trees, look at the small creeping creatures and enjoy them moving, while continuing to think about their behavior and so on.&lt;/p&gt;

&lt;p&gt;To me that’s the meaning of life and that’s what gave birth to the scientific era and finally the technological revolution which we are experiencing now! And who knows, maybe this curious attitude will lead you to become the next Nobel Laureate!!&lt;/p&gt;</content><author><name></name></author><category term="misc" /><summary type="html">It all started with a Big Bang...</summary></entry></feed>