<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Matrices on avni.sh</title>
    <link>http://www.avni.sh/tags/matrices/</link>
    <description>Recent content in Matrices on avni.sh</description>
    <image>
      <title>avni.sh</title>
      <url>http://www.avni.sh/cover.webp</url>
      <link>http://www.avni.sh/cover.webp</link>
    </image>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Tue, 16 Jul 2024 00:00:00 +0000</lastBuildDate>
    <atom:link href="http://www.avni.sh/tags/matrices/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Gaussian Elimination</title>
      <link>http://www.avni.sh/posts/linear-algebra/gaussian-elimination/</link>
      <pubDate>Tue, 16 Jul 2024 00:00:00 +0000</pubDate>
      <guid>http://www.avni.sh/posts/linear-algebra/gaussian-elimination/</guid>
      <description>The Gaussian Elimination algorithm is used to find the solution of a system of linear equations by performing row-wise operations on its matrix.</description>
      <content:encoded><![CDATA[<p>Let&rsquo;s consider a problem where we have to buy oranges and apples but under the following conditions.</p>
<ul>
<li>The difference between 2X the number of oranges and the number of apples should be 3.</li>
<li>The sum of 4X the number of oranges and the number of apples should be 9.</li>
<li>The difference between 5X the number of oranges and 3X the number of apples should be 7.</li>
</ul>
<h1 id="system-of-linear-equations">System of Linear Equations</h1>
<p>These conditions could be modeled using the following equations</p>
<p>$$2x-y=3\tag{1}$$</p>
<p>$$4x+y=9\tag{2}$$</p>
<p>$$5x-3y=7\tag{3}$$</p>
<p>where $x$ and $y$ are the number of oranges and apples respectively. Collectively, these equations would be called a <strong>system of linear equations</strong> of variables $x$ and $y$.</p>
<p>We can also represent this system of linear equations using matrices as follows.</p>
<p>$$\begin{bmatrix} 2x - y \\ 4x+y \\ 5x-3y \end{bmatrix} = \begin{bmatrix} 3 \\ 9  \\ 7\end{bmatrix}$$</p>
<h1 id="row-picture">Row Picture</h1>
<p>The representation of a system of linear equations as a <a href="/posts/math/linear-algebra/matrices/#multiplication-of-matrices" target="_blank">matrix multiplication</a> product of the coefficients and the variable matrix is called the <strong>row picture</strong>. Each row represents a different linear equation.</p>
<p>$$\begin{bmatrix} 2 &amp; -1 \\ 4 &amp; 1 \\ 5 &amp; -3 \end{bmatrix} \times \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 3 \\ 9  \\ 7\end{bmatrix}$$</p>
<p>As a linear algebra equation, this could be written as $\textbf{Ax=b}$. Where $\textbf{A}$ is the matrix of coefficients, $\textbf{x}$ is the vector of variables, and $\textbf{b}$ is the vector of constants.</p>
<h1 id="column-picture">Column Picture</h1>
<p>Alternatively, the coefficients of the variable $x$ and $y$ could be separated into two matrices as follows.</p>
<p>$$\begin{bmatrix} 2 \\ 4 \\ 5 \end{bmatrix} x \ + \begin{bmatrix} -1 \\ 1 \\ -3 \end{bmatrix}y = \begin{bmatrix} 3 \\ 9  \\ 7\end{bmatrix}$$</p>
<p>this is the <strong>column picture</strong> of the system of linear equations.</p>
<h1 id="solution-of-linear-equations">Solution of Linear Equations</h1>
<p>The value of variables that satisfies all equations is called the <strong>solution</strong> (or solutions if there are multiple) of the system of linear equations.</p>
<h2 id="one-solution">One Solution</h2>
<p>If we plot equations $(1)$, $(2)$, and $(3)$ on a 2D graph</p>
<p align="center"><img src="one_solution.png" alt="System of linear equations with one solution on a 2D graph"></p>
<p>The point at which all three lines intersect will be the solution of the linear equations i.e. $(2, 1)$.</p>
<h2 id="infinitely-many-solutions">Infinitely Many Solutions</h2>
<p>Consider the following system of linear equations.</p>
<p>$$x+3y=9 \tag{4}$$</p>
<p>$$2x+6y=18\tag{5}$$</p>
<p>If we plot them on a 2D graph, both lines overlap over each other.</p>
<p align="center"><img src="infinitely_many_solutions.png" alt="System of linear equations with infinitely many solutions on a 2D graph"></p>
<p>The set of values for $x$ and $y$ for which both equations are satisfied is infinitely large.
We can also observe this from the fact that if we multiply the equation $(4)$ by $2$ we get the equation $(5)$.</p>
<p>$$2 \times (x+3y = 9)$$</p>
<p>$$2x + 2\cdot3y = 2\cdot 9$$</p>
<p>$$2x+6y=18$$</p>
<p>Thus, the equation $(5)$ is just the equation $(4)$ scaled up by 2.</p>
<h2 id="no-solution">No Solution</h2>
<p>It is also possible for a system of linear equations that none of the variable values satisfy all the equations, like the following.</p>
<p>$$x+y=4 \tag{6}$$</p>
<p>$$x+y=8\tag{7}$$$$x-y=0 \tag{8}$$</p>
<p align="center"><img src="no_solution.png" alt="System of linear equations with no solution on a 2D graph"></p>
<p>Although we have multiple points of intersection between the lines, there is no single point where all three lines intersect. Thus, for no common values of $x$ and $y$ equations $(6)$, $(7)$, and $(8)$ are satisfied.</p>
<h1 id="elimination-method">Elimination Method</h1>
<p>To find the values of $x$ and $y$ in the system of linear equations $(1)$, $(2)$, and $(3)$, we can start by eliminating one of the variables from any of the equations.</p>
<p>If we multiply the equation $(2)$ by $3$</p>
<p>$$3 \times (4x+y=9) $$</p>
<p>$$12x + 3y = 27 \tag{9}$$</p>
<p>and add it to the equation $(3)$</p>
<p>$$(5x - 3y)+(12x+3y) = 7+27$$</p>
<p>$$5x+12x-3y+3y=34$$</p>
<p>$$17x=34$$</p>
<p>$$x=2$$</p>
<p>Now that we know the value of $x$ we can substitute it into any equation (like equation $(2)$) to find the value of $y$.</p>
<p>$$4 \times 2 + y = 9$$</p>
<p>$$8 + y = 9$$</p>
<p>$$y=9-8$$</p>
<p>$$y=1$$</p>
<p>To verify if $(2, 1)$ is indeed the solution of the system of linear equations $(1)$, $(2)$, and $(3)$ we can test the values in the equation $(1)$.</p>
<p>$$2x-y=3$$</p>
<p>$$2 \times 2 - 1 = 3$$</p>
<p>$$4-1 = 3$$</p>
<p>$$3=3$$</p>
<p>Thus, we can confirm that $(2, 1)$ is the solution.</p>
<h1 id="gaussian-elimination">Gaussian Elimination</h1>
<p>The <strong>Gaussian Elimination</strong> is an algorithm for finding the solution of a system of linear equations by performing row-wise operation on the matrix of coefficients and constants. It is named after Carl Friedrich Gauss.</p>
<p>It is similar to the <em>elimination method</em> discussed above, the difference is that the operations are performed collectively on the matrix. It involves the following steps.</p>
<ol>
<li>Expressing the system of linear equations with matrices, in the row picture form.</li>
<li>Creating an augmented matrices of coefficients and constants.</li>
<li>Reducing the augmented matrix to its row echelon form.</li>
<li>Breaking the row echelon form of the augmented matrix back to its row picture form.</li>
<li>Solving the reduced equations to find the values of variables.</li>
</ol>
<p>Let&rsquo;s apply the Gaussian Elimination on the row picture of equations $(1)$, $(2)$, and $(3)$</p>
<p>$$\begin{bmatrix} 2 &amp; -1 \\ 4 &amp; 1 \\ 5 &amp; -3 \end{bmatrix} \times \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 3 \\ 9  \\ 7\end{bmatrix}$$</p>
<h2 id="augmented-matrix-of-coefficients-and-constants">Augmented Matrix of Coefficients and Constants</h2>
<p>First, we have to create an augmented matrix from the coefficient and constants matrix of the row picture.</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 4 &amp; 1 &amp; | &amp; 9 \\ 5 &amp; -3 &amp; | &amp; 7\end{bmatrix}$$</p>
<p>Then, we reduce this augmented matrix to its row echelon form.</p>
<h2 id="row-echelon-form">Row Echelon Form</h2>
<p>The <strong>row echelon form</strong> of a matrix is an <a href="/posts/math/linear-algebra/matrices/#triangular-matrix" target="_blank">upper-triangular matrix</a> representation (the last row of the matrix could be just $0$s) of the augmented matrix.</p>
<p>To obtain the row echelon form, we can perform <a href="/posts/math/linear-algebra/matrices/#addition-and-subtraction-of-matrices" target="_blank">matrix addition</a>, <a href="/posts/math/linear-algebra/matrices/#multiplication-and-division-with-a-scalar-value" target="_blank">scalar multiplication</a> (with values other than $0$) and swaps between the rows of the augmented matrix.</p>
<p>Initially, the first element in the <a href="/posts/math/linear-algebra/matrices/#diagonal-of-a-matrix" target="_blank">diagonal</a> of the matrix is the <strong>pivot</strong>. All the elements below the pivot have to be reduced to $0$s by performing scalar multiplication and matrix addition/subtraction between the pivot row and the rows below it.</p>
<p>$$\begin{bmatrix} {\color{blue} 2} &amp; -1 &amp; | &amp; 3 \\ 4 &amp; 1 &amp; | &amp; 9 \\ 5 &amp; -3 &amp; | &amp; 7\end{bmatrix}$$</p>
<p>After that, we move on to the next pivot (next element in the diagonal) and repeat the process until we obtain the row echelon form.
If the next pivot element candidate is zero then we won&rsquo;t be able to reduce the elements below it to zero by just scalar multiplication and matrix addition/subtraction. In such cases, we perform a swap between the next row and any other row below it to obtain a non-zero element as the pivot.</p>
<p>If we multiply row 1 of the augmented matrix with the scalar value ${5 \over 2}$</p>
<p>$${5 \over 2} \times \begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3\end{bmatrix} = \begin{bmatrix} 5 &amp; {-5 \over 2} &amp; | &amp; {15 \over 2}\end{bmatrix}$$</p>
<p>and subtract it from row 3 we will obtain the following matrix.</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 4 &amp; 1 &amp; | &amp; 9 \\ 5-5 &amp; -3+{5 \over 2} &amp; | &amp; 7-{15 \over 2}\end{bmatrix}$$</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 4 &amp; 1 &amp; | &amp; 9 \\ 0 &amp; {-1 \over 2} &amp; | &amp; {-1 \over 2}\end{bmatrix}$$</p>
<p>Next, to reduce the last element under the pivot to $0$, we can multiply row 1 with the scalar value $2$</p>
<p>$$2 \times \begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \end{bmatrix} = \begin{bmatrix} 4 &amp; -2 &amp; | &amp; 6\end{bmatrix}$$</p>
<p>and subtract it from row 2.</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 4-4 &amp; 1+2 &amp; | &amp; 9-6 \\ 0 &amp; {-1 \over 2} &amp; | &amp; {-1 \over 2}\end{bmatrix}$$</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 0 &amp; 3 &amp; | &amp; 3 \\ 0 &amp; {-1 \over 2} &amp; | &amp; {-1 \over 2}\end{bmatrix}$$</p>
<p>Now, we move on to the next pivot.</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 0 &amp; {\color{blue} 3} &amp; | &amp; 3 \\ 0 &amp; {-1 \over 2} &amp; | &amp; {-1 \over 2}\end{bmatrix}$$</p>
<p>To reduce the element below the pivot to zeros we can multiply row 2 with the scalar value $1 \over 6$</p>
<p>$${1 \over 6} \times \begin{bmatrix} 0 &amp; 3 &amp; | &amp; 3 \end{bmatrix} = \begin{bmatrix} 0 &amp; {1 \over 2} &amp; | &amp; {1 \over 2} \end{bmatrix}$$</p>
<p>and add it to row 3</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 0 &amp; 3 &amp; | &amp; 3 \\ 0+0 &amp; {-1 \over 2}+{1 \over 2} &amp; | &amp; {-1 \over 2}+{1 \over 2}\end{bmatrix}$$</p>
<p>$$\begin{bmatrix} 2 &amp; -1 &amp; | &amp; 3 \\ 0 &amp; 3 &amp; | &amp; 3 \\ 0 &amp; 0 &amp; | &amp; 0\end{bmatrix}$$</p>
<p>This is the row echelon form the augmented matrix created by equation $(1)$, $(2)$, and $(3)$. Notice that the number of non-zero elements in the diagonal ($2$ and $3$) equals to the number of variables in the equations ($x$ and $y$).</p>
<h2 id="back-substitution">Back Substitution</h2>
<p>If we break the row echelon form of the augmented matrix back into the row picture we will obtain the reduced linear equations.</p>
<p>$$\begin{bmatrix} 2 &amp; -1 \\ 0 &amp; 3 \end{bmatrix} \times \begin{bmatrix} x \\ y\end{bmatrix} = \begin{bmatrix} 3 \\ 3\end{bmatrix}$$</p>
<p>$$\begin{bmatrix} 2x-y \\ 3y \end{bmatrix} = \begin{bmatrix} 3 \\ 3\end{bmatrix}$$</p>
<p>By solving these equations for the values of $y$</p>
<p>$$3y=y$$</p>
<p>$$y=1$$</p>
<p>and $x$</p>
<p>$$2x-y=3$$</p>
<p>$$2x - 1 = 3$$</p>
<p>$$2x = 3+1$$</p>
<p>$$2x=4$$</p>
<p>$$x=2$$</p>
<p>we have obtained the solution of the system of linear equations i.e. $(2, 1)$ which is the same solution that we got from the elimination method and the graph.</p>
<h2 id="infinitely-many-solutions-1">Infinitely many solutions</h2>
<p>Applying Gaussian Elimination to the system of linear equations $(4)$ and $(5)$.</p>
<ul>
<li>Creating the augmented matrix</li>
</ul>
<p>$$\begin{bmatrix} 1 &amp; 3 &amp; | &amp; 9 \\ 2 &amp; 6 &amp; | &amp; 18 \end{bmatrix}$$</p>
<ul>
<li>Selecting the pivot element.</li>
</ul>
<p>$$\begin{bmatrix} {\color{blue} 1} &amp; 3 &amp; | &amp; 9 \\ 2 &amp; 6 &amp; | &amp; 18 \end{bmatrix}$$</p>
<ul>
<li>Calculating the row echelon form by multiplying row 1 by the scalar value $2$ and subtracting it from row 2.</li>
</ul>
<p>$$\begin{bmatrix} 1 &amp; 3 &amp; | &amp; 9 \\ 0 &amp; 0 &amp; | &amp; 0\end{bmatrix}$$</p>
<p>The number of non-zero elements in the diagonal ($1$) is lower than the number of variables in the equation ($x$ and $y$).</p>
<p>After converting the row echelon form back to the row picture we get back the equation $(4)$ which can have infinitely many solutions like $(1, 2)$, $(0, 3)$, $(9, 0)$, etc.</p>
<h2 id="no-solution-1">No Solution</h2>
<p>Following is the application of Gaussian elimination on the system of linear equations $(6)$, $(7)$, and $(8)$</p>
<ul>
<li>Creating the augmented matrix.</li>
</ul>
<p>$$\begin{bmatrix} 1 &amp; 1 &amp; | &amp; 4 \\ 1 &amp; 1 &amp; | &amp; 8 \\ 1 &amp; -1 &amp; | &amp; 0\end{bmatrix}$$</p>
<ul>
<li>Selecting the pivot element.</li>
</ul>
<p>$$\begin{bmatrix} {\color{blue} 1} &amp; 1 &amp; | &amp; 4 \\ 1 &amp; 1 &amp; | &amp; 8 \\ 1 &amp; -1 &amp; | &amp; 0\end{bmatrix}$$</p>
<ul>
<li>Subtracting row 1 from row 2</li>
</ul>
<p>$$\begin{bmatrix} 1 &amp; 1 &amp; | &amp; 4 \\ 0 &amp; 0 &amp; | &amp; -4 \\ 1 &amp; -1 &amp; | &amp; 0\end{bmatrix}$$</p>
<ul>
<li>Subtracting row 1 from row 3</li>
</ul>
<p>$$\begin{bmatrix} 1 &amp; 1 &amp; | &amp; 4 \\ 0 &amp; 0 &amp; | &amp; -4 \\ 0 &amp; -2 &amp; | &amp; -4\end{bmatrix}$$</p>
<ul>
<li>Since the next pivot candidate is $0$, we&rsquo;ll have to swap rows 2 and 3 and make $-2$ the new pivot element.</li>
</ul>
<p>$$\begin{bmatrix} 1 &amp; 1 &amp; | &amp; 4 \\ 0 &amp; {\color{blue} -2} &amp; | &amp; -4 \\ 0 &amp; 0 &amp; | &amp; -4 \end{bmatrix}$$</p>
<p>We can&rsquo;t reduce this further without disrupting the row echelon form. The number of non-zero elements in the diagonal ($1$, $-2$, and $-4$) is higher than the number of variables in the equations ($x$ and $y$).</p>
<p>The reduced equations obtained after converting this back to row picture are:</p>
<p>$$x+y=4 \tag{10}$$</p>
<p>$$-2y=-4 \tag{11}$$</p>
<p>$$0=-4 \tag{12}$$</p>
<p>The equation $(12)$ is a fallacy. Thus, we can confirm that there is no solution for the system of linear equations $(10)$, $(11)$, and $(12)$.</p>
<h1 id="resources">Resources</h1>
<p><a href="https://youtu.be/QVKj3LADCnA?si=CXmVqn42IgpG8lwO" target="_blank">2. Elimination with Matrices.</a><br>
<a href="https://medium.com/linear-algebra/part-1-linear-equation-of-two-variables-and-matrices-d8de21eb8d51" target="_blank">Part 1 : Linear equation of two variables and Matrices</a><br>
<a href="https://medium.com/linear-algebra/part-5-row-picture-and-column-picture-899e6d834564" target="_blank">Part 5 : Row Picture and Column Picture</a><br>
<a href="https://medium.com/linear-algebra/part-6-gaussian-elimination-b1ad4a279a74" target="_blank">Part 6 : Gaussian Elimination</a></p>
]]></content:encoded>
    </item>
    <item>
      <title>Matrices</title>
      <link>http://www.avni.sh/posts/linear-algebra/matrices/</link>
      <pubDate>Fri, 12 Jul 2024 00:00:00 +0000</pubDate>
      <guid>http://www.avni.sh/posts/linear-algebra/matrices/</guid>
      <description>A matrix is a two dimensional array of data.</description>
      <content:encoded><![CDATA[<p>A matrix is a rank 2 <a href="/posts/math/linear-algebra/tensors/" target="_blank">tensor</a> i.e. an array expanding in a maximum of two independent dimensions.</p>
<p>Multiple <a href="/posts/math/linear-algebra/vectors/" target="_blank">vectors</a> could be arranged in rows or columns to create a matrix.</p>
<p>$$\vec{a} = \begin{bmatrix} 5 \\ 6 \\ 7 \end{bmatrix}\ \ \vec{b} = \begin{bmatrix} 7 \\ 9 \\ 10 \end{bmatrix}$$</p>
<p>$$\textbf{A} = \begin{bmatrix} \vec{a} &amp; \vec{b} \end{bmatrix} = \begin{bmatrix} 5 &amp; 7 \\ 6 &amp; 9 \\ 7 &amp; 10 \end{bmatrix}$$</p>
<p>$$\textbf{B} = \begin{bmatrix} \vec{a} \\ \vec{b} \end{bmatrix} = \begin{bmatrix} 5 &amp; 6 &amp; 7 \\ 7 &amp; 9 &amp; 10 \end{bmatrix}$$</p>
<p>The term <em>matrix</em> encompasses all vectors so $\vec{a}$ and $\vec{b}$ could be called matrices or vectors interchangeably.</p>
<h1 id="order-of-a-matrix">Order of a matrix</h1>
<p>The notation of the number of $rows \times columns$ of a matrix is called its <strong>order</strong>. For example, the following matrix has order $3 \times 4$.</p>
<p>$$\begin{bmatrix} 1 &amp; 8 &amp; 99 &amp; 0 \\ 56 &amp; 43 &amp; 91 &amp; 2 \\ 9 &amp; 5 &amp; 33 &amp; 1 \end{bmatrix}$$</p>
<p>Vectors are matrices with an order of either $n \times 1$ or $1 \times n$.</p>
<h1 id="diagonal-of-a-matrix">Diagonal of a matrix</h1>
<p>The <strong>diagonal</strong> of the matrix refers to the elements from the top left corner to the bottom right corner of the matrix (highlighted below).</p>
<p>$$\begin{bmatrix}  {\color {red} 4} &amp; 9 &amp; 5 \\ 9 &amp; {\color{red} 3} &amp; 1 \\ 90 &amp; 2 &amp; {\color{red} 8}\end{bmatrix} \ \ \ \begin{bmatrix}  {\color {red} 5} &amp; 9 &amp; 5 &amp; 98 \\ 9 &amp; {\color{red} 43} &amp; 1 &amp; 1 \\ 90 &amp; 2 &amp; {\color{red} 9} &amp; 52\end{bmatrix}$$</p>
<h1 id="square-matrix">Square Matrix</h1>
<p>A matrix with an equal number of rows and columns is called a <strong>square matrix</strong>. For example:</p>
<p>$$\begin{bmatrix} 1 &amp; 8 &amp; 9 \\ 2 &amp; 0 &amp; 11 \\ 9 &amp; 7 &amp; 1\end{bmatrix}$$</p>
<h2 id="triangular-matrix">Triangular Matrix</h2>
<p>A <strong>triangular matrix</strong> is a type of square matrix where either the elements above or below the diagonal are all $0$s.</p>
<p>Here is an example of an <strong>upper triangular matrix</strong></p>
<p>$$\begin{bmatrix} \color{red} 6 &amp; \color{red} 21 &amp; \color{red} 6 &amp; \color{red} 88 \\ 0 &amp; \color{red} 71 &amp; \color{red} 90 &amp; \color{red} 8 \\ 0 &amp; 0 &amp; \color{red} 1 &amp; \color{red} 71 \\ 0 &amp; 0 &amp; 0 &amp; \color{red} 4 \end{bmatrix}$$</p>
<p>Following is an example of a <strong>lower triangular matrix</strong></p>
<p>$$\begin{bmatrix} \color{red} 6 &amp;  0 &amp;  0 &amp;  0 \\ \color{red} 7 &amp; \color{red} 71 &amp; 0 &amp; 0 \\ \color{red} 12 &amp; \color{red} 61 &amp; \color{red} 1 &amp; 0 \\ \color{red} 4 &amp; \color{red} 6 &amp; \color{red} 5 &amp; \color{red} 4 \end{bmatrix}$$</p>
<h1 id="matrix-operations">Matrix Operations</h1>
<h2 id="equality-of-matrices">Equality of Matrices</h2>
<p>Two matrices are equal if</p>
<ol>
<li>They have the same order.</li>
<li>The values of the corresponding elements are the same.</li>
</ol>
<p>Given a matrix $\textbf{A} = \begin{bmatrix} 1 &amp; 2 \\ 3 &amp; 4 \end{bmatrix}$ and a matrix $\textbf{B} = \begin{bmatrix} 1 &amp; 2 \\ 3 &amp; 4 \end{bmatrix}$ we can say that $\textbf{A} = \textbf{B}$ because the order of both matrices is $2 \times 2$ and the elements corresponding to same position are also equal.</p>
<h2 id="addition-and-subtraction-of-matrices">Addition and Subtraction of Matrices</h2>
<p>To add two matrices we just have to sum the elements at corresponding positions. The order of both matrices should be the same for addition (and subtraction).</p>
<p>$$\begin{bmatrix} 4 &amp; 5 &amp; 9 \\ 89 &amp; 5 &amp; 91 \end{bmatrix} + \begin{bmatrix} 9 &amp; 4 &amp; 8 \\ 9 &amp; 1 &amp; 85 \end{bmatrix} = \begin{bmatrix} 4+9 &amp; 5+4 &amp; 9+8 \\ 89+9 &amp; 5+1 &amp; 91+85  \end{bmatrix} = \begin{bmatrix} 13 &amp; 9 &amp; 17 \\ 98 &amp; 6 &amp; 176 \end{bmatrix}$$</p>
<p>The subtraction operation is similar to the addition.</p>
<p>$$\begin{bmatrix} 9 &amp; 4 &amp; 8 \\ 9 &amp; 1 &amp; 85 \end{bmatrix} - \begin{bmatrix} 4 &amp; 5 &amp; 9 \\ 89 &amp; 5 &amp; 91 \end{bmatrix}  = \begin{bmatrix} 9-4 &amp; 4-5 &amp; 8-9 \\ 9-89 &amp; 1-5 &amp; 85-91  \end{bmatrix} = \begin{bmatrix} 5 &amp; -1 &amp; -1 \\ -80 &amp; -4 &amp; -6 \end{bmatrix}$$</p>
<h2 id="multiplication-and-division-with-a-scalar-value">Multiplication and Division with a Scalar Value</h2>
<p>If we want to multiply a matrix with a <a href="/posts/math/linear-algebra/tensors/#scalar-rank-0-tensor" target="_blank">scalar</a> value, we just have to multiply the scalar with each element individually.</p>
<p>$$5 \times \begin{bmatrix} 7 &amp; 6 \\ 8 &amp; 4 \end{bmatrix} = \begin{bmatrix} 7 \times 5 &amp; 6 \times 5 \\ 8 \times 5 &amp; 4 \times 5 \end{bmatrix} = \begin{bmatrix} 35 &amp; 30 \\ 40 &amp; 20 \end{bmatrix}$$</p>
<p>Division operation with a scalar value is also applied similarly on the matrix.</p>
<p>$$\begin{bmatrix} 9 &amp; 6 \\ 12 &amp; 36 \end{bmatrix} \div 3 = \begin{bmatrix} 9 &amp; 6 \\ 12 &amp; 36 \end{bmatrix} \times {1 \over 3} = \begin{bmatrix} {9 \over 3} &amp; {6 \over 3} \\ {12 \over 3} &amp; {36 \over 3} \end{bmatrix} = \begin{bmatrix} 3 &amp; 2 \\ 4 &amp; 12 \end{bmatrix}$$</p>
<h2 id="transpose-of-a-matrix">Transpose of a Matrix</h2>
<p>The <strong>transpose</strong> operation on a matrix swaps the elements in its rows and columns. For example, if we apply the transpose operation on the matrix $\textbf{C} = \begin{bmatrix} 4 &amp; 5 &amp; 8 \\ 6 &amp; 7 &amp; 9 \end{bmatrix}$ the result would be $\textbf{C}^T = \begin{bmatrix} 4 &amp; 6 \\ 5 &amp; 7 \\ 8 &amp; 9 \end{bmatrix}$.</p>
<p>Notice that the order of the matrix is also changed from $2 \times 3$ to $3 \times 2$.</p>
<h3 id="symmetric-matrix">Symmetric Matrix</h3>
<p>A matrix is <strong>symmetric</strong> if it remains unchanged after the transpose operation is applied i.e. $\textbf{C}^T = \textbf{C}$. The following matrix is an example of a symmetric matrix:</p>
<p>$$\left(\begin{bmatrix} 9 &amp; 5 &amp; 89 \\ 5 &amp; 8 &amp; 67 \\ 89 &amp; 67 &amp; 34\end{bmatrix}\right)^T = \begin{bmatrix} 9 &amp; 5 &amp; 89 \\ 5 &amp; 8 &amp; 67 \\ 89 &amp; 67 &amp; 34\end{bmatrix}$$</p>
<h3 id="skew-symmetric-matrix">Skew-Symmetric Matrix</h3>
<p>If we perform the transpose operation on a skew-symmetric matrix the result will be the original matrix multiplied by the scalar value $(-1)$ i.e. $\textbf{C}^T = (-1) \times \textbf{C}$.</p>
<p>$$\left(\begin{bmatrix} 9 &amp; 5 &amp; 89 \\ -5 &amp; 8 &amp; 67 \\ -89 &amp; -67 &amp; 34\end{bmatrix}\right)^T = \begin{bmatrix} 9 &amp; -5 &amp; -89 \\ 5 &amp; 8 &amp; -67 \\ 89 &amp; 67 &amp; 34\end{bmatrix}$$</p>
<h2 id="multiplication-of-matrices">Multiplication of Matrices</h2>
<p>To calculate the product of two matrices we have to take the <a href="/posts/math/linear-algebra/vectors/#dot-product" target="_blank">dot product</a> of each row from the first matrix with every column of the second matrix.</p>
<p>$$\textbf{A} \times \textbf{B} = \begin{bmatrix} a_{11} &amp; a_{12} \\ a_{21} &amp; a_{22} \\ a_{31} &amp; a_{32} \end{bmatrix} \times \begin{bmatrix} b_{11} &amp; b_{12} \\ b_{21} &amp; b_{22}  \end{bmatrix} = \begin{bmatrix} a_{11}b_{11} +a_{12}b_{21} &amp; a_{11}b_{12} +a_{12}b_{22} \\ a_{21}b_{11} +a_{22}b_{21} &amp; a_{21}b_{12} +a_{22}b_{22} \\ a_{31}b_{11} +a_{32}b_{21} &amp; a_{31}b_{12} +a_{32}b_{22}\end{bmatrix}$$</p>
<p>Because the dot product could only be calculated between the vectors with identical number of elements, to multiply two matrices the number of columns in the first matrix should be equal to the number of rows in the second matrix.</p>
<p align="center"><img src="order_of_matrices.png" alt="Order of product matrix"></p>
<p>If we switch the matrix $\textbf{A}$ and $\textbf{B}$ while performing multiplication the resulting product matrix will have different dimensions (and elements). Thus,  <em>matrix multiplication is not commutative</em>.
$$\textbf{A} \times \textbf{B} \neq \textbf{B} \times \textbf{A}$$</p>
<h3 id="identity-matrix">Identity Matrix</h3>
<p>A square matrix with $1$s on its diagonal (and $0$s as non-diagonal elements) is called an <strong>identity matrix</strong>. Multiplying any matrix with an identity matrix (of valid order) is analogous to multiplying a number with $1$.</p>
<p>In equations, the identity matrix is represented with $\textbf{I}$</p>
<p>$$\textbf{A} \times \textbf{I} = \textbf{A}$$
$$\begin{bmatrix} 7 &amp; 1 &amp; 8 \\ 4 &amp; 5 &amp; 3 \\ 1 &amp; 2 &amp; 6 \end{bmatrix} \times \begin{bmatrix} 1 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 1 \end{bmatrix} = \begin{bmatrix} 7 &amp; 1 &amp; 8 \\ 4 &amp; 5 &amp; 3 \\ 1 &amp; 2 &amp; 6 \end{bmatrix}$$</p>
<h3 id="null-matrix">Null Matrix</h3>
<p>If multiplication with identity matrix is analogous to multiplication with $1$ then multiplication with null matrix will be analogous to multiplication of a number with $0$.
A null matrix has only $0$s as elements and doesn&rsquo;t have to be a square matrix. It is represented as $\textbf{0}$ in equations.</p>
<p>$$\textbf{A} \times \textbf{0} = \textbf{0}$$
$$\begin{bmatrix} 7 &amp; 1 &amp; 8 \\ 4 &amp; 5 &amp; 3 \\ 1 &amp; 2 &amp; 6 \end{bmatrix} \times \begin{bmatrix} 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 \end{bmatrix} = \begin{bmatrix} 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 \end{bmatrix}$$</p>
<h3 id="idempotent-matrix">Idempotent Matrix</h3>
<p>A matrix when multiplied by itself remains unchanged, is called an <strong>idempotent matrix</strong>.
$$\textbf{A} \times \textbf{A} = \textbf{A}^{2} = \textbf{A}$$
Since the order of the product matrix has to be the same as the original matrix, an idempotent matrix must always be a square matrix.
All identity matrices are idempotent.</p>
<h2 id="division-of-matrices">Division of Matrices</h2>
<p>If we have to divide a matrix with another matrix</p>
<p>$$\textbf{A} \div \textbf{B} = {\textbf{A} \over \textbf{B}}$$</p>
<p>we can rephrase it as</p>
<p>$$\textbf{A} \times {\textbf{I} \over \textbf{B}} = \textbf{A} \times \textbf{B}^{-1}$$</p>
<h3 id="inverse-of-a-matrix">Inverse of a Matrix</h3>
<p>The matrix $\textbf{B}^{-1}$ will be called the <strong>inverse</strong> of the matrix $\textbf{B}$.
$$\textbf{B} \times \textbf{B}^{-1} = \textbf{I}$$
The inverse matrix has the same order as the original matrix.
We can find the inverse of any square matrix using the process of <a href="/posts/math/linear-algebra/gauss-jordan-elimitation/" target="_blank">Gauss-Jordan Elimination</a>.</p>
<h1 id="resources">Resources</h1>
<p><a href="https://www.khanacademy.org/math/precalculus/x9e81a4f98389efdf:matrices/x9e81a4f98389efdf:properties-of-matrix-multiplication/a/matrix-multiplication-dimensions" target="_blank">Matrix multiplication dimensions</a><br>
<a href="https://medium.com/linear-algebra/part-2-operations-on-matrices-3caab542aebd" target="_blank">Part 2 : Operations on Matrices</a><br>
<a href="https://medium.com/linear-algebra/part-3-types-of-matrices-diagonal-and-transpose-151418a47c06" target="_blank">Part 3 : Types of Matrices, Diagonal, and Transpose</a></p>
]]></content:encoded>
    </item>
    <item>
      <title>Vectors</title>
      <link>http://www.avni.sh/posts/linear-algebra/vectors/</link>
      <pubDate>Wed, 10 Jul 2024 00:00:00 +0000</pubDate>
      <guid>http://www.avni.sh/posts/linear-algebra/vectors/</guid>
      <description>A vector is an array of data expanding in one dimension.</description>
      <content:encoded><![CDATA[<p>A vector could represent the magnitude of a quantity broken down into its various components, for example, the velocity of an object moving on a 2D plane could be represented using the vector $\vec{v} = \begin{bmatrix} 8 &amp; 9 \end{bmatrix}$.</p>
<p align="center"><img src="vector_example.png" alt="Velocity of an object moving in 2D plane"></p>
<p>On a 2D plane $8$ and $9$ could be called the horizontal and vertical components of the vector $\vec{v}$ respectively. But the words <em>horizontal</em> and <em>vertical</em> on a 2D or 3D plane are relative to the viewer (try rotating the figure above anti-clockwise).</p>
<p>By using <strong>unit vectors</strong> $\hat{i}$, $\hat{j}$, and $\hat{k}$ (vectors with <a href="/posts/math/linear-algebra/tensors/#magnitude-of-a-vector" target="_blank">magnitude</a> 1 along the x, y, and z axis respectively) we can define the orientation of each component of the vector. Thus, vector $\vec{v}$ could be redefined as $8\hat{i} + 9\hat{j} + 0\hat{k}$ in a 3D plane or $8\hat{i} + 9\hat{j}$ on a 2D plane.</p>
<p align="center"><img src="unit_vectors.png" alt="Unit vectors along the x, y, and z axis"></p>
<p align="center"><small><i>Unit vectors along the x, y, and z axis</i></small></p>
<h1 id="projections">Projections</h1>
<p>We can break the vector into its components if we know the angle between the vector and any of the axes.</p>
<p>Assuming we have a vector $\vec{v}$ from the origin, we can draw a circle on the plane with radius $|\vec{v}|$ (magnitude of the vector). To break it into two components along the x and y axis we can draw a <em>projection</em> line starting from the tip of the vector to both axes.</p>
<p align="center"><img src="vector_components.png" alt="Breaking a vector into its components"></p>
<p>From the figure above we can assess that</p>
<p>$$ \vec{v}_{x} = |\vec{v}| \times {\vec{v}_x \over |\vec{v}|} = |\vec{v}|\cos(\theta) $$</p>
<p>$$ \vec{v}_{y} = |\vec{v}| \times {\vec{v}_y \over |\vec{v}|} = |\vec{v}|\sin(\theta) $$</p>
<p>Thus, the vector components of $\vec{v}$ along the x and y axis are $|\vec{v}|\cos(\theta)$ and $|\vec{v}|\sin(\theta)$ respectively, where $\theta$ is the angle between the vector and the x-axis.</p>
<p>Similarly, we can find the projection of a vector on another vector. For example, we have two vectors $\vec{v}$ and $\vec{u}$ subtended by the angle $\phi$.</p>
<p align="center"><img src="vector_projection.png" alt="Projection of vector v on u"></p>
<p align="center"><small><i>Projection of $\vec{v}$ on $\vec{u}$</i></small></p>
<p>The component of $\vec{v}$ on vector $\vec{u}$ will be $|\vec{v}| \cos(\phi)$, but this is just a scalar value. To add direction we have to include $\hat{u}$, the unit vector along the direction of $\vec{u}$. Thus, the <strong>projection vector</strong> of $\vec{v}$ on $\vec{u}$ is $|\vec{v}| \cos(\phi) \hat{u}$.</p>
<h1 id="dot-product">Dot Product</h1>
<p>The <strong>dot product</strong> of two vectors is calculated by multiplying the projection of the first vector with the magnitude of the second vector. It quantifies the similarity in the direction of both vectors.</p>
<p>$$ \vec{v} \cdot \vec{u} = |\vec{v}||\vec{u}|\cos(\phi)$$
The dot product operation is <em>commutative</em>, so it doesn&rsquo;t matter if we multiply the projection of $\vec{v}$ with the magnitude of $\vec{u}$ or vice-versa.
$$\vec{v} \cdot \vec{u} = |\vec{v}||\vec{u}|\cos(\phi) = |\vec{u}||\vec{v}|\cos(\phi) = \vec{u} \cdot \vec{v}$$</p>
<p align="center"><img src="dot_products.png" alt="Projection of vector v on u and vector u on v"></p>
<p align="center"><small><i>Projection of $\vec{v}$ on $\vec{u}$ and projection of $\vec{u}$ on $\vec{v}$</i></small></p>
<p>We can also calculate the dot product by multiplying the corresponding elements of both vectors and adding them up.</p>
<p>$$\vec{v} \cdot \vec{u} = \begin{bmatrix} 1 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} 3 \\ -2 \end{bmatrix} = 1 \times 3 + 1 \times (-2) = 1$$</p>
<h1 id="cross-product">Cross Product</h1>
<p>The <strong>cross product</strong> of two vectors returns a vector that is perpendicular to the direction of both vectors.</p>
<p align="center"><img src="cross_product.png" alt="The result of the cross product of two vectors is another vector that is perpendicular to both"></p>
<p>The magnitude of the resulting vector of the cross product is equal to the area of the parallelogram created by the two vectors.</p>
<p align="center"><img src="cross_product_parallelogram.png" alt="Parallelogram created by both vectors"></p>
<p>The length of the sides of the parallelogram will be $|\vec{v}|$ and $|\vec{u}|$.</p>
<p align="center"><img src="vector_parallelogram.png" alt="Parallelogram created by both vectors"></p>
<p>Hence, from the area of the parallelogram we can calculate the magnitude of the cross product of vectors $\vec{u}$ and $\vec{v}$
$$| \vec{u} \times \vec{v}| = |\vec{u}||\vec{v}|\sin(\phi)$$</p>
<p>Unlike the dot product, the cross product is not commutative.
$$\vec{u} \times \vec{v} \neq \vec{v} \times \vec{u}$$</p>
<p>If we have to find the cross product of two vectors using their elements, we have to create a matrix of both vectors and calculate its <a href="/posts/math/linear-algebra/determinants/" target="_blank">determinant</a>.</p>
<p>$$\vec{u} \times \vec{v} = \begin{bmatrix} u_1 \\ u_2 \end{bmatrix} \times \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \begin{vmatrix} u_1 &amp; v_1 \\ u_2 &amp; v_2 \end{vmatrix} = u_1 v_2 - v_1 \ u_2$$</p>
<h1 id="hadamard-product">Hadamard Product</h1>
<p>Named after Jacques Hadamard, the <strong>Hadamard product</strong> is the resultant matrix after the multiplication of corresponding elements of two vectors or matrices.
$$\vec{v} \odot \vec{u} = \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} \odot \begin{bmatrix} u_1 \\ u_2 \end{bmatrix} = \begin{bmatrix} v_1 \times u_1 \\ v_2 \times u_2 \end{bmatrix}$$
The result of the Hadamard product will be the same irrespective of the order of multiplication. Thus, it is commutative.
$$\vec{v} \odot \vec{u} = \vec{u} \odot \vec{v}$$
Some use cases of the Hadamard product operation are JPEG image compression and LSTM (Long Short-Term Memory) cells of RNNs (Recurrent Neural Networks). It is also known as Schur Product (named after Issai Schur).</p>
<h1 id="resources">Resources</h1>
<p><a href="https://medium.com/linear-algebra/part-12-vectors-a99364499121" target="_blank">Part 12 : Vectors</a><br>
<a href="https://www.khanacademy.org/math/precalculus/x9e81a4f98389efdf:vectors/x9e81a4f98389efdf:component-form/v/vector-components-from-magnitude-and-direction" target="_blank">Vector components from magnitude &amp; direction</a><br>
<a href="https://medium.com/linear-algebra/part-13-vector-components-b78a61be3817" target="_blank">Part 13 : Vector Components</a><br>
<a href="https://www.youtube.com/watch?v=LyGKycYT2v0" target="_blank">Dot products and duality | Chapter 9, Essence of linear algebra</a><br>
<a href="https://medium.com/linear-algebra/part-14-dot-and-hadamard-product-b7e0723b9133" target="_blank">Part 14 : Dot and Hadamard Product</a><br>
<a href="https://www.youtube.com/watch?v=eu6i7WJeinw" target="_blank">Cross products | Chapter 10, Essence of linear algebra</a><br>
<a href="https://www.khanacademy.org/math/cc-sixth-grade-math/x0267d782:cc-6th-plane-figures/cc-6th-parallelogram-area/a/area-of-parallelogram" target="_blank">Area of parallelograms</a><br>
<a href="https://en.wikipedia.org/wiki/Jacques_Hadamard" target="_blank">Jacques Hadamard</a><br>
<a href="https://en.wikipedia.org/wiki/Issai_Schur" target="_blank">Issai Schur</a></p>
]]></content:encoded>
    </item>
    <item>
      <title>Tensors</title>
      <link>http://www.avni.sh/posts/linear-algebra/tensors/</link>
      <pubDate>Wed, 03 Jul 2024 00:00:00 +0000</pubDate>
      <guid>http://www.avni.sh/posts/linear-algebra/tensors/</guid>
      <description>A tensor is an array of data expanding in multiple independent dimensions.</description>
      <content:encoded><![CDATA[<p>In linear algebra, a <strong>tensor</strong> is an array of data expanding in multiple (or zero) independent dimensions. It is used to represent quantities/equations/functions with multiple components, for example, the equation $3x+2y=0$ could be represented with the tensor $[3\ 2\ 0]$ where each value in the tensor represents the different components of the equation.</p>
<p>The number of independent dimensions of a tensor is called its <strong>rank</strong>.</p>
<p>Vectors and matrices could be generalized with the term tensor. The following Venn diagram visualizes the connection between them.</p>
<p align="center"><img src="tensors-matrices-vectors-venn-diagram.png" alt="Venn Diagram of Tensors, Matrices and Vectors"></p>
<h1 id="scalar-rank-0-tensor">Scalar (Rank 0 Tensor)</h1>
<p>A rank 0 tensor does not expand in any dimension, it is used to represent quantities that could be expressed by just one component i.e. its magnitude or scale. For example, the distance of $8\ cm$ between two points could be represented by the tensor $[ 8 ]$.</p>
<p>Since the value of a rank 0 tensor signifies only its <em>scale</em> or <em>magnitude</em> it could be called a <strong>scalar</strong> value. A scalar value could represent the mass of an object, the temperature of a room, the speed of a car, etc.</p>
<h1 id="vector-rank-1-tensor">Vector (Rank 1 Tensor)</h1>
<p>A rank 1 tensor (or a <strong>vector</strong>) expands in one dimension i.e. it represents values with more than one component.</p>
<p align="center"><img src="vectors.png" alt="Vectors (Rank 1) Tensor"></p>
<p>Vectors are used to represent the magnitude and direction of different components of quantities such as displacement of an object, velocity of a car, electric field generated by a particle, etc. For example, displacement of $5\ m$ in the east direction and $10\ m$ in the north direction for an object could be represented with the vector $[5\ 10]$ where each component represents displacement in the east and north direction respectively.</p>
<p>In equations, vectors are denoted as bold letters ($\textbf{E}$) or letters with an arrow on top ($\vec{E}$).</p>
<h2 id="magnitude-of-a-vector">Magnitude of a Vector</h2>
<p>To obtain the magnitude/scalar value of a vector we have to square all of its components and take the square root of their sum. For example, the magnitude of the displacement vector in the example above i.e. the distance between the start and the end will be $$|\ [ 0\ 5 \ 0 \ 10]\ | = \sqrt{0^2 + 5^2 + 0^2 + 10^2} = 25+100 = 125\ m$$
The magnitude of a vector is denoted by enclosing it within $|\ \ |$, for example, $|\vec{E}|$.</p>
<h1 id="matrix-rank-2-tensor">Matrix (Rank 2 Tensor)</h1>
<p>A rank 2 tensor (or a <strong>matrix</strong>) expands in two independent dimensions.</p>
<p align="center"><img src="matrices.png" alt="Matrices (Rank 2) Tensor"></p>
<p>A <em>system of linear equations</em>
$$x - 2y = 6$$
$$ x - y = 4 $$
$$ x + y = 0 $$
could be represented using a matrix as</p>
<p>$$ \begin{bmatrix} 1 &amp; -2 &amp; 6 \\ 1 &amp; -1 &amp; 4 \\ 1 &amp; 1 &amp; 0 \end{bmatrix}$$</p>
<p>This matrix has three rows and three columns. Thus, its <strong>order</strong> will be $3 \times 3$.
A matrix with one row or one column could be called a matrix or vector interchangeably.</p>
<h1 id="tensors-rank-3">Tensors (Rank &gt;=3)</h1>
<p>The following diagram visualizes a rank 3 tensor in a 3-dimensional plane.</p>
<p align="center"><img src="3d-tensor.png" alt="Rank 3 Tensor"></p>
<p>Tensors with the rank greater than 3 are difficult to visualize but they could be represented as nested arrays in any programming language.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="c1"># The following array has four levels of nesting</span>
</span></span><span class="line"><span class="cl"><span class="n">rank_4_tensor</span> <span class="o">=</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="p">[</span>
</span></span><span class="line"><span class="cl">        <span class="p">[</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span>
</span></span><span class="line"><span class="cl">            <span class="p">],</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">8</span><span class="p">,</span> <span class="mi">58</span><span class="p">,</span> <span class="mi">26</span>
</span></span><span class="line"><span class="cl">            <span class="p">]</span>
</span></span><span class="line"><span class="cl">        <span class="p">],</span>
</span></span><span class="line"><span class="cl">        <span class="p">[</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">5</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="mi">19</span>
</span></span><span class="line"><span class="cl">            <span class="p">],</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span>
</span></span><span class="line"><span class="cl">            <span class="p">]</span>
</span></span><span class="line"><span class="cl">        <span class="p">]</span>
</span></span><span class="line"><span class="cl">    <span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="p">[</span>
</span></span><span class="line"><span class="cl">        <span class="p">[</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">13</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">15</span>
</span></span><span class="line"><span class="cl">            <span class="p">],</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">16</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">18</span>
</span></span><span class="line"><span class="cl">            <span class="p">]</span>
</span></span><span class="line"><span class="cl">        <span class="p">],</span>
</span></span><span class="line"><span class="cl">        <span class="p">[</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">19</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">21</span>
</span></span><span class="line"><span class="cl">            <span class="p">],</span>
</span></span><span class="line"><span class="cl">            <span class="p">[</span>
</span></span><span class="line"><span class="cl">                <span class="mi">22</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">24</span>
</span></span><span class="line"><span class="cl">            <span class="p">]</span>
</span></span><span class="line"><span class="cl">        <span class="p">]</span>
</span></span><span class="line"><span class="cl">    <span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="p">]</span>
</span></span></code></pre></div><h1 id="resources">Resources</h1>
<p><a href="https://www.youtube.com/watch?v=f5liqUk0ZTw" target="_blank">What&rsquo;s a Tensor?</a><br>
<a href="https://mathworld.wolfram.com/TensorRank.html" target="_blank">Tensor Rank</a><br>
<a href="https://medium.com/linear-algebra/part-1-linear-equation-of-two-variables-and-matrices-d8de21eb8d51" target="_blank">Part 1 : Linear equation of two variables and Matrices</a><br>
<a href="https://medium.com/linear-algebra/part-4b-tensors-scalars-and-vectors-68cf6c1f2be" target="_blank">Part 4B : Tensors, Scalars, Vectors, and Matrices</a></p>
]]></content:encoded>
    </item>
    <item>
      <title>Arrays, Strings, and HashMaps</title>
      <link>http://www.avni.sh/posts/data-structures-and-algorithms/arrays-strings-hashmaps/</link>
      <pubDate>Fri, 29 Sep 2023 00:00:00 +0000</pubDate>
      <guid>http://www.avni.sh/posts/data-structures-and-algorithms/arrays-strings-hashmaps/</guid>
      <description>Understanding basic data structures like arrays, strings, and hashmaps</description>
      <content:encoded><![CDATA[<p>Data structures like arrays, strings, and hashmaps are available by default in most programming languages. They facilitate storage of large amounts of data in an efficient format which makes it easier (and sometimes relatively faster) to access during runtime.</p>
<h1 id="arrays">Arrays</h1>
<p align="center"><img src="arrays.png" alt="Array"></p>
<p>An array is a sequential store of data (referred to as <em>elements</em>).</p>
<p>In languages like Python, an array can store elements with multiple data types, like, <code>[1, &quot;Hello, World&quot;, True]</code> but for languages like Go, C++, and Java an array can store elements of a singular data type.</p>
<h2 id="iterating-over-an-array-using-indexes">Iterating over an array using indexes</h2>
<p>An <strong>index</strong> is assigned to each element based on its location in the array. The value stored at the <code>index</code> could be extracted using <code>[ ]</code> brackets with the array&rsquo;s variable name, for example, <code>arrayExample[index]</code>.</p>
<p>We can iterate over values stored in an array by defining a loop starting from <code>0</code> (index of the first element in the <a href="/posts/go/go-programming-language/" target="_blank">Go Programming Language</a>) till <code>len(arrayExample)-1</code> (index of last element).</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">  <span class="nx">arrayExample</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="mi">12</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3123</span><span class="p">,</span> <span class="mi">78</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">i</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">arrayExample</span><span class="p">);</span><span class="nx">i</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Element at index&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="s">&#34;is:&#34;</span><span class="p">,</span> <span class="nx">arrayExample</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Output</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index 0 is: 12</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index 1 is: 3</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index 2 is: 2</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index 3 is: 1</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index 4 is: 3123</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index 5 is: 78</span>
</span></span></code></pre></div><p>The <a href="/posts/computer-science/interview-preparation/time-complexity/" target="_blank">time complexity</a> of this loop is $O(n)$ where $n$ is the size of <code>arrayExample</code>.</p>
<h2 id="nested-arrays">Nested Arrays</h2>
<p>A simple array will store data in just one dimension but we can nest arrays to store multidimensional data like <em>matrices</em>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="nx">nestedArrayExample</span> <span class="o">:=</span> <span class="p">[][]</span><span class="kt">int</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">                        <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">234</span><span class="p">},</span>
</span></span><span class="line"><span class="cl">                        <span class="p">{</span><span class="mi">456</span><span class="p">,</span> <span class="mi">345</span><span class="p">,</span> <span class="mi">3457</span><span class="p">,</span> <span class="mi">5</span><span class="p">},</span>
</span></span><span class="line"><span class="cl">                        <span class="p">}</span>
</span></span></code></pre></div><p>To iterate over the elements in a nested array we have to define multiple nested loops.
The time complexity of fetching all the elements from a nested array will be $O(n^m)$ where $n$ is the size of individual arrays and $m$ is the order of nesting (to iterate over all the elements we have to define $m$ nested loops each of length $n$).</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">  <span class="nx">nestedArrayExample</span> <span class="o">:=</span> <span class="p">[][]</span><span class="kt">int</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">234</span><span class="p">},</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span><span class="mi">456</span><span class="p">,</span> <span class="mi">345</span><span class="p">,</span> <span class="mi">3457</span><span class="p">,</span> <span class="mi">5</span><span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">i</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">nestedArrayExample</span><span class="p">);</span><span class="nx">i</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Nested Array at index:&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="k">for</span> <span class="nx">j</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">j</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">nestedArrayExample</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span><span class="nx">j</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;Element at index[%d][%d]: %d \n&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">j</span><span class="p">,</span> <span class="nx">nestedArrayExample</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">j</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Output</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Nested Array at index: 0</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[0][0]: 1 </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[0][1]: 123 </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[0][2]: 123 </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[0][3]: 234 </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Nested Array at index: 1</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[1][0]: 456 </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[1][1]: 345 </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[1][2]: 3457 </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Element at index[1][3]: 5</span>
</span></span></code></pre></div><p>In the example above an array is nested inside another array ($m=2$), to traverse it we have to define a loop nested inside another loop. Thus, the time complexity of the program is $O(n^2)$.</p>
<h2 id="arraylist">ArrayList</h2>
<p align="center"><img src="arraylist.png" alt="ArrayList"></p>
<p><strong>ArrayList</strong> data structure is similar to an array but it doubles in size when it runs out of space. It is ideal for scenarios with limited memory space for program execution.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">type</span> <span class="nx">ArrayList</span> <span class="kd">struct</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nx">capacity</span> <span class="kt">int</span>
</span></span><span class="line"><span class="cl">  <span class="nx">array</span> <span class="p">[]</span><span class="kt">int</span> 
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="p">(</span><span class="nx">al</span> <span class="o">*</span><span class="nx">ArrayList</span><span class="p">)</span> <span class="nf">insert</span><span class="p">(</span><span class="nx">x</span> <span class="kt">int</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">  <span class="k">if</span><span class="p">(</span><span class="nx">al</span><span class="p">.</span><span class="nx">capacity</span><span class="o">==</span><span class="mi">0</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// If ArrayList is at full capacity</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// then create a new temporary ArrayList</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// twice the size of the existing one</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Resizing ArrayList&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="kd">var</span> <span class="nx">newArray</span> <span class="p">[]</span><span class="kt">int</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// Copy all the elements from existing</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// ArrayList to the new ArrayList</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// This will take O(n) time</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// where n is the size of al.array</span>
</span></span><span class="line"><span class="cl">    <span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">i</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">al</span><span class="p">.</span><span class="nx">array</span><span class="p">);</span><span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nx">newArray</span> <span class="p">=</span> <span class="nb">append</span><span class="p">(</span><span class="nx">newArray</span><span class="p">,</span> <span class="nx">al</span><span class="p">.</span><span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// Insert the element in the new ArrayList</span>
</span></span><span class="line"><span class="cl">    <span class="nx">newArray</span> <span class="p">=</span> <span class="nb">append</span><span class="p">(</span><span class="nx">newArray</span><span class="p">,</span> <span class="nx">x</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// Increase the capacity of ArrayList</span>
</span></span><span class="line"><span class="cl">    <span class="nx">al</span><span class="p">.</span><span class="nx">capacity</span> <span class="p">=</span> <span class="nb">len</span><span class="p">(</span><span class="nx">al</span><span class="p">.</span><span class="nx">array</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// Replace the old ArrayList with a new one</span>
</span></span><span class="line"><span class="cl">    <span class="nx">al</span><span class="p">.</span><span class="nx">array</span> <span class="p">=</span> <span class="nx">newArray</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="p">}</span> <span class="k">else</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// Insert the element into the existing ArrayList</span>
</span></span><span class="line"><span class="cl">    <span class="nx">al</span><span class="p">.</span><span class="nx">array</span> <span class="p">=</span> <span class="nb">append</span><span class="p">(</span><span class="nx">al</span><span class="p">.</span><span class="nx">array</span><span class="p">,</span> <span class="nx">x</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// Decrement the capacity by 1</span>
</span></span><span class="line"><span class="cl">    <span class="nx">al</span><span class="p">.</span><span class="nx">capacity</span> <span class="o">-=</span> <span class="mi">1</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">  <span class="nx">al</span> <span class="o">:=</span> <span class="nx">ArrayList</span><span class="p">{</span><span class="nx">capacity</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="nx">array</span><span class="p">:</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{}}</span> 
</span></span><span class="line"><span class="cl">  <span class="nx">al</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">al</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">al</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">al</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">al</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">3123</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;ArrayList after all elements are inserted:&#34;</span><span class="p">,</span> <span class="nx">al</span><span class="p">.</span><span class="nx">array</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Output</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Resizing ArrayList</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Resizing ArrayList</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Resizing ArrayList</span>
</span></span><span class="line"><span class="cl"><span class="c1">// ArrayList after all elements are inserted: [12 3 2 1 3123]</span>
</span></span></code></pre></div><p>The time complexity of inserting a new element in ArrayList is assumed to be $O(1)$, but if the list is already at max capacity then the time complexity will be $O(n)$ where $n$ is the number of existing elements.</p>
<h1 id="strings">Strings</h1>
<p>A string is like an array of characters. It is defined as a separate datatype, but most array operations like loop, slice, and indexing are interoperable.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">  <span class="nx">stringExample</span> <span class="o">:=</span> <span class="s">&#34;Hello, World&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;First character is:&#34;</span><span class="p">,</span> <span class="nx">stringExample</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Last character is:&#34;</span><span class="p">,</span> <span class="nx">stringExample</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="nx">stringExample</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Slice of string:&#34;</span><span class="p">,</span> <span class="nx">stringExample</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">i</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">stringExample</span><span class="p">);</span><span class="nx">i</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;Character at index: %d is: %c \n&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">stringExample</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Output</span>
</span></span><span class="line"><span class="cl"><span class="c1">// First character is: H</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Last character is: d</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Slice of string: ll</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 0 is: H </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 1 is: e </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 2 is: l </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 3 is: l </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 4 is: o </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 5 is: , </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 6 is:   </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 7 is: W </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 8 is: o </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 9 is: r </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 10 is: l </span>
</span></span><span class="line"><span class="cl"><span class="c1">// Character at index: 11 is: d</span>
</span></span></code></pre></div><h2 id="concatenation-of-strings">Concatenation of Strings</h2>
<p align="center"><img src="strings.png" alt="String Concatenation"></p>
<p>A program that concatenates two strings of size $m$ and $n$, has time complexity $O(m + n)$ or $O(2n)$ if both strings are of equal length.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">  <span class="s">&#34;fmt&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="s">&#34;strings&#34;</span>
</span></span><span class="line"><span class="cl"><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">concat</span><span class="p">(</span><span class="nx">stringA</span> <span class="kt">string</span><span class="p">,</span> <span class="nx">stringB</span> <span class="kt">string</span><span class="p">)(</span><span class="kt">string</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="c1">// Create a new list of strings</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// to store the concatenated string</span>
</span></span><span class="line"><span class="cl">  <span class="kd">var</span> <span class="nx">newString</span> <span class="p">[]</span><span class="kt">string</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="c1">// Copy stringA</span>
</span></span><span class="line"><span class="cl">  <span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">i</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">stringA</span><span class="p">);</span><span class="nx">i</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nx">newString</span> <span class="p">=</span> <span class="nb">append</span><span class="p">(</span><span class="nx">newString</span><span class="p">,</span> <span class="nb">string</span><span class="p">(</span><span class="nx">stringA</span><span class="p">[</span><span class="nx">i</span><span class="p">]))</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="c1">// Copy stringB</span>
</span></span><span class="line"><span class="cl">  <span class="k">for</span> <span class="nx">j</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">j</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">stringB</span><span class="p">);</span><span class="nx">j</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nx">newString</span> <span class="p">=</span> <span class="nb">append</span><span class="p">(</span><span class="nx">newString</span><span class="p">,</span> <span class="nb">string</span><span class="p">(</span><span class="nx">stringB</span><span class="p">[</span><span class="nx">j</span><span class="p">]))</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="c1">// Join the list of strings to create a new string</span>
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="nx">strings</span><span class="p">.</span><span class="nf">Join</span><span class="p">(</span><span class="nx">newString</span><span class="p">,</span> <span class="s">&#34;&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">  <span class="nx">stringA</span> <span class="o">:=</span> <span class="s">&#34;Hello&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="nx">stringB</span> <span class="o">:=</span> <span class="s">&#34;World&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;Concatenation of %s and %s is: %s \n&#34;</span><span class="p">,</span> <span class="nx">stringA</span><span class="p">,</span> <span class="nx">stringB</span><span class="p">,</span> 
</span></span><span class="line"><span class="cl">                <span class="nf">concat</span><span class="p">(</span><span class="nx">stringA</span><span class="p">,</span> <span class="nx">stringB</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Output</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Concatenation of Hello and World is: HelloWorld </span>
</span></span></code></pre></div><h2 id="stringbuilder">StringBuilder</h2>
<p>Using <strong>StringBuilder</strong> (a dynamically sized array similar to ArrayList) we can reduce the time complexity of operations performed on a string.</p>
<p>Upon initialization, the StringBuilder will load the characters of a string into an array. If we have to concatenate another string of length $n$, the array will be resized with time complexity $O(n)$. Once all operations are performed, the array in StringBuilder can be converted back to a string.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">  <span class="s">&#34;fmt&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="s">&#34;strings&#34;</span>
</span></span><span class="line"><span class="cl"><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">type</span> <span class="nx">StringBuilder</span> <span class="kd">struct</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nx">array</span> <span class="p">[]</span><span class="kt">string</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="p">(</span><span class="nx">sb</span> <span class="o">*</span><span class="nx">StringBuilder</span><span class="p">)</span> <span class="nf">load</span><span class="p">(</span><span class="nx">value</span> <span class="kt">string</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// We assume the time complexity of this function</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// to be O(1)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nx">sb</span><span class="p">.</span><span class="nx">array</span> <span class="p">=</span> <span class="nx">strings</span><span class="p">.</span><span class="nf">Split</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="s">&#34;&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="p">(</span><span class="nx">sb</span> <span class="o">*</span><span class="nx">StringBuilder</span><span class="p">)</span> <span class="nf">concat</span><span class="p">(</span><span class="nx">value</span> <span class="kt">string</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// Time complexity of concatenating a string of</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// length m will be O(m)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">i</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span><span class="nx">i</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">sb</span><span class="p">.</span><span class="nx">array</span> <span class="p">=</span> <span class="nb">append</span><span class="p">(</span><span class="nx">sb</span><span class="p">.</span><span class="nx">array</span><span class="p">,</span> <span class="nb">string</span><span class="p">(</span><span class="nx">value</span><span class="p">[</span><span class="nx">i</span><span class="p">]))</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="p">(</span><span class="nx">sb</span> <span class="o">*</span><span class="nx">StringBuilder</span><span class="p">)</span> <span class="nf">unload</span><span class="p">()(</span><span class="kt">string</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// Just like load() method the time complexity of</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// unloading an array to string will be assumed as O(1)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="nx">strings</span><span class="p">.</span><span class="nf">Join</span><span class="p">(</span><span class="nx">sb</span><span class="p">.</span><span class="nx">array</span><span class="p">,</span> <span class="s">&#34;&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">  <span class="nx">sb</span> <span class="o">:=</span> <span class="nx">StringBuilder</span><span class="p">{</span><span class="nx">array</span><span class="p">:</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{}}</span>
</span></span><span class="line"><span class="cl">  <span class="nx">sb</span><span class="p">.</span><span class="nf">load</span><span class="p">(</span><span class="s">&#34;Hello&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Instance of StringBuilder:&#34;</span><span class="p">,</span> <span class="nx">sb</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nx">sb</span><span class="p">.</span><span class="nf">concat</span><span class="p">(</span><span class="s">&#34;World&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;StringBuilder array after concatenation:&#34;</span><span class="p">,</span> <span class="nx">sb</span><span class="p">.</span><span class="nx">array</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Unloaded String:&#34;</span><span class="p">,</span> <span class="nx">sb</span><span class="p">.</span><span class="nf">unload</span><span class="p">())</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Output</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Instance of StringBuilder: {[H e l l o]}</span>
</span></span><span class="line"><span class="cl"><span class="c1">// StringBuilder array after concatenation: [H e l l o W o r l d]</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Unloaded String: HelloWorld</span>
</span></span></code></pre></div><h1 id="hashmaps">HashMaps</h1>
<p align="center"><img src="hashtables.png" alt="HashMap"></p>
<p>A <strong>HashMap</strong> is a data structure that stores values mapped to keys. It is generally used to store values that require frequent access, as the time complexity of accessing/searching an element in HashMap is $O(1)$.</p>
<p>The <a href="/posts/go/go-programming-language/#maps" target="_blank">Map</a> in Go, is an implementation of the HashMap data structure.</p>
<h2 id="hash-function">Hash Function</h2>
<p>While inserting a value in HashMap, a <strong>Hash Function</strong> calculates a hash from the input value and allocates a location to store the value.</p>
<p>A HashMap collision occurs when two values have the same hash or they are allocated the same storage location. In the case of collision, multiple values could be stored in the same location using an array or <a href="/posts/computer-science/interview-preparation/linked-lists" target="_blank">LinkedList</a>.</p>
<p>An ideal hash function will produce the least collisions, resulting in $O(1)$ lookup time. But in the worst-case scenario (all values are inserted in the same location) the lookup time will increase to $O(n)$, same as an array.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Go" data-lang="Go"><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">type</span> <span class="nx">HashMap</span> <span class="kd">struct</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nx">table</span> <span class="kd">map</span><span class="p">[</span><span class="kt">int</span><span class="p">][]</span><span class="kt">int</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">calculateHash</span><span class="p">(</span><span class="nx">value</span> <span class="kt">int</span><span class="p">)(</span><span class="kt">int</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="nx">value</span><span class="o">%</span><span class="mi">10</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="p">(</span><span class="nx">hm</span> <span class="o">*</span><span class="nx">HashMap</span><span class="p">)</span> <span class="nf">insert</span><span class="p">(</span><span class="nx">insertValue</span> <span class="kt">int</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// Calculate hash value from insertValue</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hash</span> <span class="o">:=</span> <span class="nf">calculateHash</span><span class="p">(</span><span class="nx">insertValue</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="c1">// Append the insertValue to the array </span>
</span></span><span class="line"><span class="cl">  <span class="c1">// present at &#34;hash&#34; key</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span><span class="p">.</span><span class="nx">table</span><span class="p">[</span><span class="nx">hash</span><span class="p">]</span> <span class="p">=</span> <span class="nb">append</span><span class="p">(</span><span class="nx">hm</span><span class="p">.</span><span class="nx">table</span><span class="p">[</span><span class="nx">hash</span><span class="p">],</span> <span class="nx">insertValue</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="p">(</span><span class="nx">hm</span> <span class="o">*</span><span class="nx">HashMap</span><span class="p">)</span> <span class="nf">search</span><span class="p">(</span><span class="nx">searchValue</span> <span class="kt">int</span><span class="p">)(</span><span class="kt">bool</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl">  <span class="c1">// Narrow down the search to the array stored </span>
</span></span><span class="line"><span class="cl">  <span class="c1">// at the &#34;hash&#34; key</span>
</span></span><span class="line"><span class="cl">  <span class="nx">searchSpace</span> <span class="o">:=</span> <span class="nx">hm</span><span class="p">.</span><span class="nx">table</span><span class="p">[</span><span class="nf">calculateHash</span><span class="p">(</span><span class="nx">searchValue</span><span class="p">)]</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="c1">// Search for searchValue in the array stored </span>
</span></span><span class="line"><span class="cl">  <span class="c1">// at &#34;hash&#34; key</span>
</span></span><span class="line"><span class="cl">  <span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="mi">0</span><span class="p">;</span><span class="nx">i</span><span class="p">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="nx">searchSpace</span><span class="p">);</span><span class="nx">i</span><span class="o">++</span><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="nx">searchValue</span> <span class="o">==</span> <span class="nx">searchSpace</span><span class="p">[</span><span class="nx">i</span><span class="p">]){</span>
</span></span><span class="line"><span class="cl">      <span class="k">return</span> <span class="kc">true</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="kc">false</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span> <span class="o">:=</span> <span class="nx">HashMap</span><span class="p">{</span><span class="nx">table</span><span class="p">:</span> <span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">int</span><span class="p">][]</span><span class="kt">int</span><span class="p">)}</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">65</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">67</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="nx">hm</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="mi">55</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;HashMap:&#34;</span><span class="p">,</span> <span class="nx">hm</span><span class="p">.</span><span class="nx">table</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nx">searchValue1</span> <span class="o">:=</span> <span class="mi">55</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Searching for&#34;</span><span class="p">,</span> <span class="nx">searchValue1</span><span class="p">,</span> <span class="s">&#34;in HashMap:&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                <span class="nx">hm</span><span class="p">.</span><span class="nf">search</span><span class="p">(</span><span class="nx">searchValue1</span><span class="p">))</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nx">searchValue2</span> <span class="o">:=</span> <span class="mi">2</span>
</span></span><span class="line"><span class="cl">  <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Searching for&#34;</span><span class="p">,</span> <span class="nx">searchValue2</span><span class="p">,</span> <span class="s">&#34;in HashMap:&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                <span class="nx">hm</span><span class="p">.</span><span class="nf">search</span><span class="p">(</span><span class="nx">searchValue2</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Output</span>
</span></span><span class="line"><span class="cl"><span class="c1">// HashMap: map[1:[1] 2:[12] 3:[23] 5:[65 55] 7:[67]]</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Searching for 55 in HashMap: true</span>
</span></span><span class="line"><span class="cl"><span class="c1">// Searching for 2 in HashMap: false</span>
</span></span></code></pre></div><h1 id="resources">Resources</h1>
<p><a href="https://www.freecodecamp.org/news/hash-tables/" target="_blank">Hash Table Explained: What it Is and How to Implement It</a><br>
<a href="https://www.freecodecamp.org/news/java-list-tutorial-util-list-api-example/" target="_blank">Java List Methods Tutorial – Util List API Example</a><br>
<a href="https://www.freecodecamp.org/news/efficient-string-building-in-javascript/" target="_blank">How to Work with Strings in JavaScript – Tips for Efficient String Concatenation</a></p>
]]></content:encoded>
    </item>
  </channel>
</rss>
