<?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>DSA · MangoDriod</title><link>https://md.eknath.dev/categories/dsa/</link><description>DSA updates from MangoDriod</description><language>en-us</language><atom:link href="https://md.eknath.dev/categories/dsa/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Wed, 25 Nov 2026 09:45:04 +0530</lastBuildDate><item><title>Linked List in Java - Collections Framework- DSA</title><link>https://md.eknath.dev/posts/dsa/linked-list-in-java-collections/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/linked-list-in-java-collections/</guid><pubDate>Wed, 23 Feb 2022 13:02:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><category>linkedlist</category><description>Gist: A Linked list is dynamic array where a group of objects can be stored while following the insertion order and allow duplicates, the 2 types of linked list are singly and doubly linked lists, the default one is singly linked list where the elements can be traversed only in forward direction this is because unlike array here the elements are stored at random locations and only the node of each element knows the address of the next element. A simple example would be our circuit board here a point X may be connected to Y and Y connected to point Z here the insertion order is preserved and we can know the next point only by following the signals from the previous point.</description><content:encoded><![CDATA[<h3 id="gist">Gist:</h3>
<p>A Linked list is dynamic array where a group of objects can be stored while following the insertion order and allow duplicates, the 2 types of linked list are singly and doubly linked lists, the default one is singly linked list where the elements can be traversed only in forward direction this is because unlike array here the elements are stored at random locations and only the node of each element knows the address of the next element. A simple example would be our circuit board here a point X may be connected to Y and Y connected to point Z here the insertion order is preserved and we can know the next point only by following the signals from the previous point.</p>
<p>Same as the example but in Linked list each element has 2 parts in singly linked list where 1 part is the node with the address to the next node and the other which is the actual data of that element like a post letter the envelope has the address and inside the contents. but in doubly linked list there are 3 parts 2 of them are nodes which has the address of previous and next element and the other one is the actual data of that element.</p>
<p>The insertion or deletion are in constant time so compared to ArrayList the mega shifting process will be avoided.</p>
<h3 id="hierarchical-order">Hierarchical order</h3>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/linked-list-in-java-collections-1.png"></p>
<h3 id="type-of-constructors">Type of constructors</h3>
<ul>
<li>Empty argument constructor or the default constructor is the same as invoking any other object here a new linked list object will be created, do note that there is no scope for reserving memory for the number of elements as we add new element the size is maintained dynamically a new LinkedList is an empty list.</li>
</ul>
<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>LinkedList list <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkedList();
</span></span></code></pre></div><ul>
<li>Conversion of other Collections like ArrayList, Vector, etc&hellip;to a LinkedList is possible with the following constructor.</li>
</ul>
<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>LinkedList arraysFromVector <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkedList( VariableNameOfTheCollection );
</span></span><span style="display:flex;"><span><span style="color:#75715e">//here any collection will be converted into a LinkedList</span>
</span></span></code></pre></div><h3 id="important-points">Important Points</h3>
<ul>
<li>By default an empty LinkedList is created.</li>
<li>Is Dynamically Growable and Shrinkable.</li>
<li>Heterogeneous Objects are allowed.</li>
<li>There is no initial capacity</li>
<li>No load factor is applicable here.</li>
<li>Order of insertion is preserved</li>
<li>We can also use generic data-type such that the list acts like a homogenous collection.</li>
<li>Random access interface is not implemented.</li>
<li>Ideal if the operations are Insertion or deletion.</li>
</ul>
<h3 id="implementing-interfaces">Implementing Interfaces</h3>
<ul>
<li>Serializable</li>
<li>Cloneable</li>
<li>Iterable &lt;E&gt;</li>
<li>Collection &lt;E&gt;</li>
</ul>
<h3 id="methods-available-to-only-the-linkedlist-object">Methods available to only the LinkedList object.</h3>
<ul>
<li>.addFirst(Element) -&gt; adds the element in the front of the LinkedList.</li>
<li>.addLast(Element) -&gt; add an element in the last part of the LinkedList.</li>
<li>getFirst() &ndash;&gt; returns the Object at the front of the Linked List.</li>
<li>getLast() &ndash;&gt; returns the last object in the linked list.</li>
<li>removeFirst() -&gt; returns the object in the first and removes it from the linked list</li>
<li>removeLast() &ndash;&gt; returns the last objects and removes the object from the linked list.</li>
</ul>
<table>
  <thead>
      <tr>
          <th>Pros</th>
          <th>Cons</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ideal if insertion or deletion operations are more.</td>
          <td>Worst if there are lots of search operations as it needs to iterate through all the other elements.</td>
      </tr>
      <tr>
          <td>By default the elements can be traversed through forward direction.</td>
          <td>Java LinkedList class is non-synchronized.</td>
      </tr>
      <tr>
          <td>Can hold duplicate elements.</td>
          <td>Does not implement random access interface.</td>
      </tr>
      <tr>
          <td>Can hold null values.</td>
          <td></td>
      </tr>
      <tr>
          <td>The process of insertion or deletion is simple and fast.</td>
          <td></td>
      </tr>
  </tbody>
</table>
<hr>
<p><em>Note: This article was originally published on Blogger on February 23, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><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>Collection Interface - Java Collections Framework - DSA</title><link>https://md.eknath.dev/posts/dsa/collection-interface-in-java-dsa/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/collection-interface-in-java-dsa/</guid><pubDate>Fri, 11 Feb 2022 17:59:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><description>Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example add() to add an element, size() to get the size and much more, below is a table of most common methods.</description><content:encoded><![CDATA[<p>Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example <code>add()</code> to add an element, <code>size()</code> to get the size and much more, below is a table of most common methods.</p>
<h4 id="hierarchy-of-the-collection-interface">Hierarchy of the Collection Interface.</h4>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/collection-interface-in-java-dsa-1.jpg"></p>
<p>The Parent of Collection Interface is <strong>Iterable</strong> Interface and the Collection is the base interface for List Interface, Set Interface and Queue Interface, the respective classes which implement either of the sub interfaces will also implement the defined methods from the Collection Interface, below are some of the commonly used methods.</p>
<h4 id="defined-methods">Defined Methods:</h4>
<table>
  <thead>
      <tr>
          <th>Method</th>
          <th>Description</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>add()</td>
          <td>This method returns a Boolean value true if it inserts the specified element in this collection.</td>
      </tr>
      <tr>
          <td>addAll()</td>
          <td>This method returns a Boolean value true if all elements were added successfully.</td>
      </tr>
      <tr>
          <td>clear()</td>
          <td>Removes all the elements of the collection.</td>
      </tr>
      <tr>
          <td>contains()</td>
          <td>returns a Boolean value if the specified object exists true else false.</td>
      </tr>
      <tr>
          <td>containsAll()</td>
          <td>returns a Boolean value if the specified Objects from the argument of collection and if all the elements exist inside the collection then it returns true else false.</td>
      </tr>
      <tr>
          <td>equals()</td>
          <td>returns true if the specified Object is equal to this current Collection.</td>
      </tr>
      <tr>
          <td>hashCode()</td>
          <td>returns the hash code value of this collection.</td>
      </tr>
      <tr>
          <td>isEmpty()</td>
          <td>returns true if this collection has no elements otherwise returns false.</td>
      </tr>
      <tr>
          <td>iterator()</td>
          <td>returns an Iterator over the elements in proper sequence.</td>
      </tr>
      <tr>
          <td>remove()</td>
          <td>Removes the first element where the specified element is found, but only the 1st element of the specified object will be removed.</td>
      </tr>
      <tr>
          <td>removeAll()</td>
          <td>same like remove() but all objects matching will be removed from this collection.</td>
      </tr>
      <tr>
          <td>removeIf()</td>
          <td>It removes all the elements of this collection that satisfy the given predicate filter.</td>
      </tr>
      <tr>
          <td>retainAll()</td>
          <td>The Objects inside the arguments collection will be kept and all other Objects will be removed from this collection.</td>
      </tr>
      <tr>
          <td>size()</td>
          <td>This returns the number of objects present inside this collection.</td>
      </tr>
      <tr>
          <td>spliterator()</td>
          <td>It returns a spliterator over the elements in this collection.</td>
      </tr>
      <tr>
          <td>toArray()</td>
          <td>It returns an array containing all the elements of this collection which are in proper sequence.</td>
      </tr>
  </tbody>
</table>
<h4 id="classes-that-implements-collection">Classes that implements Collection</h4>
<p><code>ArrayList</code> , <code>LinkedList</code> , <code>Vector</code> , <code>Stack</code> , <code>PriorityQueue</code> , <code>ArrayDeque</code> , <code>TreeSet</code> , <code>HashSet</code> , <code>LinkedHashSet</code></p>
<p>Which means all the above methods in the table can be used from the objects of these classes which implement Collection interface, along with these different collections have their own interfaces they implement depending on the underlying collections.</p>
<h4 id="sample-code">Sample Code:</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:#f92672">import</span> java.util.*;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">TestJavaCollection1</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String args<span style="color:#f92672">[]</span>) {
</span></span><span style="display:flex;"><span>        ArrayList<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span> list <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span>(); <span style="color:#75715e">// Creating arraylist</span>
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ravi&#34;</span>); <span style="color:#75715e">// Adding object in arraylist</span>
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Vijay&#34;</span>);
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ravi&#34;</span>);
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ajay&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Traversing list through Iterator</span>
</span></span><span style="display:flex;"><span>        Iterator<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span> itr <span style="color:#f92672">=</span> list.<span style="color:#a6e22e">iterator</span>();
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">while</span> (itr.<span style="color:#a6e22e">hasNext</span>()) {
</span></span><span style="display:flex;"><span>            System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(itr.<span style="color:#a6e22e">next</span>());
</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>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// output</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ravi</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Vijay</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ravi</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ajay</span>
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on February 11, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Array List - Collections Framework in Java - DSA</title><link>https://md.eknath.dev/posts/dsa/array-list-of-collections-in-java-dsa/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/array-list-of-collections-in-java-dsa/</guid><pubDate>Fri, 11 Feb 2022 10:15:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><category>arraylist</category><description>Gist: An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector.</description><content:encoded><![CDATA[<h3 id="gist">Gist:</h3>
<p>An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector.</p>
<h3 id="hierarchical-order">Hierarchical order</h3>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/array-list-of-collections-in-java-dsa-1.jpg"></p>
<h3 id="type-of-constructors">Type of constructors</h3>
<ul>
<li>Empty argument constructor or the default constructor is the same as invoking any other object here a new ArrayList is created with a default size of 10. Below is the most commonly used constructor by beginners and others alike.</li>
</ul>
<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>ArrayList array <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList();
</span></span><span style="display:flex;"><span><span style="color:#75715e">//array has a capacity of 10</span>
</span></span></code></pre></div><ul>
<li>The default constructor above will allot only 10 slots but if you want the initial size to be 20 or 1000 you can do so with the following constructor this is ideal when you know the number of entries expected, compared to the default here you will save a lot of performance. since this part is important I will stretch it with an example.</li>
</ul>
<p>If you are preparing to insert 100 inputs by calling the default constructor, then every time the ArrayList reaches its capacity it will clone itself and grow to a certain capacity and this internal task will take a toll on your performance so instead if you already set the capacity while creating the array list this internal process of growing can be avoided thus saving a lot of performance and time.</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>ArrayList arrayOfPages <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList(380);
</span></span><span style="display:flex;"><span><span style="color:#75715e">//array has a capacity of 380</span>
</span></span></code></pre></div><ul>
<li>Conversion of other Collections like LinkedList, Vector, etc&hellip;to an ArrayList is possible with the following constructor.</li>
</ul>
<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>ArrayList arraysFromVector <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList( VariableNameOfTheCollection );
</span></span><span style="display:flex;"><span><span style="color:#75715e">//here any collection will be converted into an ArrayList</span>
</span></span></code></pre></div><h3 id="important-points">Important Points</h3>
<ul>
<li>Is Dynamically Growable and Shrinkable.</li>
<li>Heterogeneous Objects are allowed.</li>
<li>The initial capacity is 10</li>
<li>Load Factor is 0.75</li>
<li>Order of insertion is preserved</li>
<li>We can also use generic data-type such that the array acts like a homogenous.</li>
</ul>
<h3 id="implementing-interfaces">Implementing Interfaces</h3>
<ul>
<li>Serializable</li>
<li>Cloneable</li>
<li>RandomAccess</li>
<li>Iterable &lt;E&gt;</li>
<li>Collection &lt;E&gt;</li>
<li>List &lt;E&gt;</li>
</ul>
<h3 id="commonly-used-methods-from-list-and-the-respective-base-classes">Commonly Used methods from List and the respective base classes.</h3>
<ul>
<li>.add(element) -&gt; add an element in the last.</li>
<li>.add(Index, element) -&gt; add an element at the specified index.</li>
<li>.addAll(Collection x) -&gt; adds all the elements of the collection x</li>
<li>.addAll(Index , Collection x) -&gt; adds all the elements of the collection x from the specified index.</li>
<li>.set(index,element) -&gt; change an element at the specified index.</li>
<li>.get(index) -&gt; Retrieve the element from the requested index.</li>
<li>.contains(object) -&gt; Retrieves a boolean value if the element is available..</li>
<li>.indexOf(object) -&gt; Retrieves index value if the element is available or -1.</li>
<li>.remove(object) -&gt; to remove an element at the requested position.</li>
<li>.sort() -&gt; Sorts the collection.</li>
<li>.subList(startIndex, endIndex) -&gt; returns an array list with values from the range of index.</li>
<li>.trimToSize() -&gt; The capacity of the array will be set to the size of the array to save memory.</li>
</ul>
<table>
  <thead>
      <tr>
          <th>Pros</th>
          <th>Cons</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ideal if a lot of index-based retrieval or update operations will be performed.</td>
          <td>If elements need to be <strong>added</strong> or <strong>removed</strong> at an index the entire array elements will be shifted internally.</td>
      </tr>
      <tr>
          <td>Can be traversed through both directions.</td>
          <td>Java ArrayList class is non-synchronized.</td>
      </tr>
      <tr>
          <td>Can hold duplicate elements.</td>
          <td></td>
      </tr>
      <tr>
          <td>Can take hold of null.</td>
          <td></td>
      </tr>
      <tr>
          <td>Once the load factor is met then the capacity will be increased by (current capacity *3/2)+1</td>
          <td></td>
      </tr>
  </tbody>
</table>
<hr>
<p><em>Note: This article was originally published on Blogger on February 11, 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>