<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nayan Pahuja</title>
    <description>The latest articles on DEV Community by Nayan Pahuja (@pahujanayan).</description>
    <link>https://dev.to/pahujanayan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1095098%2Fd7f2c2f3-136d-4b11-acdd-6b29b1300d4b.png</url>
      <title>DEV Community: Nayan Pahuja</title>
      <link>https://dev.to/pahujanayan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pahujanayan"/>
    <language>en</language>
    <item>
      <title>Cracking the Code: Unlocking Password-Protected PDFs with Masked Brute Force</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Sun, 01 Oct 2023 17:44:02 +0000</pubDate>
      <link>https://dev.to/pahujanayan/cracking-the-code-unlocking-password-protected-pdfs-with-masked-brute-force-2ekh</link>
      <guid>https://dev.to/pahujanayan/cracking-the-code-unlocking-password-protected-pdfs-with-masked-brute-force-2ekh</guid>
      <description>&lt;p&gt;Hey Guys! This article is something I am not used to writing so, If I miss any spots, please feel free to correct me in the comments. I am looking forward to constructive feedback.&lt;/p&gt;

&lt;p&gt;With that in mind, Let's start the article.&lt;/p&gt;

&lt;p&gt;We will be using the following resources for this article, I will be linking them with their explanation incase anyone wants to follow along.&lt;/p&gt;




&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.openwall.com/john/" rel="noopener noreferrer"&gt;&lt;strong&gt;John the Ripper&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;John the Ripper is an incredible open source software designed for the purpose of recovering lost or forgotten passwords through various password cracking techniques. It is highly versatile and can be used to crack a wide range of password types, including those stored in various cryptographic formats and hash algorithms.&lt;/p&gt;

&lt;p&gt;We are going to work with the Jumbo version of JohnTheRipper. This is a community-enhanced, "jumbo" version of John the Ripper. It has a lot of code, documentation, and data contributed by the user community. This is not "official" John the Ripper code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hashcat.net/hashcat/" rel="noopener noreferrer"&gt;&lt;strong&gt;HashCat&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hashcat is also another incredible open source software password cracking tool that is widely used by security professionals, penetration testers, and researchers to recover passwords from various types of hashed data.&lt;br&gt;
It is known for it's wide support for multiple hash algorithms and fast speed.&lt;/p&gt;


&lt;h2&gt;
  
  
  Setup the environment:
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Fun Story: I am writing this article because I needed to crack a password today for a document that was emailed to me, but I couldn't open it.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  John The Ripper:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F85rjs5b9vtqa2c2jg6q0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F85rjs5b9vtqa2c2jg6q0.png" alt="John" width="800" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download Windows binaries for Jumbo version and place it in the users directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Install Perl
&lt;/h3&gt;

&lt;p&gt;We are also going to need to install Perl programming language for this to run JtR.&lt;br&gt;
Download the Strawberry version of Perl, the link to which can be found here: &lt;strong&gt;&lt;a href="https://strawberryperl.com/" rel="noopener noreferrer"&gt;Perl&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Download the installer according to your respective bit for your desktop/laptop.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: Remember to restart your laptop after installing perl.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvuzs9cmd8mdu0w57gzp3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvuzs9cmd8mdu0w57gzp3.png" alt="Perl" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Download HashCat:
&lt;/h3&gt;

&lt;p&gt;Download the Hashcat binaries and place it in a directory. For convenience I am going to place it in users as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdxk3fi8qd6ebjvjmhb4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdxk3fi8qd6ebjvjmhb4.png" alt="Hashcat" width="800" height="108"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Cracking the password:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Make sure to unzip the all the stuff that we have downloaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's get to the magic part.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit the directory where you placed John The Ripper.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv6kgbxa3u14mntguvbhz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv6kgbxa3u14mntguvbhz.png" alt="JtR1" width="468" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Change your current directory to run using cd run.&lt;/p&gt;

&lt;p&gt;You can follow the commands as I did above to get to the directory.&lt;/p&gt;

&lt;p&gt;If you run the command &lt;code&gt;dir&lt;/code&gt; in windows cmd and &lt;code&gt;ls&lt;/code&gt; in UNIX, you can see all the directories features that Jack the Ripper offers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnsht4gyvx6d9p35uzs2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnsht4gyvx6d9p35uzs2.png" alt="JtR2" width="501" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Out of this we need the command &lt;code&gt;pdf2john&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;John the Ripper is primarily focused on password cracking and hashing-related tasks, and it doesn't directly handle PDF cracking. To work with a password-protected PDF file in John the Ripper, you first need to create a hash file using the 'pdf2john.pl' tool, which is available in the 'run' directory after compiling John the Ripper from its source code. This Perl script tool allows you to extract the hash (metadata information) from the PDF file and save it to a new file using the following command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I have a test pdf by the named pdf here which is protected by the password 2020!.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You must place your pdf inside the run directory of Jack The Ripper for convenience or specify the path in the command. I am going to do the earlier here.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5x0on4qpexi40ukbiv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5x0on4qpexi40ukbiv1.png" alt="JtR3" width="735" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you check the run directory with your file explorer you would see something like this in it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvshnvg7h04t17mgz8yfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvshnvg7h04t17mgz8yfm.png" alt="Notepad1" width="800" height="60"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We only need the hash key not the file name so I am going to remove that from our hash key and place the key.txt in hashcat directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;We are done with the work of John The Ripper now. We can proceed with using Hashcat for further process.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;


&lt;h3&gt;
  
  
  Hashcat:
&lt;/h3&gt;

&lt;p&gt;We are going to use the masking approach for this the documentation to which can be found here:&lt;/p&gt;

&lt;p&gt;You can open another terminal for this or use the same and change your directory to this.&lt;/p&gt;

&lt;p&gt;Run the command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hashcat.exe -h&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to find about various attributes you can select in hashcat.&lt;/p&gt;

&lt;p&gt;You might see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F691cif9jouhjhqcmmhzz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F691cif9jouhjhqcmmhzz.png" alt="HashCat" width="426" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get a little understanding of what Brute Force is first.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Imagine to open the door you needed a key and you had 1000 keys with no information. You would have to go through every key possibly hitting the right one because you know it's one of these keys that unlock it. You will have to try every possible key.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Same as you would in say a 3 digit lock you can actually go through all the combinations &lt;code&gt;000---&amp;gt;999&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But if we were to increase the digits, possible include characters this would be very hard.&lt;/p&gt;

&lt;p&gt;So if we have some information even very little information such as the number of characters, what type, lowercase, uppercase digits etc can help bring down our possibilities from billion to millions!.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So I am going to take this hint here that I know the password will be of length 5 with 4 numerical digits and 1 special character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What we need to utilize hashcat here is use this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hashcat.exe -a 3 -m 10500 key.txt ?d?d?d?d?s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0it9aghd55shser2glcb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0it9aghd55shser2glcb.png" alt="HashCat0" width="800" height="114"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a breakdown:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a&lt;/code&gt; means: Attack Mode-&amp;gt; Brute force in this case.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;m&lt;/code&gt; means: Hash Type&lt;/li&gt;
&lt;li&gt; &lt;code&gt;key.txt&lt;/code&gt; is our file name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;?d?d?d?d?s&lt;/code&gt; means that the first four characters are to be of type digits and last is a special character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The result might be something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mn4uq4rzkdww2csb59m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mn4uq4rzkdww2csb59m.png" alt="HashCat2" width="800" height="609"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Incase you are trying it on some other pdf file the results might take time accordingly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Retrieving the password:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Now for the final step. Open the potfile using a notepad and you would see something like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is a Potfile?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The potfile is only created after at least one hash has been cracked successfully. The directory does exist but no potfile there. Cracked over a million of hashes already and hashcat removes already cracked hashes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyanhuz9pax7lq95m8jb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyanhuz9pax7lq95m8jb7.png" alt="Result" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;2020!&lt;/code&gt; is our password!.&lt;/p&gt;

&lt;p&gt;Yay!. We have successfully cracked a password.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;In the digital age, password security is of utmost importance, and sometimes, it becomes necessary to recover lost or forgotten passwords. In this article, we explored two powerful open-source tools, John the Ripper and Hashcat, which are invaluable resources for security professionals, penetration testers, and researchers.&lt;/p&gt;

&lt;p&gt;This is it for this article!. Thanks for reaching the end. Please feel free to drop comments below, I'll be sure to reply!.&lt;/p&gt;

</description>
      <category>security</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>DAY 108 - Find Duplicate</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Tue, 19 Sep 2023 18:26:51 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-108-find-duplicate-47je</link>
      <guid>https://dev.to/pahujanayan/day-108-find-duplicate-47je</guid>
      <description>&lt;p&gt;Hey  Guys! Welcome back to another 100DaysOf Code day.&lt;br&gt;
We are going to do another daily leetcode question today.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/find-the-duplicate-number/description/?envType=daily-question&amp;amp;envId=2023-09-19" rel="noopener noreferrer"&gt;Find Duplicate&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There is only one repeated number in nums, return this repeated number.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You must solve the problem without modifying the array nums and uses only constant extra space.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,3,4,2,2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [3,1,3,4,2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Understanding the Problem
&lt;/h2&gt;

&lt;p&gt;Before diving into the code, let's break down the problem and understand what's expected:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You are given an array &lt;code&gt;nums&lt;/code&gt; containing n + 1 integers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each integer in the array is between 1 and n (inclusive).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is only one integer that appears more than once in the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your task is to find and return this duplicate integer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Approach
&lt;/h2&gt;

&lt;p&gt;To efficiently solve this problem, we can use the Floyd's Tortoise and Hare algorithm, which is also known as the cycle detection algorithm. Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We initialize two pointers, &lt;code&gt;slow&lt;/code&gt; and &lt;code&gt;fast&lt;/code&gt;, both initially pointing to the first element of the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We use a loop to advance these pointers. The &lt;code&gt;slow&lt;/code&gt; pointer moves one step at a time, while the &lt;code&gt;fast&lt;/code&gt; pointer moves two steps at a time. This simulates two runners racing along the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If there is a duplicate number in the array, the pointers will eventually enter a cycle, meaning they will meet at the same position.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the pointers meet inside the cycle, we reset one of the pointers, let's say &lt;code&gt;fast&lt;/code&gt;, to the beginning of the array, while keeping the other pointer (&lt;code&gt;slow&lt;/code&gt;) inside the cycle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We then advance both pointers at the same rate (one step at a time). When they meet again, it will be at the entrance of the cycle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The position where the two pointers meet for the second time is the location of the duplicate number.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We return this duplicate number as our result.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;findDuplicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time and Space Complexity
&lt;/h2&gt;

&lt;p&gt;Let's analyze the time and space complexity of our solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: The Floyd's Tortoise and Hare algorithm runs in O(N) time, where N is the length of the array. This is because both pointers move through the array at different speeds, but the fast pointer covers the cycle at most twice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: We use only a constant amount of extra space to store the &lt;code&gt;slow&lt;/code&gt; and &lt;code&gt;fast&lt;/code&gt; pointers. Therefore, the space complexity is O(1).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>datastructures</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>DAY 107 - Daily Temperatures</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Mon, 18 Sep 2023 15:15:49 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-107-daily-temperatures-8h1</link>
      <guid>https://dev.to/pahujanayan/day-107-daily-temperatures-8h1</guid>
      <description>&lt;p&gt;Hey Guys!, Welcome to DAY 107 of #100DaysOfCode challenge.&lt;br&gt;
Let's get right into our question.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/daily-temperatures/description/" rel="noopener noreferrer"&gt;Daily Temperatures&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Given an array of integers &lt;code&gt;temperatures&lt;/code&gt; represents the daily temperatures, return an array &lt;code&gt;answer&lt;/code&gt; such that &lt;code&gt;answer[i]&lt;/code&gt; is the number of days you have to wait after the &lt;code&gt;ith&lt;/code&gt; day to get a warmer temperature. If there is no future day for which this is possible, keep &lt;code&gt;answer[i] == 0&lt;/code&gt; instead.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; temperatures = [73,74,75,71,69,72,76,73]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,1,4,2,1,1,0,0]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; temperatures = [30,40,50,60]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,1,1,0]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; temperatures = [30,60,90]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,1,0]&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Problem Breakdown:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We have been given an array &lt;code&gt;temperatures&lt;/code&gt; which contains temperatures on &lt;code&gt;n&lt;/code&gt; different days.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to find out for the &lt;code&gt;i'th&lt;/code&gt; day , how many days do we need to wait to experience a temperature greater than it. If we can't find a temperature warmer than it, then we must return 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So the question is pretty straightforward, let's see how we can solve this.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Intuition:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We can go through every temperature using a for loop and then we can use another loop  to find the temperature that is greater to our current temperature.&lt;/li&gt;
&lt;li&gt;We can use this approach but it is of the order (N&lt;sup&gt;2&lt;/sup&gt;), so we can probably do better than this.&lt;/li&gt;
&lt;li&gt;Let's look at it this way, at once we need to maintain how much data, we definitely need the prevDay and prevTemp for comparison and we need the current day as we are traversing through.&lt;/li&gt;
&lt;li&gt;Surely we have a data structure we can use to remember our previousTemperatures. It should be stack here as we can use extra memory.&lt;/li&gt;
&lt;li&gt;We will keep temperatures in our stack, and if we encounter a greater value, we shall subtract the days and push them into our answer.&lt;/li&gt;
&lt;li&gt;But for sure, we will be faced with the challenge in which our stack would be holding decreasing temperatures,&lt;/li&gt;
&lt;li&gt;For that, suppose we do encounter a temp greater than current top of the stack but not all the members in the stack we need to pop our stack until we have found the element less than equal to the current top.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Approach:
&lt;/h2&gt;

&lt;p&gt;To solve this problem efficiently, we can use a stack to keep track of the indices of the temperatures in the list. Here's the step-by-step approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We initialize an empty stack &lt;code&gt;st&lt;/code&gt; to store pairs of &lt;code&gt;(index, temperature)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We also initialize an empty vector &lt;code&gt;answer&lt;/code&gt; of the same size as the input list to store the number of days to wait for each day.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We iterate through the input list of temperatures, where &lt;code&gt;i&lt;/code&gt; represents the current day.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For each day, we check if the stack is not empty, and the temperature at the top of the stack is less than the current temperature. If this condition is met, it means we have found a warmer day for the day at the top of the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We pop elements from the stack and calculate the number of days to wait for each of them. For each popped element &lt;code&gt;(prevDay, prevTemp)&lt;/code&gt;, we calculate &lt;code&gt;i - prevDay&lt;/code&gt;, which represents the number of days to wait until a warmer temperature is encountered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We update the corresponding position in the &lt;code&gt;answer&lt;/code&gt; vector with the calculated number of days.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we push the current day and temperature onto the stack and continue to the next day.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After processing all the days, we return the &lt;code&gt;answer&lt;/code&gt; vector, which contains the number of days to wait for each day.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dailyTemperatures&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// pair: [index, temp]&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;currDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;currTemp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;currTemp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;prevDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;prevTemp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

                &lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;prevDay&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;currDay&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;prevDay&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;currDay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currTemp&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Time and Space Complexity
&lt;/h2&gt;

&lt;p&gt;Let's analyze the time and space complexity of our solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: We iterate through the input list once, performing constant-time operations for each day. Therefore, the time complexity is O(N), where N is the number of days.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: We use a stack to store at most N elements in the worst case (when temperatures are strictly increasing). Additionally, we use the &lt;code&gt;answer&lt;/code&gt; vector of size N. Thus, the space complexity is O(N).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>cpp</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>DAY 106 - Next Greater Element I</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Sun, 17 Sep 2023 16:26:54 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-106-next-greater-element-i-1n3p</link>
      <guid>https://dev.to/pahujanayan/day-106-next-greater-element-i-1n3p</guid>
      <description>&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/next-greater-element-i/description/" rel="noopener noreferrer"&gt;Next Greater Element I&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The next greater element of some element &lt;code&gt;x&lt;/code&gt; in an array is the first greater element that is to the right of &lt;code&gt;x&lt;/code&gt; in the same array.&lt;/p&gt;

&lt;p&gt;You are given two distinct 0-indexed integer arrays &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;, where &lt;code&gt;nums1&lt;/code&gt;is a subset of &lt;code&gt;nums2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For each &lt;code&gt;0 &amp;lt;= i &amp;lt; nums1.length&lt;/code&gt;, find the index &lt;code&gt;j&lt;/code&gt;such that &lt;code&gt;nums1[i] == nums2[j]&lt;/code&gt; and determine the next greater element of &lt;code&gt;nums2[j]&lt;/code&gt;in &lt;code&gt;nums2&lt;/code&gt;. If there is no next greater element, then the answer for this query is &lt;code&gt;-1.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Return an array &lt;code&gt;ans&lt;/code&gt; of length &lt;code&gt;nums1.length&lt;/code&gt; such that &lt;code&gt;ans[i]&lt;/code&gt; is the next greater element as described above.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [4,1,2], nums2 = [1,3,4,2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [-1,3,-1]&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: The next greater element for each value of nums1 is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.&lt;/li&gt;
&lt;li&gt;1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.&lt;/li&gt;
&lt;li&gt;2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Problem Breakdown:
&lt;/h2&gt;

&lt;p&gt;Before we dive into the code, let's break down the problem and understand what's expected:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We have two arrays, &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;, of non-negative integers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each element in &lt;code&gt;nums1&lt;/code&gt; is present in &lt;code&gt;nums2&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For each element in &lt;code&gt;nums1&lt;/code&gt;, we need to find the next greater element in &lt;code&gt;nums2&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If there is no greater element in &lt;code&gt;nums2&lt;/code&gt;, we should return &lt;code&gt;-1&lt;/code&gt; for that element in the result array.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Approach:
&lt;/h2&gt;

&lt;p&gt;To solve this problem efficiently, we can use a stack and a hash map. Here's the step-by-step approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We initialize an empty stack &lt;code&gt;s&lt;/code&gt; and an empty unorderd_map &lt;code&gt;mp&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We traverse through &lt;code&gt;nums2&lt;/code&gt;, which serves as our parent array to find the next greater elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In each iteration, we compare the current element, &lt;code&gt;it&lt;/code&gt;, with the top element of the stack (&lt;code&gt;s.top()&lt;/code&gt;). If &lt;code&gt;it&lt;/code&gt; is greater, we update the hash map &lt;code&gt;mp&lt;/code&gt; with the mapping of the top element of the stack to &lt;code&gt;it&lt;/code&gt;. We also pop the top element from the stack because we have found the next greater element for it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After processing &lt;code&gt;nums2&lt;/code&gt;, we have a complete mapping of each element to its next greater element in &lt;code&gt;mp&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We initialize the result vector &lt;code&gt;res&lt;/code&gt; with the same size as &lt;code&gt;nums1&lt;/code&gt;, filled with -1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We iterate through &lt;code&gt;nums1&lt;/code&gt; and check if the element exists in &lt;code&gt;mp&lt;/code&gt;. If it does, we update the corresponding position in &lt;code&gt;res&lt;/code&gt; with the next greater element found in &lt;code&gt;mp&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we return the &lt;code&gt;res&lt;/code&gt; vector, which contains the next greater elements for each element in &lt;code&gt;nums1&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nextGreaterElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;unordered_map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;nums2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]){&lt;/span&gt;
                &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time and Space Complexity:
&lt;/h2&gt;

&lt;p&gt;Let's analyze the time and space complexity of our solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: The solution processes both &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt; exactly once, so the time complexity is O(N1 + N2), where N1 is the length of &lt;code&gt;nums1&lt;/code&gt;, and N2 is the length of &lt;code&gt;nums2&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: We use a stack and a hash map to store intermediate results. The space complexity is O(N2) in the worst case, where N2 is the length of &lt;code&gt;nums2&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>learning</category>
      <category>cpp</category>
      <category>beginners</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Version Control Systems and Git Part-2</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Sat, 16 Sep 2023 17:46:36 +0000</pubDate>
      <link>https://dev.to/pahujanayan/version-control-systems-and-git-part-2-3f7b</link>
      <guid>https://dev.to/pahujanayan/version-control-systems-and-git-part-2-3f7b</guid>
      <description>&lt;p&gt;In the last post we talked in brief about how Git takes snapshots of the directories we are working in , how the commits we do are actually categorized into some types of data objects and they are not really stored in a linear fashion. We did capture the essence of what Git does but we were still left hanging with little to no information about commits, SHA1 encryption that git uses and git commands. So let's get right back onto track. Incase you didn't read the first post, I would recommend reading that first as it's a continuation.&lt;/p&gt;

&lt;h2&gt;
  
  
  .git folder
&lt;/h2&gt;

&lt;p&gt;Attempting to create a good distributed version control system within a mere week is an impossible task. However, what one can successfully design during the time is a fundamental data model. &lt;/p&gt;

&lt;p&gt;So, what precisely does Git's data model entail? To delve into this topic, we must delve into the enigmatic .git folder.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;./git&lt;/code&gt; folder , it contains a folder called objects which is essentially nothing but your object can be either a commit, a tree, a blob, or a tag. They’re compressed with zlib, but we can extract and examine the objects easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git Data Model as pseudo code
&lt;/h2&gt;

&lt;p&gt;Last time we mentioned the terms such as blob(Binary Large Objects), commits, snapshots, tree and more.&lt;br&gt;
Let's expand a little more on this data model internally of git.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// a binary large object is a file or say a bunch of bytes
type blob = array&amp;lt;byte&amp;gt;

// a folder/directory contains signed files and directories
type tree = map&amp;lt;string, tree | blob&amp;gt;

// a commit is an abstracted data object model with history to parents, author, the commit message, and the current snapshot basically the metadata of commit
type commit = struct {
    parents: array&amp;lt;commit&amp;gt;
    author: string
    message: string
    snapshot: tree
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is basically how git's data is modelled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git Objects and content-definitions
&lt;/h2&gt;

&lt;p&gt;At some point this actually has to be created into data on disks.&lt;/p&gt;

&lt;p&gt;Git defines objects which can be anything of the prescribed such as a blob, tree or commit&lt;/p&gt;

&lt;p&gt;&lt;code&gt;type object =&amp;gt; tree | commit | blob&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But what git actually maintains on disk is also a map to the hash of the object not just the object itself.&lt;/p&gt;

&lt;p&gt;In git all data objects are content-addressed by SHA1 hash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;objects = map&amp;lt;string,object&amp;gt;
def store(object):
    id = SHA1(object)
    objects[id] = object

def load(id):
    return objects[id]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you don't know what SHA1 or Hashing is, you can think of it as making the data's or say the object's content and do it's addressing into a finite string. This helps us in addressing large amount of data's into small strings.&lt;/p&gt;

&lt;p&gt;In practice commits don't actually contain the objects or snapshots for that matter but they have actually pointers to the obects location.&lt;/p&gt;

&lt;p&gt;Blobs, trees, and commits are unified in this way: they are all objects. When they reference other objects, they don’t actually contain them in their on-disk representation, but have a reference to them by their hash.&lt;/p&gt;

&lt;p&gt;In the realm of cryptography, SHA-1 (Secure Hash Algorithm 1) serves as a hash function. It operates by taking an input and generating a 160-bit (equivalent to 20 bytes) hash value, commonly represented as a 40-character string composed of hexadecimal digits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fun Fact:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Generating two identical SHA-1 hashes is an exceptionally rare occurrence, nearly approaching the realm of impossibility. To put this into perspective, imagine having to generate more hash codes than there are grains of sand on all the beaches across the entire Earth before the likelihood of encountering two identical hashes even becomes a consideration.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we further use the command &lt;code&gt;git cat-file -p &amp;lt;hashId&amp;gt;&lt;/code&gt; example something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git cat-file -p 4448adbf7ecd394f42ae135bbeed9676e894af85
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tree itself contains pointers to its contents, index.txt (a blob) and blogExample(a tree). If we look at the contents addressed by the hash corresponding to index.txt with &lt;code&gt;git cat-file -p &amp;lt;hashId&amp;gt;&lt;/code&gt;, we get the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This Side Nayan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;Now any normal human would not be able to remember 40characters long random sequences for having a pointer to its object but so then how do we call it.&lt;/p&gt;

&lt;p&gt;Well Git actually maintains another set of references which gives human name to those SHA1 generated strings to actually have addressable pointers for the end user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reference_map = map&amp;lt;string,string&amp;gt; (string1  is human name and string 2 is hash)


def store_reference(name, identifier):
    reference_map[name] = identifier

def retrieve_reference(name):
    return reference_map.get(name)

def load_reference(name_or_id):
    if name_or_id in reference_map:
        return load(reference_map[name_or_id])
    else:
        return load(name_or_id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike objects, which are immutable, references are mutable (can be updated to point to a new commit). For example, the master reference usually points to the latest commit in the main branch of development.&lt;/p&gt;

&lt;p&gt;With the help of these references git can actually maintain human-readble names like master main and so on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repositories
&lt;/h2&gt;

&lt;p&gt;In essence, a Git repository can be roughly defined as a collection of data objects and references.&lt;/p&gt;

&lt;p&gt;When we examine the repository on disk, we find that it comprises only two fundamental elements: objects and references. This straightforward concept encapsulates Git's entire data model. Essentially, all Git commands can be mapped to actions that manipulate the commit Directed Acyclic Graph (DAG) by either adding objects or updating references.&lt;/p&gt;

&lt;p&gt;The next time you enter a Git command, it's valuable to contemplate how that command influences the underlying graph data structure. Conversely, if you're aiming to achieve a specific alteration within the commit DAG – for instance, "discard uncommitted changes and set the 'master' reference to point to commit 4d123f6w" – you'll likely discover a command tailored to that purpose. In this particular case, the commands would be: git&lt;code&gt;checkout master&lt;/code&gt; followed by &lt;code&gt;git reset --hard 4d123f6w&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Staging Area
&lt;/h2&gt;

&lt;p&gt;The concept of staging area is quite brilliant when you understand it although it is separate from Git's data model but is an integral part of how commits are created.&lt;/p&gt;

&lt;p&gt;Instead of having a single "create snapshot" command that captures the current state of everything and take the data again and repeat the whole story Git takes a different approach. It provides a way to choose precisely what changes we want to include in the next snapshot. This selection process is done through something called the "staging area."&lt;/p&gt;

&lt;p&gt;The staging area lets us pick and choose which modifications should go into the next snapshot. This flexibility is useful when we have multiple changes in progress or when you need to create a commit that includes some changes while excluding others.&lt;/p&gt;




&lt;p&gt;Since this post has already gone I won't be covering the git commands in depth. I will be linking down the best resources to read more about git and how git commands work.&lt;/p&gt;

&lt;p&gt;Now that we have gotten a little understanding how git works, the commands should not feel like magical incantations but some very simple CLI commands.&lt;/p&gt;

&lt;p&gt;In practice git uses delta compression and many more stuff to achieve what it is today. This is just an outline to the fancy technology used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;In conclusion, through this two part series I have tried to provide some valuable insights into Git's fundamental data model, the role of the .git folder, and how Git objects are organized using SHA-1 hashes. We've learned about the significance of references in providing human-friendly names to commits and explored the concept of the staging area for precise control over what goes into each snapshot. Understanding these core aspects of Git's inner workings is crucial for efficient version control. To further enhance your Git proficiency, you can refer to the recommended resources mentioned in the post.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/book/en/v2" rel="noopener noreferrer"&gt;Pro Git&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;I highly recommend reading first few chapters of the books and then looking into some advanced stuff.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub:&lt;/strong&gt; Git is not GitHub. GitHub has a specific way of contributing code to other projects, called pull requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other Git providers:&lt;/strong&gt; GitHub is not special: there are many Git repository hosts, like GitLab and BitBucket.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagain.net/articles/git-for-computer-scientists/" rel="noopener noreferrer"&gt;Git For Computer Scientists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://missing.csail.mit.edu/2020/version-control/" rel="noopener noreferrer"&gt;Missing Lectures VCS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nfarina.com/post/9868516270/git-is-simpler" rel="noopener noreferrer"&gt;Git is Simpler than you think&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Sources:
&lt;/h2&gt;

&lt;p&gt;This series was heavily inspired by the Missing Semester Lecture on Youtube which I will leave the link down below and a few very good articles. I have tried to incorporate the parts I think were important and best from each.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://missing.csail.mit.edu/2020/version-control/" rel="noopener noreferrer"&gt;Missing Lectures VCS Notes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=2sjqTHE0zok" rel="noopener noreferrer"&gt;Missing Lectures VCS Video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nfarina.com/post/9868516270/git-is-simpler" rel="noopener noreferrer"&gt;Git is Simpler than you think&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>codenewbie</category>
      <category>discuss</category>
      <category>learning</category>
    </item>
    <item>
      <title>DAY 105 - 155. Min Stack</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Sat, 16 Sep 2023 13:48:52 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-105-155-min-stack-5a74</link>
      <guid>https://dev.to/pahujanayan/day-105-155-min-stack-5a74</guid>
      <description>&lt;p&gt;Hey Guys! Welcome back to another day. Today we will be moving onto stacks from our linked lists and we are back with a very good question.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/min-stack/description/" rel="noopener noreferrer"&gt;155. Min Stack&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Design a stack that supports &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pop&lt;/code&gt;, &lt;code&gt;top&lt;/code&gt;, and retrieving the &lt;code&gt;minimum&lt;/code&gt; element in constant time.&lt;/p&gt;

&lt;p&gt;Implement the &lt;code&gt;MinStack&lt;/code&gt; class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;MinStack()&lt;/code&gt; initializes the stack object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;void push(int val)&lt;/code&gt; pushes the element &lt;code&gt;val&lt;/code&gt; onto the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;void pop()&lt;/code&gt; removes the element on the &lt;code&gt;top&lt;/code&gt; of the stack.&lt;/li&gt;
&lt;li&gt;int &lt;code&gt;top()&lt;/code&gt; gets the top element of the stack.&lt;/li&gt;
&lt;li&gt;int &lt;code&gt;getMin()&lt;/code&gt; retrieves the minimum element in the stack.&lt;/li&gt;
&lt;li&gt;You must implement a solution with &lt;code&gt;O(1)&lt;/code&gt; time complexity for each function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt;&lt;br&gt;
["MinStack","push","push","push","getMin","pop","top","getMin"]&lt;br&gt;
[[],[-2],[0],[-3],[],[],[],[]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
[null,null,null,null,-3,null,0,-2]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
MinStack minStack = new MinStack();&lt;br&gt;
minStack.push(-2);&lt;br&gt;
minStack.push(0);&lt;br&gt;
minStack.push(-3);&lt;br&gt;
minStack.getMin(); // return -3&lt;br&gt;
minStack.pop();&lt;br&gt;
minStack.top();    // return 0&lt;br&gt;
minStack.getMin(); // return -2&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Understanding the MinStack:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;push(val)&lt;/strong&gt;: This operation adds an element, &lt;code&gt;val&lt;/code&gt;, to the stack. It also needs to keep track of the minimum element in the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;pop()&lt;/strong&gt;: This operation removes the top element from the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;top()&lt;/strong&gt;: This operation returns the top element of the stack without removing it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;getMin()&lt;/strong&gt;: This operation returns the minimum element currently present in the stack in constant time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Approach:
&lt;/h2&gt;

&lt;p&gt;To implement the MinStack, we can use two separate stacks: one to store the actual elements (&lt;code&gt;st&lt;/code&gt;) and another to keep track of the indices of minimum elements (&lt;code&gt;miniVals&lt;/code&gt;). Here's how our approach works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We maintain two vectors, &lt;code&gt;st&lt;/code&gt; and &lt;code&gt;miniVals&lt;/code&gt;, to represent the stack and store the indices of minimum elements, respectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When pushing a new element onto the stack (&lt;code&gt;push(val)&lt;/code&gt;), we compare it with the current minimum element. If it's smaller, we push the index of the new element onto &lt;code&gt;miniVals&lt;/code&gt;. This way, we always have a reference to the index of the minimum element in the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When popping an element from the stack (&lt;code&gt;pop()&lt;/code&gt;), we simply remove the top element from &lt;code&gt;st&lt;/code&gt;. If the index of the minimum element (&lt;code&gt;miniVals.back()&lt;/code&gt;) matches the index of the element being removed, we also remove it from &lt;code&gt;miniVals&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retrieving the top element (&lt;code&gt;top()&lt;/code&gt;) is straightforward; we return the last element of &lt;code&gt;st&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To get the minimum element (&lt;code&gt;getMin()&lt;/code&gt;), we use the index stored in &lt;code&gt;miniVals&lt;/code&gt; to access the corresponding element in &lt;code&gt;st&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MinStack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;miniVals&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;MinStack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;miniVals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;miniVals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="p"&gt;()]){&lt;/span&gt;
            &lt;span class="n"&gt;miniVals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop_back&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;miniVals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="n"&gt;miniVals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop_back&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;top&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getMin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;miniVals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="p"&gt;()];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj-&amp;gt;push(val);
 * obj-&amp;gt;pop();
 * int param_3 = obj-&amp;gt;top();
 * int param_4 = obj-&amp;gt;getMin();
 */&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time and Space Complexity
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: Each of the operations (&lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pop&lt;/code&gt;, &lt;code&gt;top&lt;/code&gt;, and &lt;code&gt;getMin&lt;/code&gt;) takes constant time, O(1), as we are only performing basic vector operations, which have constant time complexity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: The space complexity is O(N), where N is the number of elements stored in the stack. This space is used to store the actual elements in &lt;code&gt;st&lt;/code&gt; and the indices of minimum elements in &lt;code&gt;miniVals&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Version Control Systems - Part 1</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Fri, 15 Sep 2023 18:46:32 +0000</pubDate>
      <link>https://dev.to/pahujanayan/version-control-systems-4lm</link>
      <guid>https://dev.to/pahujanayan/version-control-systems-4lm</guid>
      <description>&lt;p&gt;Millions of developers today use Git or Git Hub. It has become a core part of any kind of programmer's day after their work has been done. If you were applying for a development position today, it would be a prerequisite that you understand git and Github or some kind of version control system.&lt;/p&gt;

&lt;p&gt;I remember 2 years back when I first started basic development, coding, and stuff in my first year of college that I interacted with this. VCS is something developers can't think of working without today and yet no college university explicitly covers VCS.&lt;/p&gt;

&lt;p&gt;I will try to make this into a two-part series on Version Control Systems and subsequently the de facto standard for VCS today git.&lt;br&gt;
In this post I will be trying to cover how it works and what exactly happens to your data, so Let's get started.&lt;/p&gt;
&lt;h2&gt;
  
  
  Version Control Systems:
&lt;/h2&gt;
&lt;h3&gt;
  
  
  What are Version Control Systems?
&lt;/h3&gt;

&lt;p&gt;We have been using VCS for a long time now. To put it to a definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Version Control Systems are tools that are used to track changes to source(original) code changes or the collections of files and folders around it.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As the name implies Version Control Systems helps us maintain a history of changes also facilitating collaboration at the same time.&lt;/p&gt;

&lt;p&gt;Gone is the time we would need to manually change our existing codebase line by line when working with some other member. VCS does all of that and more for us today.&lt;/p&gt;

&lt;p&gt;VCSs track changes to a folder and its contents in a series of snapshots, where each snapshot encapsulates the entire state of files/folders within a top-level directory. VCSs also maintain metadata like who created each snapshot, messages associated with each snapshot, and so on.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why Version Control Systems?
&lt;/h3&gt;

&lt;p&gt;Well, we have all played games and never played them in one sitting(well sometimes), So what do we do? We save our game to our last checkpoint log in the next day and continue from the same spot or if we make a mistake we could start from our last checkpoint and start again. Why could we not do that with our code? Well, we have already been doing it, that's exactly what VCSs are doing, We can easily track what we have been doing wrong and where we went off right or try new things without breaking the existing working code.&lt;/p&gt;
&lt;h3&gt;
  
  
  Types of Version Control Systems and their History:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Local Version Control Systems:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;History:&lt;/strong&gt; Local VCSs were some of the earliest form of version control. They operated on a single computer and kept track of changes made to files using a simple database or file system.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Centralized Version Control Systems (CVCS):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Examples:&lt;/strong&gt; Concurrent Versions System (CVS), Subversion (SVN).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;History:&lt;/strong&gt; CVCSs introduced the concept of a central server that stored the repository. Developers could check out code from this central location and commit their changes back to it. While it improved collaboration compared to local VCSs, it had limitations in terms of scalability and offline access and could also lead to some conflicts more easily.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Distributed Version Control Systems (DVCS):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Examples:&lt;/strong&gt; Git, Mercurial, Bazaar.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;History:&lt;/strong&gt; DVCSs represent a significant evolution in version control. Instead of relying on a central server, each developer has their own local repository, which is a complete copy of the entire project history. Developers can work offline, branch easily, and commit locally before pushing changes to a shared central repository.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Features of Modern VCSs
&lt;/h3&gt;

&lt;p&gt;It’s an invaluable tool for seeing what other people have changed, as well as resolving conflicts in concurrent development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who authored this module?&lt;/li&gt;
&lt;li&gt;When was this specific line in the particular file last modified, and who made the changes? What was the reason behind the modification?&lt;/li&gt;
&lt;li&gt;Within the past 100 revisions, when and for what reason did a specific unit test cease to function properly?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While there exist many VCSs git is the de facto standard for today's version control systems.&lt;/p&gt;

&lt;p&gt;This fun comic by &lt;a href="https://xkcd.com/1597/" rel="noopener noreferrer"&gt;xkcd&lt;/a&gt; completely debunks half of the developer community when they start out and I certainly have done this as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvc362rrsnc85montqkwo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvc362rrsnc85montqkwo.png" alt="Git Comic" width="330" height="478"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Git
&lt;/h2&gt;

&lt;p&gt;Let's use git to understand a little bit how modern day DVCS work and let's explore how git works internally a little bit.&lt;/p&gt;

&lt;p&gt;When I began learning Git, I found that starting with the command-line interface was quite confusing. Instead of grasping the underlying concepts, I ended up memorizing a handful of commands and relied on them like magic spells whenever I ran into problems.&lt;br&gt;
I am very confident I am not alone who has done this.&lt;/p&gt;

&lt;p&gt;Git may not have the prettiest interface, but its core design and ideas are elegant. Instead of just memorizing commands, it's helpful to start from the basics, understanding Git's data model first, and then getting into the command-line part. Once you grasp the data model, using Git commands becomes more logical instead of feeling like chanting magical incantations and it works somehow.&lt;/p&gt;
&lt;h3&gt;
  
  
  Git Data Model:
&lt;/h3&gt;

&lt;p&gt;While there are numerous ad-hoc approaches to version control, Git stands out with its carefully designed model that underpins essential version control features such as history maintenance, branch support, and collaborative capabilities.&lt;/p&gt;
&lt;h3&gt;
  
  
  Snapshots:
&lt;/h3&gt;

&lt;p&gt;Imagine you are doing work in some parts but the catch is you can't exactly remember where you left the work last.&lt;br&gt;
What you can do is take a picture of where you working last time and next time you come back to it you can refer to that picture and start seeing changes from there.&lt;/p&gt;

&lt;p&gt;Every time you commit git takes a snapshot(not exactly a picture the example was just for understanding)&lt;br&gt;
Git also views what was the state of your code in the last snapshot which exists and shows the changes currently made by simply their differences.&lt;/p&gt;

&lt;p&gt;In Git terminology, a file is called a “blob” (Binary Large Object), and it’s just a bunch of bytes. A directory is called a “tree”, and it maps names to blobs or trees (so directories can contain other directories). A snapshot is the top-level tree that is being tracked.&lt;/p&gt;

&lt;p&gt;Let's make some files and folders to understand this in a better way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;blogExample
~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;blogExample
~/blogExample&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hey This Side Nayan"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; index.txt
~/blogExample&lt;span class="nv"&gt;$ &lt;/span&gt;git init
~/blogExample&lt;span class="nv"&gt;$ &lt;/span&gt;git add index.txt
~/blogExample&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"First commit, added introduction."&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, we might have a tree as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;root&amp;gt; (tree)
|
+- blogExample(tree)
|  |
|  + index.txt (blob, contents = "This Side Nayan")
|

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Version History:
&lt;/h3&gt;

&lt;p&gt;How should a VCS relate version history, which is one of the more crucial parts of a VCS. We can always take a linear history take something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgk9tug1gc4sy6wnx1alu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgk9tug1gc4sy6wnx1alu.png" alt="Linear Commit History" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But git doesn't exactly work in this way, It uses a common Data Structure known as Directed Acyclic Graph to store the snapshots.&lt;/p&gt;

&lt;p&gt;What this essentially means in Git is that each snapshot I create has a reference to a set of "parents," which are the snapshots that came before it. It's a set of parents, not just a single one (as you would have in a linear history), because a snapshot can be connected to multiple parents. This can happen, for instance, when I merge two parallel branches of development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8zs56glwi8xi3avj3d1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8zs56glwi8xi3avj3d1.png" alt="DAG" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though I have used the word commit in the images, what those circle essentially represent are Snapshots taken in place by git.&lt;/p&gt;

&lt;p&gt;This might correspond to, for example, two separate features being developed in parallel, independently from each other. In the future, these branches may be merged to create a new snapshot that incorporates both of the features,&lt;/p&gt;

&lt;p&gt;This is essentially what branching is.&lt;/p&gt;

&lt;p&gt;Commits in Git are immutable. This doesn’t mean that mistakes can’t be corrected, however; it’s just that “edits” to the commit history are actually creating entirely new commits, and references  are updated to point to the new ones.&lt;/p&gt;

&lt;h3&gt;
  
  
  Github Origin:
&lt;/h3&gt;

&lt;p&gt;You can create a Git repository on your own computer and manage it locally, taking advantage of Git's features such as branches and commits. However, if you intend to collaborate with others on the project, this approach won't suffice. An alternative is to copy the entire project to another computer and regularly export changes, sending them back and forth for updates. Nevertheless, this method is far from ideal. What you'll end up with is a centralized location where project data is stored, and it becomes the hub for downloading and uploading changes and commits. This central location is referred to as "origin." You have the option to host it on your personal server, but there are also many free solutions available, such as GitHub, that you can use for this purpose.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion Part 1:
&lt;/h2&gt;

&lt;p&gt;While there are numerous features and functionalities in VCSs(git and more), such as committing, pushing changes, merging, and more, the main takeaway from this post should be a fundamental understanding of what Git is. For those encountering version control systems for the first time, the initial confusion can be overwhelming. However, I hope this post has provided you with a clear idea of how VCSs operate and why it is an invaluable tool in the world of software development.&lt;/p&gt;

&lt;p&gt;I will be covering more of Object Data Model of git, how it uses SHA1 hashing to store the data and how commits are actually not objects but are pointers to snapshots and such more things including git commands.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>DAY 104 - Merge k Sorted Lists</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Fri, 15 Sep 2023 09:07:57 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-104-merge-k-sorted-lists-4dfl</link>
      <guid>https://dev.to/pahujanayan/day-104-merge-k-sorted-lists-4dfl</guid>
      <description>&lt;p&gt;Hey Guys!, Welcome to DAY 104 and we are continuing on our journey to master linked list. Today we are back with a hard interesting question. So Let's get right down to it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/merge-k-sorted-lists//" rel="noopener noreferrer"&gt;Merge k Sorted Lists&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You are given an array of &lt;code&gt;k&lt;/code&gt; linked-lists &lt;code&gt;lists&lt;/code&gt;, each linked-list is sorted in ascending order.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Merge all the linked-lists into one sorted linked-list and return it.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; lists = [[1,4,5],[1,3,4],[2,6]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,1,2,3,4,4,5,6]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The linked-lists are:&lt;br&gt;
[&lt;br&gt;
  1-&amp;gt;4-&amp;gt;5,&lt;br&gt;
  1-&amp;gt;3-&amp;gt;4,&lt;br&gt;
  2-&amp;gt;6&lt;br&gt;
]&lt;br&gt;
merging them into one sorted list:&lt;br&gt;
1-&amp;gt;1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;4-&amp;gt;5-&amp;gt;6&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; lists = []&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; []&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Question Breakdown:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Ok so despite it being tagged as the hard problem is quite easy to understand.&lt;/li&gt;
&lt;li&gt;The description is asking us to make an merge all the given linked lists in a vector which are already sorted in order.&lt;/li&gt;
&lt;li&gt;So what we have to do is basically merge all the lists but If the size is too big, we can't be declaring pointers for every list right.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Approach
&lt;/h2&gt;

&lt;p&gt;To solve this problem efficiently, we can use a divide-and-conquer strategy. Here's a step-by-step explanation of our approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Handle Edge Cases&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the input array of linked lists is empty, we return &lt;code&gt;nullptr&lt;/code&gt;, indicating an empty list.&lt;/li&gt;
&lt;li&gt;If there's only one linked list in the array, we return it as the result.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Pairwise Merge&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We start by dividing the array of linked lists into pairs and merging each pair into a single list.&lt;/li&gt;
&lt;li&gt;For example, if we have k linked lists, we pair the first list with the (k/2 + 1)-th list, the second list with the (k/2 + 2)-th list, and so on.&lt;/li&gt;
&lt;li&gt;We do this merging iteratively until we have only one merged list remaining.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Merging Two Lists&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To merge two sorted linked lists, we use a helper function called &lt;code&gt;mergeTwoLists&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This function takes two pointers, &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt;, each pointing to the head of a sorted list.&lt;/li&gt;
&lt;li&gt;We create a new dummy node, &lt;code&gt;ans&lt;/code&gt;, to build the merged list.&lt;/li&gt;
&lt;li&gt;While both &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt; are not null, we compare the values at their current positions.&lt;/li&gt;
&lt;li&gt;We append the node with the smaller value to the merged list and advance the respective pointer.&lt;/li&gt;
&lt;li&gt;We continue this process until one of the lists becomes null.&lt;/li&gt;
&lt;li&gt;Finally, we attach the remaining nodes (if any) from the non-null list to the merged list.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Return the Result&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After pairwise merging, we're left with one merged list in the array.&lt;/li&gt;
&lt;li&gt;We return the head of this merged list as our final result.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;mergeTwoLists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;mergeKLists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mergeTwoLists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Let's analyze the time and space complexity of our solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: The time complexity depends on the number of nodes in all the linked lists. During each pairwise merge, we traverse all the nodes exactly once. Since we perform log₂(k) iterations (where k is the number of linked lists), the total time complexity is O(N⋅log(k)), where N is the total number of nodes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: The space complexity is O(1) for variables used in the algorithm, and we don't use any additional data structures apart from the input and output. Therefore, the space complexity is O(1).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>cpp</category>
      <category>discuss</category>
    </item>
    <item>
      <title>DAY 103 - Copy List with Random Pointer</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Thu, 14 Sep 2023 12:18:52 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-103-copy-list-with-random-pointer-48c1</link>
      <guid>https://dev.to/pahujanayan/day-103-copy-list-with-random-pointer-48c1</guid>
      <description>&lt;p&gt;Hey Guys!, Welcome to DAY 103 and we are continuing on our journey to master linked list. Today we are back with an interesting question. So Let's get right down to it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/copy-list-with-random-pointer/description/" rel="noopener noreferrer"&gt;Copy List with Random Pointer&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;A linked list of length &lt;code&gt;n&lt;/code&gt; is given such that each node contains an additional &lt;code&gt;random&lt;/code&gt; pointer, which could point to any node in the list, or &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Construct a &lt;code&gt;deep copy&lt;/code&gt; of the list. The &lt;code&gt;deep copy&lt;/code&gt; should consist of exactly &lt;code&gt;n&lt;/code&gt; brand new nodes, where each new &lt;code&gt;node&lt;/code&gt; has its value set to the value of its corresponding original &lt;code&gt;node&lt;/code&gt;. Both the &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;random&lt;/code&gt; pointer of the new nodes should point to new nodes in the copied &lt;code&gt;list&lt;/code&gt; such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.&lt;/p&gt;

&lt;p&gt;For example, if there are two nodes &lt;code&gt;X and Y&lt;/code&gt; in the original list, where &lt;code&gt;X.random --&amp;gt; Y&lt;/code&gt;, then for the corresponding two nodes x and y in the copied list, &lt;code&gt;x.random --&amp;gt; y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return the &lt;code&gt;head&lt;/code&gt; of the copied linked list.&lt;/p&gt;

&lt;p&gt;The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [&lt;code&gt;val&lt;/code&gt;, &lt;code&gt;random_index&lt;/code&gt;] where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;val&lt;/code&gt;: an integer representing &lt;code&gt;Node.val&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;random_index&lt;/code&gt;: the index of the node (range from 0 to n-1) that the &lt;code&gt;random&lt;/code&gt; pointer points to, or null if it does not point to any node.
Your code will only be given the &lt;code&gt;head&lt;/code&gt; of the original linked list.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rdhz2wu1qwazqszmdxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rdhz2wu1qwazqszmdxb.png" alt="Example" width="800" height="162"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [[7,null],[13,0],[11,4],[10,2],[1,0]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[7,null],[13,0],[11,4],[10,2],[1,0]]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqqut7n6qbrz7rsrcxkq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqqut7n6qbrz7rsrcxkq2.png" alt="Example" width="800" height="139"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [[3,null],[3,0],[3,null]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[3,null],[3,0],[3,null]]&lt;/p&gt;




&lt;h2&gt;
  
  
  Question Breakdown:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Ok so despite the lengthy the problem is quite easy to understand.&lt;/li&gt;
&lt;li&gt;The description is asking us to make an exact to exact copy of the given linked list.&lt;/li&gt;
&lt;li&gt;So what's the catch? The catch is that we have to create a copy of new node and allocate it in a new memory(space), we can't use the old linked list or have any of our nodes point back to it's nodes.&lt;/li&gt;
&lt;li&gt;To add to our difficulty, they have given us a random pointer which points to any node in the linked list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Intuition:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Ok so this seems easy, we can just do a loop and make new Nodes and pointers for every and move ahead?.&lt;/li&gt;
&lt;li&gt;Yeah but we also need to deal with random pointers pointing at nodes that have not existed yet or been created yet.&lt;/li&gt;
&lt;li&gt;Look in example 1:, if we make a copy of it line by line our 2nd node points to last node, but if we haven't reached last node how will create a pointer to it.&lt;/li&gt;
&lt;li&gt;Well How about we forget pointing at all and first make all the nodes. If we don't assign pointer to it? how will we move, well we can use a map to link old list and new list.&lt;/li&gt;
&lt;li&gt;Using a map we can link them and after we have created nodes , now any of our node's next or random pointer's will point to a node that actually exists in memory.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Approach:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;First Pass: Creating New Nodes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We start by iterating through the original list using a temporary pointer, &lt;code&gt;temp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;While traversing, we create a new node for each node in the original list and assign its &lt;code&gt;val&lt;/code&gt; with the same value as the original node.&lt;/li&gt;
&lt;li&gt;We also store a mapping between the original node and the new node in an unordered map (&lt;code&gt;nodes&lt;/code&gt;) for the creation of &lt;code&gt;random&lt;/code&gt; pointers later.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Second Pass: Setting &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;random&lt;/code&gt; Pointers&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We reset &lt;code&gt;temp&lt;/code&gt; to the head of the original list.&lt;/li&gt;
&lt;li&gt;While traversing the original list again, we set the &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;random&lt;/code&gt; pointers for each new node.&lt;/li&gt;
&lt;li&gt;For the &lt;code&gt;next&lt;/code&gt; pointer, we look up the mapping in the &lt;code&gt;nodes&lt;/code&gt; map to find the corresponding new node.&lt;/li&gt;
&lt;li&gt;For the &lt;code&gt;random&lt;/code&gt; pointer, we do the same: look up the mapping to find the corresponding new node.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Returning the Result&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finally, we return the head of the new list, which is stored in &lt;code&gt;nodes[head]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="cm"&gt;/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;

    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;copyRandomList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;unordered_map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//use to store link to old nodes and new nodes&lt;/span&gt;

        &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//temporary pointer to fill nodes&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//make copy of node and attach to nodes&lt;/span&gt;
            &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//reinitialize the pointer so that we can traverse again&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time and Space Complexity
&lt;/h2&gt;

&lt;p&gt;Let's analyze the time and space complexity of this solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: This solution makes two passes through the entire linked list. Therefore, the time complexity is O(N), where N is the number of nodes in the linked list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: The space complexity is O(N) as well. We use additional space to store the mapping between original nodes and new nodes in the &lt;code&gt;nodes&lt;/code&gt; hash map. The space required for the new linked list is also O(N) as we create a new node for each node in the original list.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>discuss</category>
      <category>cpp</category>
    </item>
    <item>
      <title>DAY 102 - 2130. Maximum Twin Sum of a Linked List</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Wed, 13 Sep 2023 12:22:36 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-102-2130-maximum-twin-sum-of-a-linked-list-h5k</link>
      <guid>https://dev.to/pahujanayan/day-102-2130-maximum-twin-sum-of-a-linked-list-h5k</guid>
      <description>&lt;p&gt;&lt;em&gt;Hello People!. Continuing our challenge until we break it , we continue on our path to learn linked list and solve it's leetcode problems. Today we are going to do a very easy problem and the intuition behind this can be sometimes hard to catch, so let's start.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/description/" rel="noopener noreferrer"&gt;2130. Maximum Twin Sum of a Linked List&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In a linked list of size &lt;code&gt;n&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is even, the &lt;code&gt;ith&lt;/code&gt; node &lt;code&gt;(0-indexed)&lt;/code&gt; of the linked list is known as the twin of the &lt;code&gt;(n-1-i)th&lt;/code&gt; node, if &lt;code&gt;0 &amp;lt;= i &amp;lt;= (n / 2) - 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, if &lt;code&gt;n&lt;/code&gt; = 4, then node &lt;code&gt;0&lt;/code&gt; is the twin of node &lt;code&gt;3&lt;/code&gt;, and node &lt;code&gt;1&lt;/code&gt; is the twin of node &lt;code&gt;2&lt;/code&gt;. These are the only nodes with twins for &lt;code&gt;n = 4&lt;/code&gt;.&lt;br&gt;
The twin sum is defined as the sum of a node and its twin.&lt;/p&gt;

&lt;p&gt;Given the &lt;code&gt;head&lt;/code&gt; of a linked list with even length, return the maximum twin sum of the linked list.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37s2fwid17gufp4odg7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37s2fwid17gufp4odg7z.png" alt="Example" width="433" height="122"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [5,4,2,1]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 6&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.&lt;br&gt;
There are no other nodes with twins in the linked list.&lt;br&gt;
Thus, the maximum twin sum of the linked list is 6. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Example 2:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqb9uaack8i4ezqodu5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqb9uaack8i4ezqodu5w.png" alt="Example" width="433" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; head = [4,2,2,3]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 7&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
The nodes with twins present in this linked list are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.&lt;/li&gt;
&lt;li&gt;Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
Thus, the maximum twin sum of the linked list is max(7, 4) = 7.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Problem Breakdown:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We are given a singly linked list of even length each having value from 0 to 10^5.&lt;/li&gt;
&lt;li&gt;We are also given a pair relation, to simplify the relation it's the first node from the start and last node. They make a pair and we move ahead from start to next node and we move back from last node to our second last node, sequentially every such two nodes form a pair.&lt;/li&gt;
&lt;li&gt;Okay so, if anyone has done a little bit of coding questions, this is pretty easy, We can simply take a vector to store the values and use a while loop to traverse from both ends using two pointers and find the maximum.&lt;/li&gt;
&lt;li&gt;Yes but in this approach we will be using O(N) extra space, the challenge lies in solving this without using extra space.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So how do we do it? Let's head down to approach.&lt;/p&gt;




&lt;h2&gt;
  
  
  Intuition:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;So the challenge is we can only move forward in our linked list.&lt;/li&gt;
&lt;li&gt;If we could move back we could solve this right, obviously we can't make this into a doubly linked list so how do we move back.&lt;/li&gt;
&lt;li&gt;Let's first identify which nodes do we need, L0 &amp;amp; LN , L1 &amp;amp; LN-1 , L2 &amp;amp; LN-2 and so on...&lt;/li&gt;
&lt;li&gt;If we carefully see our mid pointer will have it's next node as it's pair.&lt;/li&gt;
&lt;li&gt;If we can somehow travel in reverse after our mid, we will be able to get the solution.&lt;/li&gt;
&lt;li&gt;Let's try doing this exactly, we divide our linked list into two parts, one is our left part and one is our right part.&lt;/li&gt;
&lt;li&gt;When we reverse a linked list the last node is our head.&lt;/li&gt;
&lt;li&gt;After we reverse it, we now have a pointer to last node and we can move forward.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Continuing on our intuition let's write the approach:&lt;/p&gt;




&lt;h2&gt;
  
  
  Approach:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reverse the Right Half of the List&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;First, we identify the middle of the linked list using Floyd's Tortoise and Hare algorithm. We do this by having two pointers, &lt;code&gt;slow&lt;/code&gt; and &lt;code&gt;fast&lt;/code&gt;, traverse the list at different speeds. When they meet, &lt;code&gt;slow&lt;/code&gt; points to the middle node.&lt;/li&gt;
&lt;li&gt;Next, the right half of the linked list is reversed. Reversing the list makes it easier to calculate the maximum pair sum.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;In case you don't know this approach, check out my article on it : &lt;a href="https://dev.to/pahujanayan/day-93-floyd-cycle-detection-algorithm-finding-a-cycle-in-a-linked-list-1oic"&gt;Floyd's tortoise and hare algorithm&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Find the Maximum Pair Sum&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With the right half of the list reversed, we initialize two pointers, &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt;, at the heads of the original and reversed lists, respectively.&lt;/li&gt;
&lt;li&gt;As the pointers traverse the lists, the maximum sum of pairs is calculated. We do this by by summing the values of nodes pointed to by &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt; and updating the maximum sum (&lt;code&gt;maxi&lt;/code&gt;) if we encounter larger sum.&lt;/li&gt;
&lt;li&gt;We continue the process until both pointers reach the end of their respective lists.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Return the Maximum Sum&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The maximum pair sum (&lt;code&gt;maxi&lt;/code&gt;) is the result of the problem and hence we return it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="cm"&gt;/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;

    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;reverseList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;nex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;nex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nex&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;pairSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;//find the middle node of the list using floyd's tortoise and hare&lt;/span&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//slow points to middle of the list&lt;/span&gt;
    &lt;span class="c1"&gt;//cut the list from middle&lt;/span&gt;


    &lt;span class="c1"&gt;//reverse the right list&lt;/span&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reverseList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;maxi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxi&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time and Space Complexity
&lt;/h2&gt;

&lt;p&gt;Now, let's analyze the time and space complexity of this solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: We traverse the entire linked list twice. The first traversal finds the middle of the list, and the second traversal calculates the maximum pair sum. Therefore, the time complexity is O(N), where N is the number of nodes in the linked list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: We use a constant amount of extra space for variables. No additional data structures or memory allocation is required, resulting in a space complexity of O(1).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>100daysofcode</category>
      <category>leetcode</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>DAY 101 - 61. Rotate List</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Tue, 12 Sep 2023 05:42:26 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-101-61-rotate-list-5b9j</link>
      <guid>https://dev.to/pahujanayan/day-101-61-rotate-list-5b9j</guid>
      <description>&lt;p&gt;Hey Everyone!, a bit change of plans, I will be continuing this series until I accidentally break it.&lt;/p&gt;

&lt;p&gt;Today we are going to do another linked list question, which is quite encountered in arrays as well is rotate the linked list.&lt;/p&gt;




&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/rotate-list/description/" rel="noopener noreferrer"&gt;Rotate List&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Given the &lt;code&gt;head&lt;/code&gt; of a linked list, rotate the list to the right by &lt;code&gt;k&lt;/code&gt; places.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F85bnkjs62ev5tczpp3jy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F85bnkjs62ev5tczpp3jy.png" alt="Ex" width="712" height="302"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [1,2,3,4,5], k = 2&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [4,5,1,2,3]&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Understanding the Problem
&lt;/h2&gt;

&lt;p&gt;Before we dive into the code, let's break down the problem and understand its requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We're given a linked list &lt;code&gt;head&lt;/code&gt; where each node contains a value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to perform operations in the such that list should be rotated to the right by &lt;code&gt;k&lt;/code&gt; positions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to return the &lt;code&gt;head&lt;/code&gt; of the new linked list after the rotation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Intuition:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We've encountered such type of problems in arrays first and truth be told, we are not going to change our approach much here.&lt;/li&gt;
&lt;li&gt;We know that if k &amp;gt; length of the list we need it's modulus because it's just one length wrapped around + rotations.
-We need to find out what will be the end in our rotated list.&lt;/li&gt;
&lt;li&gt;Our len - k will give us the ending node of the final list, so now we know what should be null.&lt;/li&gt;
&lt;li&gt;We just make the node next to it head and our ending point null after making it a circular list.&lt;/li&gt;
&lt;li&gt;Let's write approach for better understanding.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Approach
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Base Cases&lt;/strong&gt;: First, we handle the base cases where no rotation is needed or where the input linked list is empty or contains only one node. In these cases, we directly return the &lt;code&gt;head&lt;/code&gt; since no rotation is required.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Calculate the Length&lt;/strong&gt;: We need to determine the length of the linked list to understand how many positions to rotate. We traverse the linked list once and count the number of nodes to find the length.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Make the List Circular&lt;/strong&gt;: Since the rotation should wrap around, we connect the last node to the &lt;code&gt;head&lt;/code&gt;, effectively making it circular.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Calculate the New Head&lt;/strong&gt;: To find the new &lt;code&gt;head&lt;/code&gt; of the rotated list, we need to locate the node that will become the first node in the rotated list. This node is located at a position &lt;code&gt;len - k&lt;/code&gt;, where &lt;code&gt;len&lt;/code&gt; is the length of the list and &lt;code&gt;k&lt;/code&gt; is the number of positions to rotate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Break the Circular List&lt;/strong&gt;: After determining the new &lt;code&gt;head&lt;/code&gt;, we need to break the circular list. We set the &lt;code&gt;next&lt;/code&gt; of the node at position &lt;code&gt;len - k&lt;/code&gt; to &lt;code&gt;nullptr&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Return the Result&lt;/strong&gt;: Finally, we return the new &lt;code&gt;head&lt;/code&gt;, which represents the rotated linked list.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;//only rotate by len % k&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;//we got the length now;&lt;/span&gt;
        &lt;span class="c1"&gt;//make it circular&lt;/span&gt;
        &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Complexity Analysis:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: The algorithm makes a single pass through the linked list to calculate its length, so its time complexity is O(N), where N is the number of nodes in the linked list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: The algorithm uses a constant amount of extra space for variables, so the space complexity is O(1).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>discuss</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>DAY 100 - 1282. Group the People Given the Group Size They Belong To</title>
      <dc:creator>Nayan Pahuja</dc:creator>
      <pubDate>Mon, 11 Sep 2023 07:46:02 +0000</pubDate>
      <link>https://dev.to/pahujanayan/day-100-1282-group-the-people-given-the-group-size-they-belong-to-48cc</link>
      <guid>https://dev.to/pahujanayan/day-100-1282-group-the-people-given-the-group-size-they-belong-to-48cc</guid>
      <description>&lt;p&gt;Hey Guys! Welcome to the 100th Day in our 100DaysOfCodeChallenge.&lt;br&gt;
Before starting today's question, I would like to thank anyone who has spared time to read what I've written over the course of 100 days and anyone who reads this into the future. I won't say much but the journey has been amazing and I recommend everyone to take this challenge at least once. It's a whole another feeling of it's own.&lt;/p&gt;

&lt;p&gt;I am going to take a quick break of a week or something probably less and I will be back but I will be writing more freely now and will try other things than Data Structures and Algorithm Articles.&lt;/p&gt;

&lt;p&gt;Let's get back to our work, today we are going to sovle today's daily leetcode question.&lt;/p&gt;




&lt;h2&gt;
  
  
  Question: &lt;a href="https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/description/" rel="noopener noreferrer"&gt;1282. Group the People Given the Group Size They Belong To&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;There are &lt;code&gt;n&lt;/code&gt; people that are split into some unknown number of groups. Each person is labeled with a &lt;strong&gt;unique ID&lt;/strong&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n - 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;groupSizes&lt;/code&gt;, where &lt;code&gt;groupSizes[i]&lt;/code&gt; is the size of the group that person &lt;code&gt;i&lt;/code&gt; is in. For example, if &lt;code&gt;groupSizes[1] = 3&lt;/code&gt;, then person &lt;code&gt;1&lt;/code&gt; must be in a group of size &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return a list of groups such that each person &lt;code&gt;i&lt;/code&gt; is in a group of size &lt;code&gt;groupSizes[i]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Each person should appear in &lt;strong&gt;exactly one group&lt;/strong&gt;, and every person must be in a group. If there are multiple answers,&lt;strong&gt;return any of them&lt;/strong&gt;. It is &lt;strong&gt;guaranteed&lt;/strong&gt; that there will be &lt;strong&gt;at least one&lt;/strong&gt; valid solution for the given input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Example 1:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; groupSizes = [3,3,3,3,3,1,3]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[5],[0,1,2],[3,4,6]]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
The first group is [5]. The size is 1, and groupSizes[5] = 1.&lt;br&gt;
The second group is [0,1,2]. The size is 3, and groupSizes[0] = groupSizes[1] = groupSizes[2] = 3.&lt;br&gt;
The third group is [3,4,6]. The size is 3, and groupSizes[3] = groupSizes[4] = groupSizes[6] = 3.&lt;br&gt;
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Example 2:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; groupSizes = [2,1,3,3,3,2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[1],[0,5],[2,3,4]]&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Understanding the Problem
&lt;/h2&gt;

&lt;p&gt;Before we delve into the code, let's gain a clear understanding of the problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We're given an array &lt;code&gt;groupSizes&lt;/code&gt; where &lt;code&gt;groupSizes[i]&lt;/code&gt; represents the group size that the &lt;code&gt;i&lt;/code&gt;-th person belongs to.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our task is to group people together based on their group sizes while ensuring that each group contains exactly &lt;code&gt;groupSizes[i]&lt;/code&gt; people.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The order of people within each group does not matter, but the order of groups in the result matters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From these observations it's clear to us we need to somehow maintain which indexed person it is, his groupSize and he must go into that groupSize.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Intuiton:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;By breaking down the question we realize we have to keep tracks of two things: &lt;br&gt;
&lt;strong&gt;First:&lt;/strong&gt; The &lt;code&gt;groupSizes&lt;/code&gt; of the &lt;code&gt;i'th&lt;/code&gt; index denote the &lt;code&gt;groupSize&lt;/code&gt; it must be in.&lt;br&gt;
&lt;strong&gt;Second:&lt;/strong&gt; We also need to keep track of other members of that specific group size.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We are related by two things, the &lt;code&gt;key&lt;/code&gt; (size of the group) and it's members(indexes of that specific group size).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can simply use an &lt;code&gt;unordered_map&amp;lt;int,vector&amp;lt;int&amp;gt;&amp;gt;&lt;/code&gt; here as it will help us keep track of both the things. Our map will store a vector containing members corresponding to our keys.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But if we store all the members they might be greater than the &lt;code&gt;groupSize&lt;/code&gt; and we might need to make more groups for it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's head down to the approach we used to solve this:&lt;/p&gt;




&lt;h2&gt;
  
  
  The Approach
&lt;/h2&gt;

&lt;p&gt;To solve this problem efficiently, we can use a systematic approach. Here's the intuition behind the approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize a Data Structure&lt;/strong&gt;: We start by initializing two data structures: a vector of vectors called &lt;code&gt;result&lt;/code&gt; to store the final answer and an unordered map called &lt;code&gt;mp&lt;/code&gt; to help us organize the people based on their group sizes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Iterate Through the Input Array&lt;/strong&gt;: We traverse the &lt;code&gt;groupSizes&lt;/code&gt; array using a loop. In each iteration, we perform the following steps:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Extract the &lt;code&gt;key&lt;/code&gt;, which represents the group size for the current person.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the person's index (representing a member) to the corresponding group in our &lt;code&gt;mp&lt;/code&gt; map.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check if the group is now full, meaning it contains exactly &lt;code&gt;key&lt;/code&gt; people. If so, we move all the members of that group from &lt;code&gt;mp&lt;/code&gt; to our &lt;code&gt;result&lt;/code&gt; vector. This ensures that we have created a complete group, and we can remove the group size from &lt;code&gt;mp&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Return the Result&lt;/strong&gt;: Finally, we return the &lt;code&gt;result&lt;/code&gt; vector, which contains all the groups of people based on their group sizes.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;groupThePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;groupSizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;unordered_map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;groupSizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;groupSizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;//this is the groupSize for that index&lt;/span&gt;

            &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//push the members of that specific group&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// group is full so push that into our answer&lt;/span&gt;
                &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
                &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;erase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;



        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Complexity Analysis:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: We make a single pass through the &lt;code&gt;groupSizes&lt;/code&gt; array, so its time complexity is O(N), where N is the length of the input array and our insertion and deletion in unordered_map is O(1). So our overall time complexity is O(N).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: We use two data structures, the &lt;code&gt;result&lt;/code&gt; vector and the &lt;code&gt;mp&lt;/code&gt; unordered map. The space required for &lt;code&gt;result&lt;/code&gt; is proportional to the number of groups and can be as large as N/2 in the worst case. The space used by &lt;code&gt;mp&lt;/code&gt; is also proportional to the number of groups. Therefore, the space complexity is O(N).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
