<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Hackerrank · MangoDriod</title><link>https://md.eknath.dev/tags/hackerrank/</link><description>Hackerrank updates from MangoDriod</description><language>en-us</language><atom:link href="https://md.eknath.dev/tags/hackerrank/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Wed, 25 Nov 2026 09:45:04 +0530</lastBuildDate><item><title>Chocolate Feast - Problem Solving - Hacker Rank Solution.</title><link>https://md.eknath.dev/posts/dsa/chocolate-feast-problem-solving-hackerrank/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/chocolate-feast-problem-solving-hackerrank/</guid><pubDate>Sun, 20 Feb 2022 00:04:00 +0530</pubDate><category>java</category><category>dsa</category><category>hackerrank</category><category>problem-solving</category><description>The expectation is to find the total number of chocolate one can consume by taking full advantage of the offer, Here there are 3 inputs n which holds the value of initial amount of money for buying chocolate, c is the cost price of each candy if paid by cash and m is the exchange rate for the candy.
Inputs n Initial cash to buy candy. c Cost of each candy if paid by cash. m Exchange rate for a new candy in offer. The initial count of chocolate will be the cash / cost and the wrappers in hand will be the same value of chocolate, and from there we loop through until the wrap count is less than the exchange rate, inside the loop the chocolate count will still hold the same formula as before but divided with exchange rate. The wrap count is the tricky part&amp;hellip; the wrap will be wrap / exchange rate (the no. of chocolates) + the remainder of this division (THIS IS VERY IMPORTANT) because for example if the count of wrapper is 3 and the exchange rate is 2 you can only buy 1 chocolate but the remainder 1 needs to be included or it will pop errors in the test cases.</description><content:encoded><![CDATA[<p>The expectation is to find the total number of chocolate one can consume by taking full advantage of the offer, Here there are 3 inputs <code>n</code> which holds the value of initial amount of money for buying chocolate, <code>c</code> is the cost price of each candy if paid by cash and <code>m</code> is the exchange rate for the candy.</p>
<h4 id="inputs">Inputs</h4>
<ul>
<li><strong>n</strong> Initial cash to buy candy.</li>
<li><strong>c</strong> Cost of each candy if paid by cash.</li>
<li><strong>m</strong> Exchange rate for a new candy in offer.</li>
</ul>
<p>The initial count of chocolate will be the cash / cost and the wrappers in hand will be the same value of chocolate, and from there we loop through until the wrap count is less than the exchange rate, inside the loop the chocolate count will still hold the same formula as before but divided with exchange rate. The wrap count is the tricky part&hellip; the wrap will be wrap / exchange rate (the no. of chocolates) + the remainder of this division (THIS IS VERY IMPORTANT) because for example if the count of wrapper is 3 and the exchange rate is 2 you can only buy 1 chocolate but the remainder 1 needs to be included or it will pop errors in the test cases.</p>
<h3 id="algorithm">Algorithm</h3>
<ul>
<li>declare 2 new variables chocolate and wrappers.</li>
<li>Initialize the chocolate to <code>n / c</code>.</li>
<li>Initialize the wrapper to chocolate.</li>
<li>Run a while loop while the wrapper &gt;= exchange rate (or <code>m</code>)</li>
<li>Add the value of chocolate to <code>wrapper / exchange rate</code> (or <code>m</code>)</li>
<li>Set the value of wrapper to the number of chocolates exchanged and the balance un-used wrapper.</li>
<li>return the count of chocolates consumed.</li>
</ul>
<p><strong>I Highly recommend you dont go through the below code now but try to solve it on your own to get the ability to solve problems, but im just pasting it here for those who haven&rsquo;t managed to find the solution despite many tries</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">int</span> <span style="color:#a6e22e">chocolateFeast</span>(<span style="color:#66d9ef">int</span> n, <span style="color:#66d9ef">int</span> c, <span style="color:#66d9ef">int</span> m) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> cho <span style="color:#f92672">=</span> n <span style="color:#f92672">/</span> c, wrap <span style="color:#f92672">=</span> cho;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">while</span> (wrap <span style="color:#f92672">&gt;=</span> m) {
</span></span><span style="display:flex;"><span>        cho <span style="color:#f92672">+=</span> wrap <span style="color:#f92672">/</span> m;
</span></span><span style="display:flex;"><span>        wrap <span style="color:#f92672">=</span> wrap <span style="color:#f92672">/</span> m <span style="color:#f92672">+</span> wrap <span style="color:#f92672">%</span> m;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> cho;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on February 20, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Designer PDF Viewer - HackerRank Problems</title><link>https://md.eknath.dev/posts/dsa/designer-pdf-viewer-hackerrank-problems/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/designer-pdf-viewer-hackerrank-problems/</guid><pubDate>Sat, 12 Feb 2022 21:55:00 +0530</pubDate><category>java</category><category>dsa</category><category>hackerrank</category><category>problem-solving</category><description>Difficulty: EASY Problem : The objective here is to find the size of the highlighted area, and we are given the sizes of all the alphabets, we have to find the largest alphabet in the highlighted word and then calculate the size of the rectangle so if the tallest character is 3 then the size of the box will be 3 * number of characters given. Visual representation of the selection :</description><content:encoded><![CDATA[<h5 id="difficulty-easy">Difficulty: EASY</h5>
<h5 id="problem-">Problem :</h5>
<p>The objective here is to find the size of the highlighted area, and we are given the sizes of all the alphabets, we have to find the largest alphabet in the highlighted word and then calculate the size of the rectangle so if the tallest character is 3 then the size of the box will be 3 * number of characters given. Visual representation of the selection :</p>
<p><code>[abc]</code> <code>[def]</code> <code>[ghij]</code></p>
<h4 id="inputs">Inputs</h4>
<ol>
<li>An array with the sizes of all alphabets a-z in order.</li>
<li>A String of highlighted words.</li>
</ol>
<h4 id="important-points-to-note">Important points to note</h4>
<ul>
<li><strong>The array which holds the height of each character in ascending order</strong>
which means the arrays 0th index will have the height of a 1st index will have the height of b and so on and so forth in the end the height of z will be there so it&rsquo;s easy to locate each character.</li>
<li><strong>A String with the highlighted word.</strong>
This means we have got the characters inside the rectangle, all we have to find is the tallest one and half our problem is solved, here we need some knowledge of ASCII representation of characters—if not I recommend you go through that first and then join again.</li>
<li><strong>All the alphabets inside the String are lower case.</strong>
This means the ASCII values of the characters will start from 97 so if we decrement this value from the characters of the string we will get their index.</li>
<li><strong>Only the tallest matters to us</strong>
With the value from the array&rsquo;s index, we need only the tallest in the range of our characters inside the string, so we need only the highest value. for example, if our String is <code>&quot;abc&quot;</code> and the array&rsquo;s values are <code>{5,3,1,9..}</code> we take only 5 coz it&rsquo;s the tallest of our string <strong>which</strong> is at index 0.</li>
<li><strong>We need to return the size of the rectangle.</strong>
Once we get the tallest character then we can multiply it by the length of the given String.</li>
</ul>
<h4 id="algorithm">Algorithm</h4>
<ul>
<li>Initialize a variable tallest to zero</li>
<li>Loop through the character inside the given string</li>
<li>Inside the loop Initialize a variable index</li>
<li>Set the value of the index to the value - 97 (here value is the current character)</li>
<li>Compare the value from the ArrayList.get(index) to tallest</li>
<li>Update tallest if the value is greater.</li>
<li>Return tallest multiplied words length after the loop.</li>
</ul>
<h4 id="below-is-the-code-implementation">Below is the code implementation.</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">int</span> <span style="color:#a6e22e">designerPdfViewer</span>(List <span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> h, String word) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> tallest <span style="color:#f92672">=</span> 0;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">char</span> c : word.<span style="color:#a6e22e">toCharArray</span>()) {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">int</span> index <span style="color:#f92672">=</span> c <span style="color:#f92672">-</span> 97;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (h.<span style="color:#a6e22e">get</span>(index) <span style="color:#f92672">&gt;</span> tallest)
</span></span><span style="display:flex;"><span>            tallest <span style="color:#f92672">=</span> h.<span style="color:#a6e22e">get</span>(index);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> tallest <span style="color:#f92672">*</span> word.<span style="color:#a6e22e">length</span>();
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on February 12, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Breaking the Records - HackerRank- Problem Solving in Java</title><link>https://md.eknath.dev/posts/dsa/breaking-records-hackerrank-problem/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/breaking-records-hackerrank-problem/</guid><pubDate>Sun, 30 Jan 2022 12:36:00 +0530</pubDate><category>java</category><category>dsa</category><category>hackerrank</category><category>problem-solving</category><description>Difficulty : EASY Problem : Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.
Visual representation of the table Game Score Minimum Maximum Count Min Count Max 0 12 12 12 0 0 1 24 12 24 0 1 2 10 10 24 1 1 3 24 10 24 1 1 Output : [1 , 1] Important Points to note We calculate the record determining her performance on Game 0 which will be the value of minimum and maximum. We change the value of maximum or minimum only if her score breaks our current record of maximum or minimum. in case she breaks record then we increment the counter respectively. return the Count max and Count min in the same order as a List. Algorithm inputs received : List&amp;lt;Integer&amp;gt; score, int games Set Min and Max to scores 1st element or 0th index element. Loop through the scores from the 2nd element or the 1st index till the scores.size()-1 inside the above loop check if the value of current index is larger than maximum if so then increase the counter of the maximum and set the value of the maximum to the current value. else if statement to check if the value is smaller than current minimum if so then increase the counter of the minimum and set the value of minimum to the current value outside the loop create an array and add the two values in the order of maxcount then min count and add the list to the return statement. I Highly recommend you dont go through the below code now but try to solve it on your own to build the ability to solve problems, but im just pasting it here for those who haven&amp;rsquo;t managed to find the solution despite many tries</description><content:encoded><![CDATA[<h5 id="difficulty--easy">Difficulty : EASY</h5>
<h5 id="problem-">Problem :</h5>
<p>Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.</p>
<h4 id="visual-representation-of-the-table">Visual representation of the table</h4>
<table>
<tr>
<th>Game</th>
<th>Score</th>
<th>Minimum</th>
<th>Maximum</th>
<th>Count Min</th>
<th>Count Max</th>
</tr>
<tr>
<td>0</td>
<td>12</td>
<td>12</td>
<td>12</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>24</td>
<td>12</td>
<td>24</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>10</td>
<td>10</td>
<td>24</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>24</td>
<td>10</td>
<td>24</td>
<td>1</td>
<td>1</td>
</tr>
  <tr>
    <th colspan="2">Output :</th>
    <td colspan="4">[1 , 1]</td>
  </tr>
</table>
<h3 id="important-points-to-note">Important Points to note</h3>
<ul>
<li>We calculate the record determining her performance on Game 0 which will be the value of minimum and maximum.</li>
<li>We change the value of maximum or minimum only if her score breaks our current record of maximum or minimum.</li>
<li>in case she breaks record then we increment the counter respectively.</li>
<li>return the Count max and Count min in the same order as a List.</li>
</ul>
<h3 id="algorithm">Algorithm</h3>
<ul>
<li>inputs received : <code>List&lt;Integer&gt; score, int games</code></li>
<li>Set Min and Max to scores 1st element or 0th index element.</li>
<li>Loop through the scores from the 2nd element or the 1st index till the scores.size()-1</li>
<li>inside the above loop check if the value of current index is larger than maximum if so then increase the counter of the maximum and set the value of the maximum to the current value.</li>
<li>else if statement to check if the value is smaller than current minimum if so then increase the counter of the minimum and set the value of minimum to the current value</li>
<li>outside the loop create an array and add the two values in the order of maxcount then min count and add the list to the return statement.</li>
</ul>
<p><strong>I Highly recommend you dont go through the below code now but try to solve it on your own to build the ability to solve problems, but im just pasting it here for those who haven&rsquo;t managed to find the solution despite many tries</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">breakingTheRecords</span>(List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> score) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> currentMax <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(0);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> currentMin <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(0);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> surpassed <span style="color:#f92672">=</span> 0;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> backlogged <span style="color:#f92672">=</span> 0;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> index <span style="color:#f92672">=</span> 1; index <span style="color:#f92672">&lt;</span> score.<span style="color:#a6e22e">size</span>(); index<span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (score.<span style="color:#a6e22e">get</span>(index) <span style="color:#f92672">&gt;</span> currentMax) {
</span></span><span style="display:flex;"><span>            surpassed<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>            currentMax <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(index);
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> (score.<span style="color:#a6e22e">get</span>(index) <span style="color:#f92672">&lt;</span> currentMin) {
</span></span><span style="display:flex;"><span>            backlogged<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>            currentMin <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(index);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> result <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;&gt;</span>();
</span></span><span style="display:flex;"><span>    result.<span style="color:#a6e22e">add</span>(surpassed);
</span></span><span style="display:flex;"><span>    result.<span style="color:#a6e22e">add</span>(backlogged);
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(surpassed <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34; &#34;</span> <span style="color:#f92672">+</span> backlogged);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> result;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on January 30, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item></channel></rss>