<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Blog by Muthya Varshith Vankamamidi]]></title><description><![CDATA[Varshith Sharma is an undergraduate Student studing in NIIT university. He is very much interested in working on full-stack , Cloud and Dev-ops]]></description><link>https://blog.iamvarshith.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1629379801403/rmDt9bVTf.png</url><title>Blog by Muthya Varshith Vankamamidi</title><link>https://blog.iamvarshith.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 15:03:21 GMT</lastBuildDate><atom:link href="https://blog.iamvarshith.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Dutch National Flag problem]]></title><description><![CDATA[Problem Statement

Given an array containing 0s, 1s and 2s, sort the array in place. You should treat numbers of the array as objects,
hence, we can’t count 0s, 1s, and 2s to recreate the array.
The flag of the Netherlands consists of three colours: ...]]></description><link>https://blog.iamvarshith.dev/dutch-national-flag-problem</link><guid isPermaLink="true">https://blog.iamvarshith.dev/dutch-national-flag-problem</guid><category><![CDATA[interview]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[data structures]]></category><category><![CDATA[General Programming]]></category><category><![CDATA[python beginner]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Sat, 05 Feb 2022 08:21:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/2VOgI53Wa6c/upload/v1644049186865/pG3w-AWX5.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-problem-statement"><strong>Problem Statement</strong></h2>
<blockquote>
<p>Given an array containing 0s, 1s and 2s, sort the array in place. You should treat numbers of the array as objects,
hence, we can’t count 0s, 1s, and 2s to recreate the array.</p>
<p>The flag of the Netherlands consists of three colours: red, white and blue; and since our input array also consists of
three different numbers that are why it is called Dutch National Flag problem.</p>
</blockquote>
<pre><code><span class="hljs-attr">Example 1:</span>

<span class="hljs-attr">Input:</span> [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>]
<span class="hljs-attr">Output:</span> [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]

<span class="hljs-attr">Example 2:</span>

<span class="hljs-attr">Input:</span> [<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>]
<span class="hljs-attr">Output:</span> [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>,]
</code></pre><h3 id="heading-so-what-is-the-plan"><strong> So what is the plan? </strong></h3>
<p> So the idea is to solve, We can use a Two Pointers approach while iterating through the array. Let’s say the two
 pointers are called low and high which are pointing to the first and the last element of the array respectively. 
 So while iterating, we will move all 0s before low and all 2s after high so that in the end, all 1s will be between 
 low and high.</p>
<h2 id="heading-lets-code-shall-we"><strong>Let's code, shall we!</strong></h2>
<pre><code>def dutch_flag(lst):
    lower_pointer <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
    high_pointer <span class="hljs-operator">=</span> len(lst) <span class="hljs-operator">-</span> <span class="hljs-number">1</span>
    iteration_pointer <span class="hljs-operator">=</span> <span class="hljs-number">0</span>

    <span class="hljs-keyword">while</span> iteration_pointer <span class="hljs-operator">&lt;</span><span class="hljs-operator">=</span> high_pointer:
        <span class="hljs-keyword">if</span> lst[iteration_pointer] <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>:
            iteration_pointer <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
        elif lst[iteration_pointer] <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>:
            lst[lower_pointer], lst[iteration_pointer] <span class="hljs-operator">=</span> lst[iteration_pointer], lst[lower_pointer]
            lower_pointer <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
            iteration_pointer <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
        elif lst[iteration_pointer] <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">2</span>:
            lst[high_pointer], lst[iteration_pointer] <span class="hljs-operator">=</span> lst[iteration_pointer], lst[high_pointer]
            high_pointer <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> lst


def main():
    arr <span class="hljs-operator">=</span> [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>]
    dutch_flag(arr)
    print(arr)

    arr <span class="hljs-operator">=</span> [<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>]
    dutch_flag(arr)
    print(arr)
</code></pre><p>The final out put is </p>
<blockquote>
<p>[0, 0, 1, 1, 2]
 [0, 0, 1, 2, 2, 2]</p>
</blockquote>
<p>Two </p>
]]></content:encoded></item><item><title><![CDATA[How to find the number of islands in a matrix]]></title><description><![CDATA[We all know that for any interview point of view the graph data structures are very much important. Because in most of the cases we end up having graph-based data structures in real-life problems.
Social Graph APIs such as Facebook's Graph API, Recom...]]></description><link>https://blog.iamvarshith.dev/how-to-find-the-number-of-islands-in-a-matrix</link><guid isPermaLink="true">https://blog.iamvarshith.dev/how-to-find-the-number-of-islands-in-a-matrix</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[interview]]></category><category><![CDATA[problem solving skills]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Mon, 18 Oct 2021 17:39:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1634575235581/p7UpOCcdB.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We all know that for any interview point of view the graph data structures are very much important. Because in most of the cases we end up having graph-based data structures in real-life problems.
Social Graph APIs such as Facebook's Graph API, Recommendation Engines such as Yelp's GraphQL API, Path Optimization Algorithms such as Google Maps Platform (Maps, Routes APIs) and Car Navigations, Web Analytics and Scientific Computations are some of the finest use cases for Graph Data Structures.</p>
<p>As a result, there is a good possibility that you will be evaluated on your implementations of graphs and trees, as well as other algorithms, during an interview. As a matter of fact, it is prudent for every developer to study and apply these data structures.</p>
<p>As part of that, I'll try to deconstruct one of the most popular interview questions about connected graphs in the business.</p>
<p>So let's get this party started.....</p>
<p>The question is as simple as it can get.</p>
<blockquote>
<p>Given a boolean 2D matrix, find the number of islands. A group of connected 1s forms an island. </p>
</blockquote>
<p>So that means you are given a matrix that is 2 dimensional. Which consist of 1's and 0's
where "0" is water and "1" island. there are asking you to find the number of islands in the matrix.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1634576791960/mkocMVK9oK.png" alt="autodraw 18_10_2021.png" /></p>
<p>Note: We have only considered the 4 sides of an element for a better understanding. Rather considering the diagonals sides as well.</p>
<p>Ok ! Now we have understood the problem. Let's see how to solve them. </p>
<p>So how would you solve this problem when you see this in your own interview.</p>
<p>PANIC 🥲 PANIC  🤷‍♂️</p>
<p>If you have no idea it's ok. All you need is to understand Connected Components in a Graph. Here is a geeksforgeeks  <a target="_blank" href="https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/">LINK</a> for you to have a quick look at it.</p>
<p>Now I assume you have a better understanding of Connected Components in a Graph. One can solve this by simply using the concept of connected components and DFS. 
So let's get into the code and I will explain along with the code for better understanding</p>
<pre><code>def islandfinder(graph: list):
    no_of_islands = <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> i in <span class="hljs-keyword">range</span>(<span class="hljs-built_in">len</span>(graph)):
        <span class="hljs-keyword">for</span> j in <span class="hljs-keyword">range</span>(<span class="hljs-built_in">len</span>(graph[i])):
            <span class="hljs-keyword">if</span> graph[i][j] == <span class="hljs-number">1</span>:
                <span class="hljs-built_in">print</span>(str(graph) + <span class="hljs-string">' '</span> + str(no_of_islands))
                no_of_islands += <span class="hljs-number">1</span>

                dfs(i, j, graph)
    <span class="hljs-built_in">print</span>(no_of_islands)
</code></pre><p>To begin with, I wrote a function that says <strong>island_finder</strong> </p>
<ul>
<li>what I intend to do is, to go to each element present in the 2D matrix and check if the element in the matrix[i][j] is "1" or "0" </li>
<li>If the element in the matrix[i][j] is a "1" that is water I want to find all the connected elements of that element. </li>
<li>To iterate through the matrix which is nothing but the list of lists in python. I have written two loops "i", "j" </li>
<li>And then if the condition is met I will pass the element to a dfs to find the element.</li>
</ul>
<p>So now we will see who I managed to write the dfs for this below.</p>
<pre><code><span class="hljs-attribute">def</span> dfs(i, j, graph):
    <span class="hljs-attribute">n</span> = len(graph[<span class="hljs-number">0</span>])
    <span class="hljs-attribute">m</span> = len(graph)
    <span class="hljs-attribute">if</span> i &lt; <span class="hljs-number">0</span> or i &gt;= m or j &lt; <span class="hljs-number">0</span> or j &gt;= n or graph[i][j] != <span class="hljs-number">1</span>:
        <span class="hljs-attribute">return</span>
    <span class="hljs-attribute">graph</span>[i][j] = <span class="hljs-number">2</span>

    <span class="hljs-attribute">dfs</span>(i, j + <span class="hljs-number">1</span>, graph)
    <span class="hljs-attribute">dfs</span>(i, j - <span class="hljs-number">1</span>, graph)
    <span class="hljs-attribute">dfs</span>(i + <span class="hljs-number">1</span>, j, graph)
    <span class="hljs-attribute">dfs</span>(i - <span class="hljs-number">1</span>, j, graph)
</code></pre><p>So this function dfs above does nothing but find DFS. But with a little twist. 😁</p>
<p>-So once the element is passed to this function that is dfs, The dfs first checks for the edge cases where the edge elements of the graphs can throw an error. </p>
<ul>
<li>once we ignore the edge cases we mark the present element "2" we can mark with anything. This just not to resend the element to <strong>find_island function </strong></li>
<li>And use a recursive function and pass the 4 sides of the given element to the same dfs function to find the connected elements.</li>
</ul>
<p>I have written the code with the inclusion of diagonal elements and with a different example below.</p>
<pre><code><span class="hljs-string">matrix</span> <span class="hljs-string">=</span> [
        [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
        [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
        [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
        [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
        [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
        [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
        [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
        [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>],
        [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
        [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]
    ]


<span class="hljs-string">def</span> <span class="hljs-string">island_finder(graph:</span> <span class="hljs-string">list):</span>
    <span class="hljs-string">no_of_islands</span> <span class="hljs-string">=</span> <span class="hljs-number">0</span>
    <span class="hljs-string">for</span> <span class="hljs-string">i</span> <span class="hljs-string">in</span> <span class="hljs-string">range(len(graph)):</span>
        <span class="hljs-string">for</span> <span class="hljs-string">j</span> <span class="hljs-string">in</span> <span class="hljs-string">range(len(graph[i])):</span>
            <span class="hljs-string">if</span> <span class="hljs-string">graph[i][j]</span> <span class="hljs-string">==</span> <span class="hljs-attr">1:</span>
                <span class="hljs-string">print(str(graph)</span> <span class="hljs-string">+</span> <span class="hljs-string">' '</span> <span class="hljs-string">+</span> <span class="hljs-string">str(no_of_islands))</span>
                <span class="hljs-string">no_of_islands</span> <span class="hljs-string">+=</span> <span class="hljs-number">1</span>

                <span class="hljs-string">dfs(i,</span> <span class="hljs-string">j,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">print(no_of_islands)</span>


<span class="hljs-string">def</span> <span class="hljs-string">dfs(i,</span> <span class="hljs-string">j,</span> <span class="hljs-string">graph):</span>
    <span class="hljs-string">n</span> <span class="hljs-string">=</span> <span class="hljs-string">len(graph[0])</span>
    <span class="hljs-string">m</span> <span class="hljs-string">=</span> <span class="hljs-string">len(graph)</span>
    <span class="hljs-string">if</span> <span class="hljs-string">i</span> <span class="hljs-string">&lt;</span> <span class="hljs-number">0</span> <span class="hljs-string">or</span> <span class="hljs-string">i</span> <span class="hljs-string">&gt;=</span> <span class="hljs-string">m</span> <span class="hljs-string">or</span> <span class="hljs-string">j</span> <span class="hljs-string">&lt;</span> <span class="hljs-number">0</span> <span class="hljs-string">or</span> <span class="hljs-string">j</span> <span class="hljs-string">&gt;=</span> <span class="hljs-string">n</span> <span class="hljs-string">or</span> <span class="hljs-string">graph[i][j]</span> <span class="hljs-type">!=</span> <span class="hljs-attr">1:</span>
        <span class="hljs-string">return</span>
    <span class="hljs-string">graph[i][j]</span> <span class="hljs-string">=</span> <span class="hljs-number">2</span>

    <span class="hljs-string">dfs(i,</span> <span class="hljs-string">j</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">dfs(i,</span> <span class="hljs-string">j</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">dfs(i</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">j,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">dfs(i</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">j,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">dfs(i</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">j</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">dfs(i</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">j</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">dfs(i</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">j</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">graph)</span>
    <span class="hljs-string">dfs(i</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">j</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-string">graph)</span>


<span class="hljs-string">island_finder(matrix)</span>
</code></pre><p>That's it. Now pass the matrix to the<strong> find_island function</strong> and enjoy the result. 😁😎</p>
<p>Happy coding guys. see you next time with some more interesting problems. Till then NAMSTEY 👏👏🙌🙏</p>
]]></content:encoded></item><item><title><![CDATA[How to enable Multi-Factor Authentication (MFA) for S3 buckets?]]></title><description><![CDATA[The security of any company should be a key priority with so many different services accessible in the cloud.
The preservation of data should indeed be at the core of every business avoiding unintended deletion.
By configuring buckets to delete with ...]]></description><link>https://blog.iamvarshith.dev/how-to-enable-multi-factor-authentication-mfa-for-s3-buckets</link><guid isPermaLink="true">https://blog.iamvarshith.dev/how-to-enable-multi-factor-authentication-mfa-for-s3-buckets</guid><category><![CDATA[Amazon S3]]></category><category><![CDATA[AWS]]></category><category><![CDATA[Security]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Sun, 05 Sep 2021 08:03:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630821371182/BN9WqdPYo.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The security of any company should be a key priority with so many different services accessible in the cloud.
The preservation of data should indeed be at the core of every business avoiding unintended deletion.
By configuring buckets to delete with MFA, you could offer a degree of additional protection to the AWS S3, which can assist prevent accidental bucket deletion and its contents. </p>
<p>Now in this blog, I will try to put comprehensible steps of <strong>How to enable Multi-Factor Authentication (MFA) for S3 buckets?</strong> and at the same to time <strong>how to delete the MFA</strong></p>
<p>So here are the prerequisites for MFA</p>
<ul>
<li>AWS account with root user access ( To activate MFA )</li>
<li>AWS CLI installed on your local system</li>
<li>MFA Device ( Oauth App or any MFA apps )</li>
</ul>
<p>Now MFA in Amazon S3 can only be configured with<strong> AWS CLI</strong> and there is no other way around this. So let's get started...</p>
<ol>
<li>First and very foremost let's head on to AWS and create an S3 bucket but remember MFA can only be activated for Versioned buckets. And also the bucket's name should be unique, So I have chosen <strong>mfatestbucket</strong> as the bucket name.</li>
</ol>
<p>Now once you click on Create Bucket button you will be redirected to a new page just like displayed below </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630823214481/Glxgu3d6E.png" alt="Screenshot 2021-09-05 115633.png" /></p>
<p>On this new page, you are required to give a name for your bucket. Remember that bucket's name should be unique. And chose your region. Since I am in India and ap-south-1 is the closest data center for me I have chosen this. But you are welcome to pick any of the AWS regions you want. And can also copy setting from existing buckets if you wish to.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630823320760/Am9v30OFh.png" alt="Screenshot 2021-09-05 115312.png" /></p>
<p>Block Public Access setting - Remember it is always efficient to let your buckets be Private and available from the public. But sometimes S3 can be used to deploy static websites. In such cases, you are required to let your bucket be public. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630823859086/mw683E1BL.png" alt="Screenshot 2021-09-05 115344.png" /></p>
<p>Bucket Versioning - Amazon S3 versioning is a way of maintaining several item versions in the same bucket. The S3 versioning capabilities allow you to save, recover and restore each version of any item stored in your buckets. By versioning, both unexpected user activities and program faults may make it easier to recover. If Amazon S3 receives several requests for writing the same subject at a time, it saves all those objects once versioning is enabled for a bucket.<strong> And It is also required to Enable MFA in S3 Buckets  </strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630824020732/4rk-1JlFq.png" alt="Screenshot 2021-09-05 115418.png" /></p>
<p>Additional options- There are some additional options like Tags, Encryptions, etc. But in this block, I am interested in concentrating only on enabling MFA from AWS CLI. But feel free to explore. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630824254753/FrpVUU-cX.png" alt="Screenshot 2021-09-05 115440.png" /></p>
<p>Now the bucket has been created with versioning we can check this by going to <strong>mfatestbucket</strong> properties. Here we can see that the properties and in these properties, we have Bucket Versioning.
Which says the Bucket Versioning is enabled but the MFA is Disabled</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630824816886/G8y8RxUnL.png" alt="Screenshot 2021-09-05 122326.png" /></p>
<p>And it is time to go to MFA enabling process. By going to the security credentials page as shown below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630825088841/HDIUB4EWf.png" alt="Screenshot 2021-09-05 121740.png" /></p>
<p>And then you will reach Your Security Credentials page which is used to manage the credentials for your AWS account. In there you can see the MFA option just like displayed below and click on that to add the MFA with your Google Authentication app...</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630825140717/KAGqzoJ31.png" alt="Screenshot 2021-09-05 121825.png" /></p>
<p>Now choose Virtual MFA for using MFA with Google Authentication app or any Authentication app.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630825341686/btivmuv4I.png" alt="Screenshot 2021-09-05 121919.png" /></p>
<p>And then open your Google Authentication app and scan the QR code.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630826045394/3Lleb-b1Y.png" alt="Screenshot 2021-09-05 121938.png" /></p>
<p>And Enter the Two Consiquite Code that displays on your APP. <em>Note the code reshuffles every one Minute. so enter two codes sequentially</em> And then Click on Assign MFA</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630826116428/sI-mRzr_Y.png" alt="Screenshot 2021-09-05 122140.png" /></p>
<p>Now Your Account is set with your MFA App (Google Authentication app). So now we shall go to cmd and access AWS CLI. If not installed please click here  <a target="_blank" href="https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-windows.html">here</a> to do so. </p>
<p><strong>Remember that the AWS profile Should be the root AWS user, not an IAM User </strong></p>
<pre><code>aws s3api <span class="hljs-keyword">list</span>-buckets
</code></pre><pre><code>{
    <span class="hljs-attr">"Buckets"</span>: [

        {
            <span class="hljs-attr">"Name"</span>: <span class="hljs-string">"mfatestbucket"</span>,
            <span class="hljs-attr">"CreationDate"</span>: <span class="hljs-string">"2021-09-05T06:25:20+00:00"</span>
        }

    ],
    <span class="hljs-attr">"Owner"</span>: {
        <span class="hljs-attr">"ID"</span>: <span class="hljs-string">"1ab1de8ca8eexxxxxxxxxxxxxxxxxxxxxxxxxxx"</span>
    }
}
</code></pre><p>Now we can see the created bucket in here. Now let's see how to enable the MFA.</p>
<pre><code>aws s3api put-bucket-versioning  
<span class="hljs-comment">-- profile {root}  </span>
<span class="hljs-comment">-- bucket {my-bucket-name} </span>
<span class="hljs-comment">-- versioning-configuration Status=Enabled,MFADelete=Enabled</span>
<span class="hljs-comment">-- mfa “{arn key} ”</span>
</code></pre><p>This is the command structure. Remember '{' , '}' are not in the command they are just to let you see the variable parts.</p>
<ul>
<li><p>aws s3api put-bucket-versioning -- is the command to put versioning in s3</p>
</li>
<li><p>Where { root } is the local root profile, Is your default profile is root you can ignore this.</p>
</li>
<li><p>bucket {my-bucket-name} is your bucket name</p>
</li>
<li><p>versioning-configuration Status=Enabled, MFADelete=Enabled - is to enable MFA</p>
</li>
<li><p>mfa {arn key }  -- arn can be found in the MFA setting in the profile and Key = your Google authentication app key (( Remember they must be separated by space )) </p>
</li>
</ul>
<p>Copy the arn key -- In the MFA setting as shown below. and hold on to it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630828096968/dprWj4oYQ.png" alt="Screenshot 2021-09-05 131806.png" /></p>
<p>Let's see How this command works in my case. </p>
<pre><code>aws s3api put-bucket-versioning 
<span class="hljs-comment">--profile root  </span>
<span class="hljs-comment">--bucket mfatestbucket </span>
<span class="hljs-comment">--versioning-configuration Status=Enabled,MFADelete=Enabled </span>
<span class="hljs-comment">--mfa "arn:aws:iam::xxxxxx<span class="hljs-doctag">xxx:</span>mfa/root-account-mfa-device 817166 "</span>
</code></pre><p>This is the command I have Used with my case. Bacasue </p>
<ul>
<li>I have a profile named <strong>root</strong> which is the root of my AWS. </li>
<li>I have bucket Name <strong>mfatestbucket</strong></li>
<li>I want versioning-configuration Status=Enabled, MFADelete=Enabled</li>
<li>I have copied my ARN As shown above, and pasted it here from the Security Credentials page </li>
<li>My Oauth token from google App Is <strong>817166</strong></li>
</ul>
<p><strong><em>In the same manner, you can also disable the MFA with the same command but With MFADelete = Disable </em></strong></p>
<pre><code>aws s3api put-bucket-versioning 
<span class="hljs-comment">--profile root  </span>
<span class="hljs-comment">--bucket mfatestbucket </span>
<span class="hljs-comment">--versioning-configuration Status=Enabled,MFADelete=Disable </span>
<span class="hljs-comment">--mfa "arn:aws:iam::xxxxxx<span class="hljs-doctag">xxx:</span>mfa/root-account-mfa-device 817166 "</span>
</code></pre><p>And Finally The MFA has been activated </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630828407034/oHVgscfefi.png" alt="image.png" /></p>
<p>🙌🙌👋🙋‍♂️  Happy Learning  🙌🙌👋🙋‍♂️</p>
]]></content:encoded></item><item><title><![CDATA[How to Deploy the static website to S3 using GitHub actions.]]></title><description><![CDATA[Every one of us, at some point, would need to deploy a static website. It might be because the page is just a portfolio or a single-page JavaScript application that would not need heavy frameworks. So most of us have to host a static website, It migh...]]></description><link>https://blog.iamvarshith.dev/how-to-deploy-the-static-website-to-s3-using-github-actions</link><guid isPermaLink="true">https://blog.iamvarshith.dev/how-to-deploy-the-static-website-to-s3-using-github-actions</guid><category><![CDATA[Amazon S3]]></category><category><![CDATA[AWS]]></category><category><![CDATA[github-actions]]></category><category><![CDATA[automation]]></category><category><![CDATA[static]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Sat, 28 Aug 2021 09:42:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630128707238/kEiOnl5cN.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Every one of us, at some point, would need to deploy a static website. It might be because the page is just a portfolio or a single-page JavaScript application that would not need heavy frameworks. So most of us have to host a static website, It might be with S3, Azure Blob Storage, etc. So while doing that most of us will simple copy the files to the S3 ( In my case ). Yet it might be a static website but the bugs or the correction will be there for sure. Then you have to manually replace the files with the S3. So today I will automate this whole process of S3 deploying using Github Actions. So let's get started.</p>
<h3 id="so-what-is-git-hub-actions">So what is git hub actions</h3>
<p>You may construct custom lifecycle routines of software development straight into your Github repository via Github Actions. The workflows consist of several activities known as actions which may be carried out under specific circumstances automatically. This allows you to add continuous integration (CI), CD- and many more functions right in your repository. With that said let's get started.</p>
<h2 id="prerequisites">Prerequisites</h2>
<ul>
<li>AWS Account or IAM User </li>
<li>Static Website</li>
<li>Github Repo</li>
</ul>
<p><em>Skip to Step - 2 if you already have a GitHub repo set.</em></p>
<h3 id="step-1-create-a-github-repo">Step - 1 Create A Github Repo</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630130417127/eGm1iZ8Y0.png" alt="Screenshot 2021-08-28 112730.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630130435478/NzIZ1xTWy.png" alt="Screenshot 2021-08-28 112858.png" /></p>
<h3 id="step-2-place-the-code-in-the-repository">Step -2 Place The code in the repository</h3>
<p>Now your repository is created, So let's start uploading your own Html, CSS, Js, Images, etc. in the repository. In case you want a dummy static website for a practice copy the code from below  </p>
<p><strong>spin.html</strong></p>
<pre><code><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Spin Wheel Using JS<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"./spin.css"</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-comment">&lt;!-- partial:index.partial.html --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">center</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"chart"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"question"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">center</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://d3js.org/d3.v3.min.js"</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-comment">&lt;!-- partial --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span>  <span class="hljs-attr">src</span>=<span class="hljs-string">"./spin.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre><p><strong>spin.css</strong></p>
<pre><code>
<span class="hljs-selector-tag">body</span>{
    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(technical2.png);
}

<span class="hljs-selector-tag">text</span>{
        <span class="hljs-attribute">font-family</span>:Helvetica, Arial, sans-serif;
        <span class="hljs-attribute">font-size</span>:<span class="hljs-number">11px</span>;
        <span class="hljs-attribute">pointer-events</span>:none;
    }
    <span class="hljs-selector-id">#chart</span>{
       <span class="hljs-comment">/* position:absolute;

        width:500px;
        height:500px;
        top:0;
        left:0;*/</span>
    }
    <span class="hljs-selector-id">#question</span>{
        <span class="hljs-attribute">position</span>: absolute;
        <span class="hljs-attribute">width</span>:<span class="hljs-number">400px</span>;
        <span class="hljs-attribute">height</span>:<span class="hljs-number">500px</span>;
        <span class="hljs-attribute">top</span>:<span class="hljs-number">180px</span>;
        <span class="hljs-attribute">left</span>:<span class="hljs-number">1000px</span>;
    }
    <span class="hljs-selector-id">#question</span> <span class="hljs-selector-tag">h1</span>{
        <span class="hljs-attribute">color</span>: white;
        <span class="hljs-attribute">text-shadow</span>: -<span class="hljs-number">1px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#fe0201</span>, <span class="hljs-number">1px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#06ffff</span>, -<span class="hljs-number">2px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#fe0201</span>, <span class="hljs-number">2px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#06ffff</span>, -<span class="hljs-number">3px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#fe0201</span>, <span class="hljs-number">3px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#06ffff</span>, -<span class="hljs-number">4px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#fe0201</span>, <span class="hljs-number">4px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#06ffff</span>, -<span class="hljs-number">5px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#fe0201</span>, <span class="hljs-number">5px</span> <span class="hljs-number">0px</span> <span class="hljs-number">#06ffff</span>;
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">90px</span>;
        <span class="hljs-attribute">font-weight</span>: bold;
        <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"Helvetica Neue"</span>, Helvetica, Arial, sans-serif;
        <span class="hljs-attribute">position</span>: absolute;
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
        <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
        <span class="hljs-attribute">top</span>:<span class="hljs-number">50%</span>;
        <span class="hljs-attribute">-webkit-transform</span>:<span class="hljs-built_in">translate</span>(<span class="hljs-number">0</span>,-<span class="hljs-number">50%</span>);
                <span class="hljs-attribute">transform</span>:<span class="hljs-built_in">translate</span>(<span class="hljs-number">0</span>,-<span class="hljs-number">50%</span>);
    }
</code></pre><p><strong>spin.js</strong></p>
<pre><code><span class="hljs-keyword">var</span> padding = {<span class="hljs-attr">top</span>:<span class="hljs-number">20</span>, <span class="hljs-attr">right</span>:<span class="hljs-number">40</span>, <span class="hljs-attr">bottom</span>:<span class="hljs-number">0</span>, <span class="hljs-attr">left</span>:<span class="hljs-number">0</span>},
            w = <span class="hljs-number">500</span> - padding.left - padding.right,
            h = <span class="hljs-number">500</span> - padding.top  - padding.bottom,
            r = <span class="hljs-built_in">Math</span>.min(w, h)/<span class="hljs-number">2</span>,
            rotation = <span class="hljs-number">0</span>,
            oldrotation = <span class="hljs-number">0</span>,
            picked = <span class="hljs-number">100000</span>,
            oldpick = [],
            color = d3.scale.category20();<span class="hljs-comment">//category20c()</span>
            <span class="hljs-comment">//randomNumbers = getRandomNumbers();</span>
        <span class="hljs-comment">//http://osric.com/bingo-card-generator/?title=HTML+and+CSS+BINGO!&amp;words=padding%2Cfont-family%2Ccolor%2Cfont-weight%2Cfont-size%2Cbackground-color%2Cnesting%2Cbottom%2Csans-serif%2Cperiod%2Cpound+sign%2C%EF%B9%A4body%EF%B9%A5%2C%EF%B9%A4ul%EF%B9%A5%2C%EF%B9%A4h1%EF%B9%A5%2Cmargin%2C%3C++%3E%2C{+}%2C%EF%B9%A4p%EF%B9%A5%2C%EF%B9%A4!DOCTYPE+html%EF%B9%A5%2C%EF%B9%A4head%EF%B9%A5%2Ccolon%2C%EF%B9%A4style%EF%B9%A5%2C.html%2CHTML%2CCSS%2CJavaScript%2Cborder&amp;freespace=true&amp;freespaceValue=Web+Design+Master&amp;freespaceRandom=false&amp;width=5&amp;height=5&amp;number=35#results</span>
        <span class="hljs-keyword">var</span> data = [
                    {<span class="hljs-string">"label"</span>:<span class="hljs-string">""</span>,  <span class="hljs-string">"value"</span>:<span class="hljs-number">1</span>,  <span class="hljs-string">"question"</span>:<span class="hljs-string">"Team 1"</span>}, <span class="hljs-comment">// padding</span>
                    {<span class="hljs-string">"label"</span>:<span class="hljs-string">""</span>,  <span class="hljs-string">"value"</span>:<span class="hljs-number">2</span>,  <span class="hljs-string">"question"</span>:<span class="hljs-string">"Team 2"</span>}, <span class="hljs-comment">//font-family</span>
                    {<span class="hljs-string">"label"</span>:<span class="hljs-string">""</span>,  <span class="hljs-string">"value"</span>:<span class="hljs-number">3</span>,  <span class="hljs-string">"question"</span>:<span class="hljs-string">"Team 3"</span>}, <span class="hljs-comment">//color</span>
                    {<span class="hljs-string">"label"</span>:<span class="hljs-string">""</span>,  <span class="hljs-string">"value"</span>:<span class="hljs-number">4</span>,  <span class="hljs-string">"question"</span>:<span class="hljs-string">"Team 4"</span>}, <span class="hljs-comment">//font-weight</span>
                    {<span class="hljs-string">"label"</span>:<span class="hljs-string">""</span>,  <span class="hljs-string">"value"</span>:<span class="hljs-number">5</span>,  <span class="hljs-string">"question"</span>:<span class="hljs-string">"Team 5"</span>}, <span class="hljs-comment">//font-size</span>
                    {<span class="hljs-string">"label"</span>:<span class="hljs-string">""</span>,  <span class="hljs-string">"value"</span>:<span class="hljs-number">6</span>,  <span class="hljs-string">"question"</span>:<span class="hljs-string">"Team 6"</span>}, <span class="hljs-comment">//background-color</span>
                    <span class="hljs-comment">/*{"label":"IPAD PRO",  "value":7,  "question":"Which word is used for specifying an HTML tag that is inside another tag?"}, //nesting*/</span>
                    <span class="hljs-comment">/*{"label":"LAND",  "value":8,  "question":"Which side of the box is the third number in: margin:1px 1px 1px 1px; ?"}, //bottom
                    {"label":"MOTOROLLA",  "value":9,  "question":"What are the fonts that don't have serifs at the ends of letters called?"}, //sans-serif
                    {"label":"BMW", "value":10, "question":"With CSS selectors, what character prefix should one use to specify a class?"}*/</span>
        ];
        <span class="hljs-keyword">var</span> svg = d3.select(<span class="hljs-string">'#chart'</span>)
            .append(<span class="hljs-string">"svg"</span>)
            .data([data])
            .attr(<span class="hljs-string">"width"</span>,  w + padding.left + padding.right)
            .attr(<span class="hljs-string">"height"</span>, h + padding.top + padding.bottom);
        <span class="hljs-keyword">var</span> container = svg.append(<span class="hljs-string">"g"</span>)
            .attr(<span class="hljs-string">"class"</span>, <span class="hljs-string">"chartholder"</span>)
            .attr(<span class="hljs-string">"transform"</span>, <span class="hljs-string">"translate("</span> + (w/<span class="hljs-number">2</span> + padding.left) + <span class="hljs-string">","</span> + (h/<span class="hljs-number">2</span> + padding.top) + <span class="hljs-string">")"</span>);
        <span class="hljs-keyword">var</span> vis = container
            .append(<span class="hljs-string">"g"</span>);

        <span class="hljs-keyword">var</span> pie = d3.layout.pie().sort(<span class="hljs-literal">null</span>).value(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">d</span>)</span>{<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;});
        <span class="hljs-comment">// declare an arc generator function</span>
        <span class="hljs-keyword">var</span> arc = d3.svg.arc().outerRadius(r);
        <span class="hljs-comment">// select paths, use arc generator to draw</span>
        <span class="hljs-keyword">var</span> arcs = vis.selectAll(<span class="hljs-string">"g.slice"</span>)
            .data(pie)
            .enter()
            .append(<span class="hljs-string">"g"</span>)
            .attr(<span class="hljs-string">"class"</span>, <span class="hljs-string">"slice"</span>);

        arcs.append(<span class="hljs-string">"path"</span>)
            .attr(<span class="hljs-string">"fill"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">d, i</span>)</span>{ <span class="hljs-keyword">return</span> color(i); })
            .attr(<span class="hljs-string">"d"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">d</span>) </span>{ <span class="hljs-keyword">return</span> arc(d); });
        <span class="hljs-comment">// add the text</span>
        arcs.append(<span class="hljs-string">"text"</span>).attr(<span class="hljs-string">"transform"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">d</span>)</span>{
                d.innerRadius = <span class="hljs-number">0</span>;
                d.outerRadius = r;
                d.angle = (d.startAngle + d.endAngle)/<span class="hljs-number">2</span>;
                <span class="hljs-keyword">return</span> <span class="hljs-string">"rotate("</span> + (d.angle * <span class="hljs-number">180</span> / <span class="hljs-built_in">Math</span>.PI - <span class="hljs-number">90</span>) + <span class="hljs-string">")translate("</span> + (d.outerRadius <span class="hljs-number">-10</span>) +<span class="hljs-string">")"</span>;
            })
            .attr(<span class="hljs-string">"text-anchor"</span>, <span class="hljs-string">"end"</span>)
            .text( <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">d, i</span>) </span>{
                <span class="hljs-keyword">return</span> data[i].label;
            });
        container.on(<span class="hljs-string">"click"</span>, spin);
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">spin</span>(<span class="hljs-params">d</span>)</span>{

            container.on(<span class="hljs-string">"click"</span>, <span class="hljs-literal">null</span>);
            <span class="hljs-comment">//all slices have been seen, all done</span>
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"OldPick: "</span> + oldpick.length, <span class="hljs-string">"Data length: "</span> + data.length);
            <span class="hljs-keyword">if</span>(oldpick.length == data.length){
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"done"</span>);
                container.on(<span class="hljs-string">"click"</span>, <span class="hljs-literal">null</span>);
                <span class="hljs-keyword">return</span>;
            }
            <span class="hljs-keyword">var</span>  ps       = <span class="hljs-number">360</span>/data.length,
                 pieslice = <span class="hljs-built_in">Math</span>.round(<span class="hljs-number">1440</span>/data.length),
                 rng      = <span class="hljs-built_in">Math</span>.floor((<span class="hljs-built_in">Math</span>.random() * <span class="hljs-number">1440</span>) + <span class="hljs-number">360</span>);

            rotation = (<span class="hljs-built_in">Math</span>.round(rng / ps) * ps);

            picked = <span class="hljs-built_in">Math</span>.round(data.length - (rotation % <span class="hljs-number">360</span>)/ps);
            picked = picked &gt;= data.length ? (picked % data.length) : picked;
            <span class="hljs-keyword">if</span>(oldpick.indexOf(picked) !== <span class="hljs-number">-1</span>){
                d3.select(<span class="hljs-built_in">this</span>).call(spin);
                <span class="hljs-keyword">return</span>;
            } <span class="hljs-keyword">else</span> {
                oldpick.push(picked);
            }
            rotation += <span class="hljs-number">90</span> - <span class="hljs-built_in">Math</span>.round(ps/<span class="hljs-number">2</span>);
            vis.transition()
                .duration(<span class="hljs-number">3000</span>)
                .attrTween(<span class="hljs-string">"transform"</span>, rotTween)
                .each(<span class="hljs-string">"end"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
                    <span class="hljs-comment">//mark question as seen</span>
                    d3.select(<span class="hljs-string">".slice:nth-child("</span> + (picked + <span class="hljs-number">1</span>) + <span class="hljs-string">") path"</span>)
                        .attr(<span class="hljs-string">"fill"</span>, <span class="hljs-string">"#111"</span>);
                    <span class="hljs-comment">//populate question</span>
                    d3.select(<span class="hljs-string">"#question h1"</span>)
                        .text(data[picked].question);
                    oldrotation = rotation;

                    <span class="hljs-comment">/* Get the result value from object "data" */</span>
                    <span class="hljs-built_in">console</span>.log(data[picked].value)

                    <span class="hljs-comment">/* Comment the below line for restrict spin to sngle time */</span>
                    container.on(<span class="hljs-string">"click"</span>, spin);
                });
        }
        <span class="hljs-comment">//make arrow</span>
        svg.append(<span class="hljs-string">"g"</span>)
            .attr(<span class="hljs-string">"transform"</span>, <span class="hljs-string">"translate("</span> + (w + padding.left + padding.right) + <span class="hljs-string">","</span> + ((h/<span class="hljs-number">2</span>)+padding.top) + <span class="hljs-string">")"</span>)
            .append(<span class="hljs-string">"path"</span>)
            .attr(<span class="hljs-string">"d"</span>, <span class="hljs-string">"M-"</span> + (r*<span class="hljs-number">.15</span>) + <span class="hljs-string">",0L0,"</span> + (r*<span class="hljs-number">.05</span>) + <span class="hljs-string">"L0,-"</span> + (r*<span class="hljs-number">.05</span>) + <span class="hljs-string">"Z"</span>)
            .style({<span class="hljs-string">"fill"</span>:<span class="hljs-string">"black"</span>});
        <span class="hljs-comment">//draw spin circle</span>
        container.append(<span class="hljs-string">"circle"</span>)
            .attr(<span class="hljs-string">"cx"</span>, <span class="hljs-number">0</span>)
            .attr(<span class="hljs-string">"cy"</span>, <span class="hljs-number">0</span>)
            .attr(<span class="hljs-string">"r"</span>, <span class="hljs-number">60</span>)
            .style({<span class="hljs-string">"fill"</span>:<span class="hljs-string">"white"</span>,<span class="hljs-string">"cursor"</span>:<span class="hljs-string">"pointer"</span>});
        <span class="hljs-comment">//spin text</span>
        container.append(<span class="hljs-string">"text"</span>)
            .attr(<span class="hljs-string">"x"</span>, <span class="hljs-number">0</span>)
            .attr(<span class="hljs-string">"y"</span>, <span class="hljs-number">15</span>)
            .attr(<span class="hljs-string">"text-anchor"</span>, <span class="hljs-string">"middle"</span>)
            .text(<span class="hljs-string">"SPIN"</span>)
            .style({<span class="hljs-string">"font-weight"</span>:<span class="hljs-string">"bold"</span>, <span class="hljs-string">"font-size"</span>:<span class="hljs-string">"30px"</span>});


        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rotTween</span>(<span class="hljs-params">to</span>) </span>{
          <span class="hljs-keyword">var</span> i = d3.interpolate(oldrotation % <span class="hljs-number">360</span>, rotation);
          <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">t</span>) </span>{
            <span class="hljs-keyword">return</span> <span class="hljs-string">"rotate("</span> + i(t) + <span class="hljs-string">")"</span>;
          };
        }


        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getRandomNumbers</span>(<span class="hljs-params"></span>)</span>{
            <span class="hljs-keyword">var</span> array = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint16Array</span>(<span class="hljs-number">1000</span>);
            <span class="hljs-keyword">var</span> scale = d3.scale.linear().range([<span class="hljs-number">360</span>, <span class="hljs-number">1440</span>]).domain([<span class="hljs-number">0</span>, <span class="hljs-number">100000</span>]);
            <span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.hasOwnProperty(<span class="hljs-string">"crypto"</span>) &amp;&amp; <span class="hljs-keyword">typeof</span> <span class="hljs-built_in">window</span>.crypto.getRandomValues === <span class="hljs-string">"function"</span>){
                <span class="hljs-built_in">window</span>.crypto.getRandomValues(array);
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"works"</span>);
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-comment">//no support for crypto, get crappy random numbers</span>
                <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000</span>; i++){
                    array[i] = <span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * <span class="hljs-number">100000</span>) + <span class="hljs-number">1</span>;
                }
            }
            <span class="hljs-keyword">return</span> array;
        }
</code></pre><p>and place any image as a background 😊</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630132496804/nPgLUwbxg.png" alt="Screenshot 2021-08-28 114433.png" /></p>
<p>This repository Looks like this 
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630132524693/01QHd9uJV.png" alt="Screenshot 2021-08-28 120435.png" /></p>
<h3 id="step-3-aws-time">Step - 3 AWS time !</h3>
<p><strong>Log on to your AWS account and create IAM User with programmatic access and do as following -</strong></p>
<p>And Now select the Security Credentials option </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630133984641/9SuGKuAaq.png" alt="Screenshot 2021-08-28 121329.png" /></p>
<p>And then select download options</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630143300807/5iYTCnJfE.png" alt="Screenshot 2021-08-28 134956.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630133999907/r1NufziO_.png" alt="Screenshot 2021-08-28 122200.png" /></p>
<p>Once the ID and key are downloaded. hold on to them. and now create an S3 Bucket from the AWS console. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630142222732/caRk8wnee.png" alt="Screenshot 2021-08-28 at 12-33-17 S3 bucket.png" /></p>
<p>I have Named my bucket SPIN, <strong>but remember that when you are creating an S3 for your domain say example.com then the S3 name must be the name of your domain. Else the CNAM from your Domain will not work </strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630142233769/v2GromXWP.png" alt="Screenshot 2021-08-28 at 12-33-52 S3 bucket.png" /></p>
<p>And in permissions - Bucket policy add the following code</p>
<pre><code>{
    <span class="hljs-attr">"Version"</span>: <span class="hljs-string">"2012-10-17"</span>,
    <span class="hljs-attr">"Statement"</span>: [
        {
            <span class="hljs-attr">"Sid"</span>: <span class="hljs-string">"PublicReadGetObject"</span>,
            <span class="hljs-attr">"Effect"</span>: <span class="hljs-string">"Allow"</span>,
            <span class="hljs-attr">"Principal"</span>: <span class="hljs-string">"*"</span>,
            <span class="hljs-attr">"Action"</span>: [
                <span class="hljs-string">"s3:GetObject"</span>
            ],
            <span class="hljs-attr">"Resource"</span>: [
                <span class="hljs-string">"arn:aws:s3:::Bucket-Name/*"</span> ( Replace this with your bucket name ) 
            ]
        }
    ]
}
</code></pre><p>And then in properties of S3 Enable your Static web Hosting and give your page homepage name like Index.html</p>
<p>Now Get back to the GitHub repo create a File with the name <strong>.github</strong> in that folder make another folder Named <strong>workflows</strong> in that put the code below with file name </p>
<p><strong>main.yml</strong></p>
<pre><code><span class="hljs-attribute">name</span>: Upload Website

<span class="yaml"><span class="hljs-attr">on:</span>
  <span class="hljs-attr">push:</span>
    <span class="hljs-attr">branches:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">main</span>

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">iamvarshith_dev:</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
    <span class="hljs-attr">steps:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Checkout</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v1</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Configure</span> <span class="hljs-string">AWS</span> <span class="hljs-string">Credentials</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">aws-actions/configure-aws-credentials@v1</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">aws-access-key-id:</span> <span class="hljs-string">${{</span> <span class="hljs-string">secrets.AWS_ACCESS_KEY_ID</span> <span class="hljs-string">}}</span>
          <span class="hljs-attr">aws-secret-access-key:</span> <span class="hljs-string">${{</span> <span class="hljs-string">secrets.AWS_SECRET_ACCESS_KEY</span> <span class="hljs-string">}}</span>
<span class="hljs-comment"># aws-region</span>
          <span class="hljs-attr">aws-region:</span> <span class="hljs-string">us-east-1(</span> <span class="hljs-string">Replace</span> <span class="hljs-string">with</span> <span class="hljs-string">your</span> <span class="hljs-string">AWS</span> <span class="hljs-string">Bucket</span> <span class="hljs-string">Region)</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Deploy</span> <span class="hljs-string">static</span> <span class="hljs-string">site</span> <span class="hljs-string">to</span> <span class="hljs-string">S3</span> <span class="hljs-string">bucket</span>
<span class="hljs-comment"># Your bucker name</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">aws</span> <span class="hljs-string">s3</span> <span class="hljs-string">sync</span> <span class="hljs-string">.</span> <span class="hljs-string">s3://Bucket_name</span> <span class="hljs-string">--delete</span></span>
</code></pre><p>And Now find Secrets in the setting just like shown below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630143035328/-KUPcvvr0.png" alt="Screenshot 2021-08-28 145550.png" /></p>
<p>And then add the AWS credentials which you have downloaded before from AWS.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630143051037/KhPJkS771H.png" alt="Screenshot 2021-08-28 145705.png" /></p>
<p>Do the same with both<strong> AWS_ACCESS_KEY_ID</strong> and  <strong>AWS_SECRET_ACCESS_KEY </strong> remember that the names of these variables should not be changed. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1630143059076/vsJu21LJQ.png" alt="Screenshot 2021-08-28 145802.png" /></p>
<p>Done ! Now push any update onto the main branch and go to GitHub actions to see if there is an error. Enjoy your automatic Push upgradable AWS S3 bucket with GitHub actions.</p>
]]></content:encoded></item><item><title><![CDATA[How to find the length of the longest substring in it with no more than K distinct characters ?]]></title><description><![CDATA[Today we shall a sliding window pattern question.

How to the length of the longest substring in it with no more than K distinct characters ?

The first and foremost step to solve the problem is to understand the question and figure what is given and...]]></description><link>https://blog.iamvarshith.dev/how-to-find-the-length-of-the-longest-substring-in-it-with-no-more-than-k-distinct-characters</link><guid isPermaLink="true">https://blog.iamvarshith.dev/how-to-find-the-length-of-the-longest-substring-in-it-with-no-more-than-k-distinct-characters</guid><category><![CDATA[100DaysOfCode]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Sun, 22 Aug 2021 12:00:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629704153095/CkDyXmQiRt.gif" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Today we shall a sliding window pattern question.</p>
<blockquote>
<p>How to the length of the longest substring in it with no more than K distinct characters ?</p>
</blockquote>
<p>The first and foremost step to solve the problem is to understand the question and figure what is given and what are we supposed to return as a result.</p>
<p>In this problem, we are given a string say <strong>str</strong>  and an integer<strong> k </strong>.  We are asked to return an integer say <strong>max_size</strong> which is the length of the longest sub string with less than the <strong>"k"</strong> number of distinct characters.  Let's see the examples.</p>
<pre><code>Example 1:

Input: String="araaci", K=2
Output: 4
Explanation: The longest substring <span class="hljs-keyword">with</span> <span class="hljs-keyword">no</span> more <span class="hljs-keyword">than</span> <span class="hljs-string">'2'</span> <span class="hljs-keyword">distinct</span> <span class="hljs-keyword">characters</span> <span class="hljs-keyword">is</span> <span class="hljs-string">"araa"</span>.

Example <span class="hljs-number">2</span>:

<span class="hljs-keyword">Input</span>: <span class="hljs-keyword">String</span>=<span class="hljs-string">"araaci"</span>, K=<span class="hljs-number">1</span>
<span class="hljs-keyword">Output</span>: <span class="hljs-number">2</span>
Explanation: The longest <span class="hljs-keyword">substring</span> <span class="hljs-keyword">with</span> <span class="hljs-keyword">no</span> more <span class="hljs-keyword">than</span> <span class="hljs-string">'1'</span> <span class="hljs-keyword">distinct</span> <span class="hljs-keyword">characters</span> <span class="hljs-keyword">is</span> <span class="hljs-string">"aa"</span>.

Example <span class="hljs-number">3</span>:

<span class="hljs-keyword">Input</span>: <span class="hljs-keyword">String</span>=<span class="hljs-string">"cbbebi"</span>, K=<span class="hljs-number">3</span>
<span class="hljs-keyword">Output</span>: <span class="hljs-number">5</span>
Explanation: The longest substrings <span class="hljs-keyword">with</span> <span class="hljs-keyword">no</span> more <span class="hljs-keyword">than</span> <span class="hljs-string">'3'</span> <span class="hljs-keyword">distinct</span> <span class="hljs-keyword">characters</span> <span class="hljs-keyword">are</span> <span class="hljs-string">"cbbeb"</span> &amp; <span class="hljs-string">"bbebi"</span>.

Example <span class="hljs-number">4</span>:

<span class="hljs-keyword">Input</span>: <span class="hljs-keyword">String</span>=<span class="hljs-string">"cbbebi"</span>, K=<span class="hljs-number">10</span>
<span class="hljs-keyword">Output</span>: <span class="hljs-number">6</span>
Explanation: The longest <span class="hljs-keyword">substring</span> <span class="hljs-keyword">with</span> <span class="hljs-keyword">no</span> more <span class="hljs-keyword">than</span> <span class="hljs-string">'10'</span> <span class="hljs-keyword">distinct</span> <span class="hljs-keyword">characters</span> <span class="hljs-keyword">is</span> <span class="hljs-string">"cbbebi"</span>.
</code></pre><p>With this we have finally understood the question, So let's get started on thinking about how to solve the question</p>
<p>We can use a sliding  <a target="_blank" href="https://www.geeksforgeeks.org/window-sliding-technique/">window pattern</a></p>
<ul>
<li>We'll start by inserting characters from the beginning of the string until the HashMap has K distinct characters.</li>
<li>Our sliding window will be formed by these characters. We're supposed to discover the window with the fewest number of unique characters. The length of this window will be remembered as the longest thus far.</li>
<li>Following that, we'll incrementally add one character to the sliding window (i.e., slide the window forward).</li>
<li>If the number of different characters in the HashMap is more than K, we'll try to reduce the window from the beginning of each phase. We'll reduce the size of the window until the HashMap contains no more than K unique characters.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629719639515/q8xjMa5YT.png" alt="Array.png" /></p>
<p>Let's get to the code ! </p>
<pre><code>def Longest_Substring_with_K_Distinct_Characters(<span class="hljs-keyword">string</span>, k):
    max_size = <span class="hljs-number">0</span>
    window_start = <span class="hljs-number">0</span>
    hashmap = {}
    <span class="hljs-keyword">for</span> window_end in <span class="hljs-keyword">range</span>(<span class="hljs-built_in">len</span>(<span class="hljs-keyword">string</span>)):
        right_char = <span class="hljs-keyword">string</span>[window_end]
        <span class="hljs-keyword">if</span> right_char not in hashmap:
            hashmap[right_char] = <span class="hljs-number">0</span>
        hashmap[right_char] += <span class="hljs-number">1</span>

        while <span class="hljs-built_in">len</span>(hashmap) &gt; k:
            hashmap[<span class="hljs-keyword">string</span>[window_start]] -= <span class="hljs-number">1</span>
            <span class="hljs-keyword">if</span> hashmap[<span class="hljs-keyword">string</span>[window_start]] == <span class="hljs-number">0</span>:
                del hashmap[<span class="hljs-keyword">string</span>[window_start]]

            window_start += <span class="hljs-number">1</span>
            max_size = max(max_size, window_end - window_start + <span class="hljs-number">1</span>)
    <span class="hljs-keyword">return</span> max_size


<span class="hljs-keyword">string</span> = <span class="hljs-string">"araaci"</span>
k = <span class="hljs-number">2</span>

<span class="hljs-built_in">print</span>(Longest_Substring_with_K_Distinct_Characters(<span class="hljs-keyword">string</span>, k))
</code></pre><p>Happy coding 🙌🙌✌️👋👋</p>
]]></content:encoded></item><item><title><![CDATA[Day-3 Squaring a Sorted Array]]></title><description><![CDATA[Hello, Today we shall solve a problem where we are given a sorted array, create a new array containing squares of all the numbers of the input array in the sorted order.
Any thoughts on how to solve this already  ? 
Great, for this problem we will us...]]></description><link>https://blog.iamvarshith.dev/day-3-squaring-a-sorted-array</link><guid isPermaLink="true">https://blog.iamvarshith.dev/day-3-squaring-a-sorted-array</guid><category><![CDATA[100DaysOfCode]]></category><category><![CDATA[dailydev]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Fri, 20 Aug 2021 20:46:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629578793314/QGJKSNyvV.gif" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello, Today we shall solve a problem where we are given a sorted array, create a new array containing squares of all the numbers of the input array in the <strong>sorted order</strong>.</p>
<p>Any thoughts on how to solve this already  ? </p>
<p>Great, for this problem we will use a Two-pointer type solution. Before that, we shall understand the question. Here we are given an array say <strong>arr</strong> which contains any integers. And asked to return an array of the squares of the elements in the <strong>arr</strong> in a sorted manner. Now I hope you understand the problem statement.</p>
<blockquote>
<p>Example 1:</p>
</blockquote>
<pre><code>  <span class="hljs-attr">Input:</span> [<span class="hljs-number">-2</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]

  <span class="hljs-attr">Output:</span> [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>]
</code></pre><blockquote>
<p>Example 2:</p>
</blockquote>
<pre><code><span class="hljs-attr">Input:</span> [<span class="hljs-number">-3</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]

<span class="hljs-attr">Output:</span> [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>]
</code></pre><p>Now let's see how can we solve this problem ? This is an easy question to answer. The only difficulty is that we can have negative integers in the input array, which makes generating the output array with squares in sorted order a little more difficult.</p>
<p>Finding the index of the first non-negative number in the array might be a simpler technique. After that, we may iterate the array with Two Pointers. The non-negative numbers will be iterated with one pointer moving ahead, while the negative numbers will be iterated with the other pointer moving backward. At any point in the process, the number that results in a larger square will be added to the output array.</p>
<p>But there is one more solution, which is much better than the one mentioned above.</p>
<p>An alternative technique may be to apply two pointers beginning at each end of the input array, as the numbers at both ends can give us the biggest square. At any point in the process, we add the larger square to the result array and advance to the next/previous number indicated by the pointer.</p>
<ul>
<li>Create an array for the results and fill it with 0s initially</li>
<li>Then create a variable say <strong>square_index</strong> for storing the last index of the result array and set it to length - 1 .</li>
<li>Then take two pointers say <strong>right_pointer</strong> <strong>left_pointer</strong> and set <strong>length(array) -1 , 0</strong> respectively</li>
<li>Then check which of the left pointer square is greater or the right square. And then insert in the result array and increment the pointers when the condition is reached and also increment the <strong>square_index</strong></li>
</ul>
<p>Let's See the code in python3</p>
<pre><code><span class="hljs-string">def</span> <span class="hljs-string">square_elemnts(arr):</span>

    <span class="hljs-string">result_arr</span> <span class="hljs-string">=</span> [<span class="hljs-number">0</span> <span class="hljs-string">for</span> <span class="hljs-string">i</span> <span class="hljs-string">in</span> <span class="hljs-string">range(len(arr))</span>]
    <span class="hljs-string">left_pointer</span> <span class="hljs-string">=</span> <span class="hljs-number">0</span>
    <span class="hljs-string">square_index</span> <span class="hljs-string">=</span> <span class="hljs-string">len(arr)</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span>
    <span class="hljs-string">right_pointer</span> <span class="hljs-string">=</span> <span class="hljs-string">len(arr)</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span>
    <span class="hljs-string">while</span> <span class="hljs-string">left_pointer</span> <span class="hljs-string">&lt;=</span> <span class="hljs-attr">right_pointer:</span>
        <span class="hljs-string">squareleft</span> <span class="hljs-string">=</span> <span class="hljs-string">arr[left_pointer]</span> <span class="hljs-string">*</span> <span class="hljs-string">arr[left_pointer]</span>
        <span class="hljs-string">squareright</span> <span class="hljs-string">=</span> <span class="hljs-string">arr[right_pointer]</span> <span class="hljs-string">*</span> <span class="hljs-string">arr[right_pointer]</span>
        <span class="hljs-string">if</span> <span class="hljs-string">squareleft</span> <span class="hljs-string">&gt;</span> <span class="hljs-attr">squareright:</span>
            <span class="hljs-string">result_arr[square_index]</span> <span class="hljs-string">=</span> <span class="hljs-string">squareleft</span>
            <span class="hljs-string">left_pointer</span> <span class="hljs-string">+=</span> <span class="hljs-number">1</span>
        <span class="hljs-attr">else:</span>
            <span class="hljs-string">result_arr[square_index]</span> <span class="hljs-string">=</span> <span class="hljs-string">squareright</span>
            <span class="hljs-string">right_pointer</span> <span class="hljs-string">-=</span> <span class="hljs-number">1</span>
        <span class="hljs-string">square_index</span> <span class="hljs-string">-=</span> <span class="hljs-number">1</span>
    <span class="hljs-string">return</span> <span class="hljs-string">result_arr</span>


<span class="hljs-string">print(square_elemnts([-2,</span> <span class="hljs-number">-1</span><span class="hljs-string">,</span> <span class="hljs-number">0</span><span class="hljs-string">,</span> <span class="hljs-number">2</span><span class="hljs-string">,</span> <span class="hljs-number">3</span><span class="hljs-string">]))</span>
</code></pre><pre><code><span class="hljs-attribute">result</span>:[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>, <span class="hljs-number">16</span>]
</code></pre>]]></content:encoded></item><item><title><![CDATA[How to remove the key and return the new length of the array without using extra space ?]]></title><description><![CDATA[Today we shall solve another Two-Pointer problem where you are given an unsorted array of numbers and a target ‘key’, remove all instances of ‘key’ in-place and return the new length of the array.
This is similar to the last problem we solved on this...]]></description><link>https://blog.iamvarshith.dev/how-to-remove-the-key-and-return-the-new-length-of-the-array-without-using-extra-space</link><guid isPermaLink="true">https://blog.iamvarshith.dev/how-to-remove-the-key-and-return-the-new-length-of-the-array-without-using-extra-space</guid><category><![CDATA[algorithms]]></category><category><![CDATA[100DaysOfCode]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Thu, 19 Aug 2021 18:42:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629571427679/AXB_tiZDp.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Today we shall solve another Two-Pointer problem where you are given an unsorted array of numbers and a target ‘key’, remove all instances of ‘key’ in-place and return the new length of the array.</p>
<p>This is similar to the last problem we solved on this blog on  <a target="_blank" href="https://blog.iamvarshith.dev/two-pointer">day one</a>  !. Try it on your own for some time before you look into the solution. Just think outside the box before writing the code. The perfect way to think of a solution is thinking , what would be an easy way for you to explain to a 5-Year-old how to do it. I think this is the trick for effortlessly writing a code for most of the problems .</p>
<p>So let's get started ... !</p>
<blockquote>
<p>Problem 1: Given an unsorted array of numbers and a target ‘key’, remove all instances of ‘key’ in-place and return the new length of the array.</p>
</blockquote>
<pre><code><span class="hljs-string">Test</span> <span class="hljs-string">cases</span> <span class="hljs-string">!</span>

<span class="hljs-attr">Example 1:</span>

<span class="hljs-attr">Input:</span> [<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">9</span>, <span class="hljs-number">3</span>]<span class="hljs-string">,</span> <span class="hljs-string">Key=3</span>
<span class="hljs-attr">Output:</span> <span class="hljs-number">4</span>
<span class="hljs-attr">Explanation:</span> <span class="hljs-string">The</span> <span class="hljs-string">first</span> <span class="hljs-string">four</span> <span class="hljs-string">elements</span> <span class="hljs-string">after</span> <span class="hljs-string">removing</span> <span class="hljs-string">every</span> <span class="hljs-string">'Key'</span> <span class="hljs-string">will</span> <span class="hljs-string">be</span> [<span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">10</span>, <span class="hljs-number">9</span>]<span class="hljs-string">.</span>

<span class="hljs-attr">Example 2:</span>

<span class="hljs-attr">Input:</span> [<span class="hljs-number">2</span>, <span class="hljs-number">11</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]<span class="hljs-string">,</span> <span class="hljs-string">Key=2</span>
<span class="hljs-attr">Output:</span> <span class="hljs-number">2</span>
<span class="hljs-attr">Explanation:</span> <span class="hljs-string">The</span> <span class="hljs-string">first</span> <span class="hljs-string">two</span> <span class="hljs-string">elements</span> <span class="hljs-string">after</span> <span class="hljs-string">removing</span> <span class="hljs-string">every</span> <span class="hljs-string">'Key'</span> <span class="hljs-string">will</span> <span class="hljs-string">be</span> [<span class="hljs-number">11</span>, <span class="hljs-number">1</span>]<span class="hljs-string">.</span>
</code></pre><p>So what do you think ! how should one solve this problem ? </p>
<p>As it is clear we are using two pointers to solve this. So for this case, the objective is to remove the <strong>key</strong> which is a duplicate element in the given array say <strong>arr</strong>. 
If you see in the first case the given <strong>key</strong> is 3. And at the 0th index, we have the 3 ! So in this case, we have to replace it with the next non-key element. that in this case, is<strong> '2'</strong>. </p>
<ul>
<li>So what we can do is take a pointer and point it to the 0th index value of an array. Say <strong>index_arr</strong></li>
<li>Now take another pointer that increments for every iteration. Say <strong>next</strong></li>
<li>Once we have these two pointers. We can make the next iterate and find the elements that are not equal to the KEY.</li>
<li>And then replace the found element of index arr[next] with arr[index_arr] </li>
<li>And then increment the <strong>index_arr</strong> by one</li>
<li>And return the <strong>index_arr</strong> which is the new length of the substring !</li>
</ul>
<p>So let's see the code !</p>
<pre><code><span class="hljs-attribute">def</span> remove_duplicates_given(arr, key):
    <span class="hljs-attribute">key_element</span> = <span class="hljs-number">0</span>

    <span class="hljs-attribute">for</span> index in range(len(arr)):
        <span class="hljs-attribute">if</span> arr[index] != key:
            <span class="hljs-attribute">arr</span>[key_element] = arr[index]
            <span class="hljs-attribute">key_element</span> += <span class="hljs-number">1</span>
    <span class="hljs-attribute">return</span> key_element


<span class="hljs-attribute">print</span>(remove_duplicates_given([<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">9</span>, <span class="hljs-number">3</span>], <span class="hljs-number">3</span>))
<span class="hljs-attribute">print</span>(remove_duplicates_given([<span class="hljs-number">2</span>, <span class="hljs-number">11</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>], <span class="hljs-number">2</span>))
</code></pre><h3 id="which-give-us-the-desired-result">which give us the desired result</h3>
<pre><code><span class="hljs-number">4</span>
<span class="hljs-number">2</span>
</code></pre><p>See you tomorrow with a new and interesting problem 😊😊🙌👍</p>
]]></content:encoded></item><item><title><![CDATA[Two Pointer]]></title><description><![CDATA[Problem Statement
Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space; after removing the duplicates in place return the length of the subarray that has no duplicate in it.
Example 1:

Input: [2, 3, 3, ...]]></description><link>https://blog.iamvarshith.dev/two-pointer</link><guid isPermaLink="true">https://blog.iamvarshith.dev/two-pointer</guid><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Thu, 19 Aug 2021 17:52:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629309234664/lon3yJY3T.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Problem Statement</p>
<p>Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space; after removing the duplicates in place return the length of the subarray that has no duplicate in it.</p>
<pre><code><span class="hljs-attr">Example 1:</span>

<span class="hljs-attr">Input:</span> [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">9</span>, <span class="hljs-number">9</span>]
<span class="hljs-attr">Output:</span> <span class="hljs-number">4</span>
<span class="hljs-attr">Explanation:</span> <span class="hljs-string">The</span> <span class="hljs-string">first</span> <span class="hljs-string">four</span> <span class="hljs-string">elements</span> <span class="hljs-string">after</span> <span class="hljs-string">removing</span> <span class="hljs-string">the</span> <span class="hljs-string">duplicates</span> <span class="hljs-string">will</span> <span class="hljs-string">be</span> [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">9</span>]<span class="hljs-string">.</span>
</code></pre><pre><code><span class="hljs-attr">Example 2:</span>

<span class="hljs-attr">Input:</span> [<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">11</span>]
<span class="hljs-attr">Output:</span> <span class="hljs-number">2</span>
<span class="hljs-attr">Explanation:</span> <span class="hljs-string">The</span> <span class="hljs-string">first</span> <span class="hljs-string">two</span> <span class="hljs-string">elements</span> <span class="hljs-string">after</span> <span class="hljs-string">removing</span> <span class="hljs-string">the</span> <span class="hljs-string">duplicates</span> <span class="hljs-string">will</span> <span class="hljs-string">be</span> [<span class="hljs-number">2</span>, <span class="hljs-number">11</span>]
</code></pre><p>So how do we solve this ? Let's put our thinking caps on! ......</p>
<p>After thinking for a while, you might find a solution where you use a two-pointer and just count the repeated elements.  Which gives you the answer.</p>
<pre><code><span class="hljs-attribute">def</span> remove_duplicates(arr):
    <span class="hljs-attribute">duplicates</span> = <span class="hljs-number">0</span>

    <span class="hljs-attribute">for</span> pointer_start in range(<span class="hljs-number">1</span>, len(arr)):
        <span class="hljs-attribute">pointer_end</span> = pointer_start - <span class="hljs-number">1</span>
        <span class="hljs-attribute">if</span> arr[pointer_start] == arr[pointer_end]:
            <span class="hljs-attribute">duplicates</span> += <span class="hljs-number">1</span>
    <span class="hljs-attribute">return</span> len(arr) - duplicates


<span class="hljs-attribute">print</span>(remove_duplicates([<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">9</span>, <span class="hljs-number">9</span>]))
<span class="hljs-attribute">print</span>(remove_duplicates([<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">11</span>]))
</code></pre><p>Output:</p>
<pre><code><span class="hljs-number">4</span>
<span class="hljs-number">2</span>
</code></pre><p> But We should not forget the given condition </p>
<blockquote>
<p>You should remove all duplicates from the array !
Now one might ask that the arrays are immutable. I understand your confusion, Array size is immutable not the array itself. So we can manipulate the data once created not the size !.</p>
</blockquote>
<p>So how should one solve this ? </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629308246447/RoM6QZQDt.png" alt="image.png" /></p>
<p>This is a very good explanation I found online. What are we doing is we are taking two pointers </p>
<ol>
<li>Next </li>
<li>nextNonDuplicate </li>
</ol>
<p>We traverse two pointers just as shown in the image. </p>
<pre><code><span class="hljs-string">def</span> <span class="hljs-string">remove_duplicates(arr):</span>
  <span class="hljs-comment"># index of the next non-duplicate element</span>
  <span class="hljs-string">next_non_duplicate</span> <span class="hljs-string">=</span> <span class="hljs-number">1</span>

  <span class="hljs-string">i</span> <span class="hljs-string">=</span> <span class="hljs-number">1</span>
  <span class="hljs-string">while(i</span> <span class="hljs-string">&lt;</span> <span class="hljs-string">len(arr)):</span>
    <span class="hljs-string">if</span> <span class="hljs-string">arr[next_non_duplicate</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">]</span> <span class="hljs-type">!=</span> <span class="hljs-string">arr[i]:</span>
      <span class="hljs-string">arr[next_non_duplicate]</span> <span class="hljs-string">=</span> <span class="hljs-string">arr[i]</span>
      <span class="hljs-string">next_non_duplicate</span> <span class="hljs-string">+=</span> <span class="hljs-number">1</span>
    <span class="hljs-string">i</span> <span class="hljs-string">+=</span> <span class="hljs-number">1</span>
  <span class="hljs-string">return</span> <span class="hljs-string">next_non_duplicate</span>
</code></pre>]]></content:encoded></item><item><title><![CDATA[100 Days of code !]]></title><description><![CDATA[Yes ! you heard me right. I am very much interested in writing and sharing the progress of my interview preparation in this blog. Starting from tomorrow, I will be using different problems to learn Algorithms with Data Structures.
In this journey, I ...]]></description><link>https://blog.iamvarshith.dev/100-days-of-code</link><guid isPermaLink="true">https://blog.iamvarshith.dev/100-days-of-code</guid><category><![CDATA[algorithms]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[data structures]]></category><category><![CDATA[100DaysOfCode]]></category><dc:creator><![CDATA[Muthya Varshith Vankamamidi]]></dc:creator><pubDate>Wed, 18 Aug 2021 11:11:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629285600292/ID3yKCZbX.gif" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Yes ! you heard me right. I am very much interested in writing and sharing the progress of my interview preparation in this blog. Starting from tomorrow, I will be using different problems to learn Algorithms with Data Structures.</p>
<p>In this journey, I would love to solve different kinds of problems by which I can learn and understand the Why and how to use different Data structures in different scenarios. </p>
<h2 id="why-and-what-are-algorithms">Why and What are Algorithms</h2>
<p> For this question, if I put it bluntly. Computers are dumb ! Yes, they are ! They don't know  What to do ? When to do it?  How to do it ? anything. But all they know is to follow the instructions given to them. So since a machine is so dumb we have to write everything in a form of Binary for it to understand. So one might wonder that my python or java code is not is binary, how will my machine understand me ? To answer it simply, your java or any other language is called a high-level language. At the end of the compilation, it will be Binary. Compilers are not a topic of this blog so we shall discuss them in detail some other time.</p>
<p>Let's get back to the point. The systematic code you write for a machine to understand without any ambiguity can be called an Algorithm. YES, that means your "Hello World program is also an algorithm.
But in this complex world of cloud computing, your print statements is very little.</p>
<h2 id="different-types-of-algorithms">Different types of algorithms</h2>
<p>There are numerous algorithms out in the world. But there are a few which are fundamental for the rest</p>
<ol>
<li>Recursive Algorithm</li>
<li>Divide and Conquer Algorithm</li>
<li>Dynamic Programming Algorithm</li>
<li>Brute Force Algorithm</li>
<li>Backtracking Algorithm</li>
<li>Greedy Algorithm</li>
</ol>
<p>In this process of 100 Day, I will be discussing each one of the above Algorithms</p>
<h2 id="and-also-in-i-would-highly-recommend-you-to-join-the-100-days-coding-cult-i-bet-it-will-be-great">And also in I would highly recommend you to join the 100-days Coding cult. I bet It will be great.</h2>
<p>Benefits: What the #100DaysOfCode can do for you</p>
<p>There are several good reasons you should consider committing to this challenge:</p>
<h3 id="coding-will-become-a-daily-habit-for-you-a-habit-that-you-can-easily-maintain-after-youve-finished-the-challenge">Coding will become a daily habit for you — a habit that you can easily maintain after you’ve finished the challenge.</h3>
<ul>
<li><p>Every day that you consistently code, you’ll build momentum. That momentum will make it easier for you to learn more advanced topics. You won’t have to spend extra time trying to remember what you did previously. You can stay in the “flow” of coding.</p>
</li>
<li><p>You’ll make friends and meet like-minded people who are also working through this challenge alongside you. They’ll help you find the strength to keep coding even on the days when you don’t feel like you’re making progress. They can also help you when you inevitably get stuck.</p>
</li>
<li><p>The projects that you’ll build will be small in scope, so by the time you finish, you’ll have completed several of them — and gained a wide range of experience.</p>
</li>
<li><p>If you were just working through tutorials, you wouldn’t have much to show for it. But with #100DaysOfCode, you’ll build real portfolio projects that you can show to potential employers and share with your family.</p>
</li>
<li><p>These projects will give you practice with concepts that frequently come up during developer job interviews.</p>
</li>
<li><p>Your GitHub profile will look extremely active. And yes, hiring managers and recruiters do look at these.</p>
</li>
<li><p>You’ll greatly diminish your fear of starting a new coding project. It will become a natural, ordinary thing to do.</p>
</li>
<li><p>You’ll have a good reason to stop procrastinating and start coding every day.</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>