<?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: Vansh Aggarwal</title>
    <description>The latest articles on DEV Community by Vansh Aggarwal (@vansh_aggarwal_5fb2fff667).</description>
    <link>https://dev.to/vansh_aggarwal_5fb2fff667</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%2F3840143%2F0a808966-4b40-40a8-9beb-f30de0fa323d.png</url>
      <title>DEV Community: Vansh Aggarwal</title>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vansh_aggarwal_5fb2fff667"/>
    <language>en</language>
    <item>
      <title>LeetCode Solution: 263. Ugly Number</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sat, 23 May 2026 19:55:31 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-263-ugly-number-5dkk</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-263-ugly-number-5dkk</guid>
      <description>&lt;h1&gt;
  
  
  Demystifying Ugly Numbers: A LeetCode Adventure (Problem 263)
&lt;/h1&gt;

&lt;p&gt;Hey fellow coders and aspiring problem-solvers! 👋 Vansh2710 here, ready to dive into another fun and beginner-friendly LeetCode challenge. Today, we're tackling &lt;strong&gt;Problem 263: Ugly Number&lt;/strong&gt;. Don't let the name scare you; these "ugly" numbers are quite elegant once you understand them!&lt;/p&gt;

&lt;p&gt;This problem is a fantastic way to practice basic arithmetic operations and &lt;code&gt;while&lt;/code&gt; loops, laying a solid foundation for more complex algorithms. So, grab your favorite beverage, and let's unravel the mystery of ugly numbers together!&lt;/p&gt;




&lt;h2&gt;
  
  
  🧐 What Exactly is an Ugly Number?
&lt;/h2&gt;

&lt;p&gt;The problem defines an &lt;strong&gt;ugly number&lt;/strong&gt; as a &lt;strong&gt;positive integer&lt;/strong&gt; whose prime factors are &lt;em&gt;only&lt;/em&gt; 2, 3, and 5.&lt;/p&gt;

&lt;p&gt;Let's break that down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Positive Integer:&lt;/strong&gt; This means we're only looking at numbers like 1, 2, 3, 4, 5, etc. Zero and negative numbers are not considered ugly.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Prime Factors:&lt;/strong&gt; These are the prime numbers that multiply together to make up the original number. For example, the prime factors of 12 are 2, 2, and 3 (since 2 * 2 * 3 = 12).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Only 2, 3, and 5:&lt;/strong&gt; This is the crucial part! If a number has &lt;em&gt;any&lt;/em&gt; other prime factor (like 7, 11, 13, etc.), it's NOT an ugly number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's look at the examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;n = 6&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Prime factors of 6 are 2 and 3.&lt;/li&gt;
&lt;li&gt;  Both 2 and 3 are allowed prime factors (from the set {2, 3, 5}).&lt;/li&gt;
&lt;li&gt;  Therefore, 6 is an ugly number. (Output: &lt;code&gt;true&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;n = 1&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  1 has no prime factors (it's a special case, considered "vacuously" true because it has &lt;em&gt;no&lt;/em&gt; prime factors that &lt;em&gt;aren't&lt;/em&gt; 2, 3, or 5).&lt;/li&gt;
&lt;li&gt;  Therefore, 1 is an ugly number. (Output: &lt;code&gt;true&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;n = 14&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Prime factors of 14 are 2 and 7.&lt;/li&gt;
&lt;li&gt;  While 2 is allowed, 7 is &lt;em&gt;not&lt;/em&gt; in our allowed set {2, 3, 5}.&lt;/li&gt;
&lt;li&gt;  Therefore, 14 is NOT an ugly number. (Output: &lt;code&gt;false&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🤔 The "Aha!" Moment - Intuition
&lt;/h2&gt;

&lt;p&gt;How can we programmatically check if a number only has 2, 3, and 5 as prime factors?&lt;/p&gt;

&lt;p&gt;Imagine you have a number, say 30.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Is it divisible by 2? Yes! &lt;code&gt;30 / 2 = 15&lt;/code&gt;. Now our number is 15.&lt;/li&gt;
&lt;li&gt;  Is 15 divisible by 2? No.&lt;/li&gt;
&lt;li&gt;  Is 15 divisible by 3? Yes! &lt;code&gt;15 / 3 = 5&lt;/code&gt;. Now our number is 5.&lt;/li&gt;
&lt;li&gt;  Is 5 divisible by 2? No.&lt;/li&gt;
&lt;li&gt;  Is 5 divisible by 3? No.&lt;/li&gt;
&lt;li&gt;  Is 5 divisible by 5? Yes! &lt;code&gt;5 / 5 = 1&lt;/code&gt;. Now our number is 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We've successfully reduced 30 to 1 by only dividing by 2, 3, and 5. This tells us that 30 is an ugly number!&lt;/p&gt;

&lt;p&gt;What if we had 14?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Is it divisible by 2? Yes! &lt;code&gt;14 / 2 = 7&lt;/code&gt;. Now our number is 7.&lt;/li&gt;
&lt;li&gt;  Is 7 divisible by 2? No.&lt;/li&gt;
&lt;li&gt;  Is 7 divisible by 3? No.&lt;/li&gt;
&lt;li&gt;  Is 7 divisible by 5? No.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point, our number (7) is still greater than 1, but it's not divisible by any of our allowed prime factors (2, 3, 5). This means 7 itself must be a prime factor that is &lt;em&gt;not&lt;/em&gt; 2, 3, or 5. Therefore, 14 is not an ugly number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The core intuition is:&lt;/strong&gt; If a number &lt;code&gt;n&lt;/code&gt; is ugly, we should be able to keep dividing it by 2, 3, or 5 until it eventually becomes 1. If at any point, &lt;code&gt;n&lt;/code&gt; is greater than 1 but not divisible by any of these three primes, then it must have another prime factor, making it not ugly.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Step-by-Step Approach
&lt;/h2&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;  According to the definition, ugly numbers must be &lt;strong&gt;positive integers&lt;/strong&gt;. So, if &lt;code&gt;n&lt;/code&gt; is 0 or negative, it's immediately &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  The number 1 is a special case. It has no prime factors, so it satisfies the condition. Our loop-based approach will correctly handle this as &lt;code&gt;n=1&lt;/code&gt; will not enter the &lt;code&gt;while(n &amp;gt; 1)&lt;/code&gt; loop and directly return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Iterative Division:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  We'll use a &lt;code&gt;while&lt;/code&gt; loop that continues as long as &lt;code&gt;n&lt;/code&gt; is greater than 1. The goal is to reduce &lt;code&gt;n&lt;/code&gt; to 1.&lt;/li&gt;
&lt;li&gt;  Inside the loop, we check for divisibility by 2, 3, and 5. The order doesn't strictly matter, but a consistent order (e.g., 2, then 3, then 5) is clean.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;If &lt;code&gt;n&lt;/code&gt; is divisible by 2:&lt;/strong&gt; Divide &lt;code&gt;n&lt;/code&gt; by 2 (&lt;code&gt;n = n / 2&lt;/code&gt;). We've factored out a '2'.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Else if &lt;code&gt;n&lt;/code&gt; is divisible by 3:&lt;/strong&gt; Divide &lt;code&gt;n&lt;/code&gt; by 3 (&lt;code&gt;n = n / 3&lt;/code&gt;). We've factored out a '3'.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Else if &lt;code&gt;n&lt;/code&gt; is divisible by 5:&lt;/strong&gt; Divide &lt;code&gt;n&lt;/code&gt; by 5 (&lt;code&gt;n = n / 5&lt;/code&gt;). We've factored out a '5'.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Else (Crucial Check):&lt;/strong&gt; If &lt;code&gt;n&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; divisible by 2, 3, or 5 at this point, but it's still greater than 1, it means &lt;code&gt;n&lt;/code&gt; must have a prime factor other than 2, 3, or 5. In this scenario, &lt;code&gt;n&lt;/code&gt; is not an ugly number, so we immediately return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Final Result:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  If the &lt;code&gt;while&lt;/code&gt; loop finishes (meaning &lt;code&gt;n&lt;/code&gt; has been successfully reduced to 1), it implies that all its prime factors were indeed 2, 3, or 5. In this case, we return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  💻 The Code
&lt;/h2&gt;

&lt;p&gt;Here's the C++ implementation following our approach:&lt;br&gt;
&lt;/p&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;bool&lt;/span&gt; &lt;span class="n"&gt;isUgly&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// Handle non-positive numbers: Ugly numbers are positive.&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;&amp;lt;=&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Loop while n is greater than 1.&lt;/span&gt;
        &lt;span class="c1"&gt;// We aim to reduce n to 1 by dividing out factors of 2, 3, and 5.&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="c1"&gt;// Try dividing by 2&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;2&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="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;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="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// Else, try dividing by 3&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&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;3&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="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;n&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// Else, try dividing by 5&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&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;5&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="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;n&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// If n is not divisible by 2, 3, or 5, and it's still &amp;gt; 1,&lt;/span&gt;
            &lt;span class="c1"&gt;// then it must have a prime factor other than 2, 3, or 5.&lt;/span&gt;
            &lt;span class="k"&gt;else&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;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Not an ugly number&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// If the loop completes, it means n was successfully reduced to 1,&lt;/span&gt;
        &lt;span class="c1"&gt;// implying all its prime factors were 2, 3, or 5.&lt;/span&gt;
        &lt;span class="c1"&gt;// Also handles the case n = 1 initially (loop won't run, returns true).&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&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;p&gt;Let's break down how efficient our solution is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Time Complexity: O(log n)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  In each iteration of the &lt;code&gt;while&lt;/code&gt; loop, we divide &lt;code&gt;n&lt;/code&gt; by at least 2 (if it's divisible by 2, 3, or 5).&lt;/li&gt;
&lt;li&gt;  This means &lt;code&gt;n&lt;/code&gt; decreases exponentially, similar to how many times you can divide a number by 2 until it reaches 1 (e.g., &lt;code&gt;log_2(n)&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  Therefore, the number of operations is proportional to the logarithm of &lt;code&gt;n&lt;/code&gt;. For example, if &lt;code&gt;n&lt;/code&gt; is &lt;code&gt;2^30&lt;/code&gt;, it takes about 30 divisions.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Space Complexity: O(1)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  We are only using a few constant variables (&lt;code&gt;n&lt;/code&gt;). We are not allocating any additional data structures whose size depends on the input &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  💡 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Iterative Reduction:&lt;/strong&gt; Many problems can be solved by iteratively reducing the input towards a base case. Here, we reduce &lt;code&gt;n&lt;/code&gt; to 1.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Handling Edge Cases:&lt;/strong&gt; Always consider special inputs like &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, and negative numbers.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Prime Factorization Logic:&lt;/strong&gt; This problem teaches a fundamental approach to checking prime factors. If you can divide out all known prime factors (2, 3, 5) and the number reduces to 1, then it's composed &lt;em&gt;only&lt;/em&gt; of those factors. If it stops reducing and is still &amp;gt; 1, it has other factors.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;The "Else" Clause is Key:&lt;/strong&gt; The &lt;code&gt;else { return false; }&lt;/code&gt; inside the loop is critical for identifying numbers with forbidden prime factors.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;That's it for Ugly Numbers! This problem is a great example of how simple arithmetic operations can be combined to solve interesting challenges. I hope you found this explanation clear and helpful.&lt;/p&gt;

&lt;p&gt;Happy coding, and see you in the next one!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Author Account: Vansh2710&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Time Published: 2026-05-24 01:25:09&lt;/em&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 1752. Check if Array Is Sorted and Rotated</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sat, 23 May 2026 18:17:13 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1752-check-if-array-is-sorted-and-rotated-996</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1752-check-if-array-is-sorted-and-rotated-996</guid>
      <description>&lt;h1&gt;
  
  
  🔄 LeetCode 1752: Can You Un-Rotate This Array? (A Beginner's Guide)
&lt;/h1&gt;

&lt;p&gt;Hey there, fellow coders! 👋 Vansh2710 here, ready to demystify another exciting LeetCode challenge. Today, we're tackling problem 1752: "Check if Array Is Sorted and Rotated." Sounds a bit like a puzzle, right? Don't worry, we'll break it down piece by piece and uncover a super elegant solution!&lt;/p&gt;

&lt;p&gt;This problem is a fantastic way to sharpen your array manipulation and logical thinking skills. It might seem tricky at first, but with a simple trick, it becomes incredibly straightforward. Let's dive in!&lt;/p&gt;




&lt;h3&gt;
  
  
  What's the Problem All About? 🧐
&lt;/h3&gt;

&lt;p&gt;Imagine you have a perfectly sorted list of numbers, like &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt;.&lt;br&gt;
Now, imagine you &lt;em&gt;rotate&lt;/em&gt; it. This means you take some elements from the beginning and move them to the end, keeping their relative order.&lt;/p&gt;

&lt;p&gt;For example, if you rotate &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt; by 2 positions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Take &lt;code&gt;1, 2&lt;/code&gt; and move them to the end: &lt;code&gt;[3, 4, 5, 1, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The problem asks: &lt;strong&gt;Given an array &lt;code&gt;nums&lt;/code&gt;, can we tell if it &lt;em&gt;could have been&lt;/em&gt; originally sorted (non-decreasingly) and then rotated some number of times (even zero rotations)?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key things to remember:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Non-decreasing order:&lt;/strong&gt; &lt;code&gt;[1, 2, 2, 3]&lt;/code&gt; is sorted. &lt;code&gt;[3, 2, 1]&lt;/code&gt; is not.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Rotation:&lt;/strong&gt; Can be any number of positions, including 0 (no rotation).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Duplicates are allowed:&lt;/strong&gt; This is an important detail! &lt;code&gt;[3, 4, 5, 5, 1, 2]&lt;/code&gt; could be &lt;code&gt;[1, 2, 3, 4, 5, 5]&lt;/code&gt; rotated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's look at examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;nums = [3, 4, 5, 1, 2]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Is &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt; sorted? Yes.&lt;/li&gt;
&lt;li&gt;  Can &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt; be rotated to get &lt;code&gt;[3, 4, 5, 1, 2]&lt;/code&gt;? Yes, rotate by 2 positions.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;nums = [2, 1, 3, 4]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  If sorted, it would be &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Can &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt; be rotated to &lt;code&gt;[2, 1, 3, 4]&lt;/code&gt;? No. The &lt;code&gt;2, 1&lt;/code&gt; part breaks the sorted order that a single rotation would maintain.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;nums = [1, 2, 3]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Is it sorted? Yes.&lt;/li&gt;
&lt;li&gt;  Can it be rotated to get &lt;code&gt;[1, 2, 3]&lt;/code&gt;? Yes, 0 rotations.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  The "Aha!" Moment - Our Intuition ✨
&lt;/h3&gt;

&lt;p&gt;Think about a truly sorted array like &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt;. If you go from left to right, &lt;code&gt;nums[i-1]&lt;/code&gt; is always less than or equal to &lt;code&gt;nums[i]&lt;/code&gt;. There are no "drops" or "descents" in value.&lt;/p&gt;

&lt;p&gt;Now, consider a &lt;em&gt;rotated&lt;/em&gt; sorted array: &lt;code&gt;[3, 4, 5, 1, 2]&lt;/code&gt;.&lt;br&gt;
If you scan this, you'll see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;3 &amp;lt;= 4&lt;/code&gt; (OK)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;4 &amp;lt;= 5&lt;/code&gt; (OK)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;5 &amp;gt; 1&lt;/code&gt; (AHA! A &lt;strong&gt;descent&lt;/strong&gt;!)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= 2&lt;/code&gt; (OK)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice something? There's only &lt;em&gt;one place&lt;/em&gt; where the non-decreasing order is broken: &lt;code&gt;5&lt;/code&gt; followed by &lt;code&gt;1&lt;/code&gt;. This "break" is exactly where the rotation happened! The elements &lt;code&gt;[1, 2]&lt;/code&gt; were moved from the beginning to the end, creating this single drop in value.&lt;/p&gt;

&lt;p&gt;What if there are &lt;em&gt;two&lt;/em&gt; descents? For example, &lt;code&gt;[2, 1, 3, 4]&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;2 &amp;gt; 1&lt;/code&gt; (Descent 1)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= 3&lt;/code&gt; (OK)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;3 &amp;lt;= 4&lt;/code&gt; (OK)
Here, we have one descent. What about the wrap-around? &lt;code&gt;4 &amp;gt; 2&lt;/code&gt;? No.
Actually, if the original array was &lt;code&gt;[1,2,3,4]&lt;/code&gt;, and we rotated it, we would &lt;em&gt;never&lt;/em&gt; get &lt;code&gt;[2,1,3,4]&lt;/code&gt;.
A single rotation implies that &lt;em&gt;all&lt;/em&gt; elements after the rotation point are smaller than &lt;em&gt;all&lt;/em&gt; elements before it. The only "break" can be at the rotation point.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the core intuition is: &lt;strong&gt;A sorted and rotated array will have at most ONE "descent" (where &lt;code&gt;nums[i-1] &amp;gt; nums[i]&lt;/code&gt;) when you iterate through it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wait, what about the wrap-around?&lt;/strong&gt;&lt;br&gt;
Consider &lt;code&gt;[4, 5, 1, 2, 3]&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;4 &amp;lt;= 5&lt;/code&gt; (OK)&lt;br&gt;
&lt;code&gt;5 &amp;gt; 1&lt;/code&gt; (Descent!)&lt;br&gt;
&lt;code&gt;1 &amp;lt;= 2&lt;/code&gt; (OK)&lt;br&gt;
&lt;code&gt;2 &amp;lt;= 3&lt;/code&gt; (OK)&lt;br&gt;
Here, we have one descent. But also, &lt;code&gt;nums[n-1]&lt;/code&gt; (which is &lt;code&gt;3&lt;/code&gt;) is less than &lt;code&gt;nums[0]&lt;/code&gt; (which is &lt;code&gt;4&lt;/code&gt;). This comparison also forms part of the "break" in sorted order, conceptually wrapping around the array. Our descent counter needs to account for this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Revised Intuition:&lt;/strong&gt; Count the number of times &lt;code&gt;nums[i-1] &amp;gt; nums[i]&lt;/code&gt;. This count should be at most 1, &lt;em&gt;including&lt;/em&gt; the wrap-around comparison between the last and first elements.&lt;/p&gt;


&lt;h3&gt;
  
  
  Step-by-Step Approach 🚶‍♂️
&lt;/h3&gt;

&lt;p&gt;Let's formalize our intuition into a clear algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize a counter:&lt;/strong&gt; We'll need a variable, let's call it &lt;code&gt;descentCount&lt;/code&gt;, and set it to &lt;code&gt;0&lt;/code&gt;. This counter will track how many times we find &lt;code&gt;nums[i-1] &amp;gt; nums[i]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Iterate through the array:&lt;/strong&gt; Loop from the second element (&lt;code&gt;i = 1&lt;/code&gt;) up to the end of the array (&lt;code&gt;nums.size() - 1&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  In each iteration, compare the current element (&lt;code&gt;nums[i]&lt;/code&gt;) with the previous one (&lt;code&gt;nums[i-1]&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  If &lt;code&gt;nums[i-1] &amp;gt; nums[i]&lt;/code&gt;, it means we've found a "descent" or a break in the non-decreasing order. Increment &lt;code&gt;descentCount&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check the wrap-around case:&lt;/strong&gt; After the loop finishes, we need to consider the connection between the &lt;em&gt;last&lt;/em&gt; element and the &lt;em&gt;first&lt;/em&gt; element. In a rotated sorted array, if there was a rotation, the last element might be greater than the first element (e.g., in &lt;code&gt;[3, 4, 5, 1, 2]&lt;/code&gt;, &lt;code&gt;nums[4]&lt;/code&gt; (which is &lt;code&gt;2&lt;/code&gt;) is less than &lt;code&gt;nums[0]&lt;/code&gt; (which is &lt;code&gt;3&lt;/code&gt;). This doesn't add a descent.&lt;br&gt;
However, in &lt;code&gt;[1,2,3]&lt;/code&gt; (0 rotations), &lt;code&gt;nums[2]&lt;/code&gt; (3) is greater than &lt;code&gt;nums[0]&lt;/code&gt; (1).&lt;br&gt;
Let's re-evaluate: The descent is where the sequence &lt;em&gt;decreases&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;[3, 4, 5, 1, 2]&lt;/code&gt; -&amp;gt; &lt;code&gt;5 &amp;gt; 1&lt;/code&gt; (1 descent)

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums[n-1]&lt;/code&gt; (2) is &lt;em&gt;not&lt;/em&gt; greater than &lt;code&gt;nums[0]&lt;/code&gt; (3).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;[1, 2, 3]&lt;/code&gt; -&amp;gt; No descents.

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums[n-1]&lt;/code&gt; (3) is &lt;em&gt;not&lt;/em&gt; greater than &lt;code&gt;nums[0]&lt;/code&gt; (1).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;[2, 1, 3, 4]&lt;/code&gt; -&amp;gt; &lt;code&gt;2 &amp;gt; 1&lt;/code&gt; (1 descent)

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums[n-1]&lt;/code&gt; (4) is &lt;em&gt;not&lt;/em&gt; greater than &lt;code&gt;nums[0]&lt;/code&gt; (2).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My logic for the wrap-around check in the solution code is &lt;code&gt;if (nums[n-1] &amp;gt; nums[0])&lt;/code&gt;. Let's trace it with examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;[3,4,5,1,2]&lt;/code&gt; (n=5)

&lt;ul&gt;
&lt;li&gt;  Loop: &lt;code&gt;(5 &amp;gt; 1)&lt;/code&gt; -&amp;gt; &lt;code&gt;descentCount = 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Wrap-around: &lt;code&gt;nums[4]&lt;/code&gt; (2) &lt;code&gt;&amp;gt;&lt;/code&gt; &lt;code&gt;nums[0]&lt;/code&gt; (3) is &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Total &lt;code&gt;descentCount = 1&lt;/code&gt;. Return &lt;code&gt;true&lt;/code&gt;. Correct.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*   `[2,1,3,4]` (n=4)
    *   Loop: `(2 &amp;gt; 1)` -&amp;gt; `descentCount = 1`
    *   Wrap-around: `nums[3]` (4) `&amp;gt;` `nums[0]` (2) is `true`. -&amp;gt; `descentCount = 2`.
    *   Total `descentCount = 2`. Return `false`. Correct. (Because original `[1,2,3,4]` cannot be rotated to `[2,1,3,4]`. It has two "breaks" if you consider it circular: `2-&amp;gt;1` and `4-&amp;gt;2` (conceptual circular link)).

*   `[1,2,3]` (n=3)
    *   Loop: No descents. `descentCount = 0`.
    *   Wrap-around: `nums[2]` (3) `&amp;gt;` `nums[0]` (1) is `true`. -&amp;gt; `descentCount = 1`.
    *   Total `descentCount = 1`. Return `true`. Correct. (A sorted array is considered a 0-rotated sorted array).

This wrap-around check for `nums[n-1] &amp;gt; nums[0]` cleverly handles the case where the "rotation point" effectively exists between the last and first element *if the array was originally sorted*. If `[1,2,3]` is seen as `1,2,3,1` (circular), then `3 &amp;gt; 1` is a descent. This means a perfectly sorted array will register 1 descent with this method.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Final Check:&lt;/strong&gt; After iterating through the array and checking the wrap-around, if &lt;code&gt;descentCount&lt;/code&gt; is less than or equal to &lt;code&gt;1&lt;/code&gt;, it means the array &lt;em&gt;could have been&lt;/em&gt; sorted and rotated. Return &lt;code&gt;true&lt;/code&gt;. Otherwise, if &lt;code&gt;descentCount&lt;/code&gt; is greater than &lt;code&gt;1&lt;/code&gt;, it means there are too many "breaks" in the sorted order for it to be a single rotation of a sorted array. Return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;h3&gt;
  
  
  Show Me the Code! 💻
&lt;/h3&gt;

&lt;p&gt;Here's the C++ implementation of this logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt; // Don't forget to include necessary headers!&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt; // For potential testing, though not strictly part of the solution&lt;/span&gt;&lt;span class="cp"&gt;
&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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&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;count&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="c1"&gt;// Initialize descent counter&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;nums&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;// Get the size of the array&lt;/span&gt;

        &lt;span class="c1"&gt;// Iterate from the second element to compare with the previous&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;1&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="c1"&gt;// If the previous element is greater than the current, it's a descent&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;nums&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;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;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;count&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="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Handle the wrap-around case: compare the last element with the first&lt;/span&gt;
        &lt;span class="c1"&gt;// If nums[n-1] is greater than nums[0], it means a "descent" occurs&lt;/span&gt;
        &lt;span class="c1"&gt;// conceptually between the end and the beginning of the array.&lt;/span&gt;
        &lt;span class="c1"&gt;// E.g., for [1,2,3], nums[2](3) &amp;gt; nums[0](1) is true.&lt;/span&gt;
        &lt;span class="c1"&gt;// For [3,4,5,1,2], nums[4](2) &amp;gt; nums[0](3) is false.&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;nums&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;&amp;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="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;count&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;// A sorted and rotated array can have at most one "descent" (break).&lt;/span&gt;
        &lt;span class="c1"&gt;// This includes the wrap-around check, meaning a perfectly sorted array&lt;/span&gt;
        &lt;span class="c1"&gt;// will result in count = 1 due to the wrap-around check.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Complexity Analysis ⏱️🚀
&lt;/h3&gt;

&lt;p&gt;Let &lt;code&gt;N&lt;/code&gt; be the number of elements in the &lt;code&gt;nums&lt;/code&gt; array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  We iterate through the array once in the &lt;code&gt;for&lt;/code&gt; loop, which takes &lt;code&gt;O(N)&lt;/code&gt; time.&lt;/li&gt;
&lt;li&gt;  All other operations (initialization, &lt;code&gt;size()&lt;/code&gt; call, final comparison) take constant time, &lt;code&gt;O(1)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Therefore, the total time complexity is dominated by the loop, making it &lt;code&gt;O(N)&lt;/code&gt;. This is highly efficient as we only need to pass through the array once.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity: O(1)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  We only use a few extra variables (&lt;code&gt;count&lt;/code&gt;, &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;i&lt;/code&gt;) to store our state, regardless of the input array size.&lt;/li&gt;
&lt;li&gt;  No auxiliary data structures (like new arrays or hash maps) are used that scale with the input size.&lt;/li&gt;
&lt;li&gt;  Thus, the space complexity is constant, &lt;code&gt;O(1)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Key Takeaways ✨
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Spotting the "Breaks":&lt;/strong&gt; The most crucial insight is that a sorted and rotated array will have at most one point where the non-decreasing order is violated.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;The Wrap-Around is Tricky:&lt;/strong&gt; Don't forget the connection between the last element and the first! This is where many solutions go wrong. Our approach correctly includes this as another potential "descent."&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Simple is Often Best:&lt;/strong&gt; This problem could tempt you into complex sorting or array manipulation, but a single pass and a counter prove to be the most elegant and efficient solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach is clean, efficient, and demonstrates a good understanding of array properties! Keep practicing, and you'll master these patterns in no time. Happy coding!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Author Account:&lt;/strong&gt; Vansh2710&lt;br&gt;
&lt;strong&gt;Time Published:&lt;/strong&gt; 2026-05-23 23:46:54&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: Test</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sat, 23 May 2026 17:15:02 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-test-5703</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-test-5703</guid>
      <description>&lt;h1&gt;
  
  
  Test
&lt;/h1&gt;

&lt;p&gt;Hello, fellow coders!&lt;/p&gt;

&lt;p&gt;Today, we're diving into what might seem like the simplest LeetCode problem ever, but don't let its simplicity fool you. It's a fantastic way to reinforce the absolute basics of problem-solving, function definition, and even complexity analysis. Sometimes, the best way to get started is by mastering the fundamentals!&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem Explanation
&lt;/h3&gt;

&lt;p&gt;You are tasked with creating a function that, when called, prints the integer &lt;code&gt;1&lt;/code&gt; to the standard output.&lt;/p&gt;

&lt;p&gt;Here are the constraints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The function should not take any arguments.&lt;/li&gt;
&lt;li&gt;  The function does not need to return any value explicitly. Its primary purpose is the side-effect of printing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yes, you read that right. Your mission, should you choose to accept it, is to make your program say "1"!&lt;/p&gt;




&lt;h3&gt;
  
  
  Intuition
&lt;/h3&gt;

&lt;p&gt;At first glance, this problem might make you chuckle. "Just print 1? Is that it?" And honestly, yes, that's pretty much it! The "aha!" moment here isn't about some complex algorithm or data structure. It's about recognizing that sometimes, the problem statement &lt;em&gt;literally&lt;/em&gt; gives you the answer.&lt;/p&gt;

&lt;p&gt;The key intuition is to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Understand the direct instruction:&lt;/strong&gt; The problem explicitly states "print the integer &lt;code&gt;1&lt;/code&gt;."&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Identify the tool:&lt;/strong&gt; Every programming language has a way to output text or numbers to the console (e.g., &lt;code&gt;print()&lt;/code&gt; in Python, &lt;code&gt;console.log()&lt;/code&gt; in JavaScript, &lt;code&gt;System.out.println()&lt;/code&gt; in Java).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Structure it as a function:&lt;/strong&gt; LeetCode problems usually require you to implement a function, so wrap your direct instruction inside one.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This problem is a gentle reminder that not every challenge requires advanced techniques; some just test your ability to read instructions and use basic language features.&lt;/p&gt;




&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;p&gt;Let's break down the incredibly complex steps (just kidding, it's super straightforward!):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Define a Function:&lt;/strong&gt; We need to create a function structure. Let's name it something simple like &lt;code&gt;solve&lt;/code&gt; or &lt;code&gt;print_one&lt;/code&gt;. Since the problem specifies no arguments, our function signature will reflect that.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Print the Integer:&lt;/strong&gt; Inside this function, we will use our chosen programming language's standard output command to display the integer &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Return Value (Explicitly):&lt;/strong&gt; Since the problem asks us to &lt;em&gt;print&lt;/em&gt; and not &lt;em&gt;return&lt;/em&gt;, our function doesn't strictly need a &lt;code&gt;return&lt;/code&gt; statement. In many languages, a function without an explicit &lt;code&gt;return&lt;/code&gt; will implicitly return &lt;code&gt;None&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, which is perfectly fine for this problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it! Three simple steps to conquer the "print 1" challenge.&lt;/p&gt;




&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;

&lt;p&gt;Here's the Python solution following our approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    This function prints the integer 1 to the console.
    It takes no arguments and returns nothing explicitly.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Example of how you would call the function:
# solve() # This would print '1' to your console
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Time &amp;amp; Space Complexity Analysis
&lt;/h3&gt;

&lt;p&gt;Even for the simplest problems, analyzing complexity is a great habit to build!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Time Complexity: O(1)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  The function performs a single, constant-time operation: printing an integer. This operation doesn't depend on any input size (as there is no input to begin with!), so its execution time remains constant regardless of how many times it's called (barring external system factors). It takes the same amount of time every single time.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Space Complexity: O(1)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  The function uses a constant amount of memory. It doesn't store any data structures that grow with input size. The memory used for the integer &lt;code&gt;1&lt;/code&gt; and the print operation itself is negligible and fixed, not scaling with any hypothetical input.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Key Takeaways
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Read Carefully:&lt;/strong&gt; Always, always, &lt;em&gt;always&lt;/em&gt; read the problem description thoroughly. Sometimes the solution is incredibly direct.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Master the Basics:&lt;/strong&gt; Understanding how to define functions, handle arguments, and perform basic I/O (input/output) like printing is foundational to all programming.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Complexity Matters (Even for Simple Problems):&lt;/strong&gt; Getting into the habit of analyzing time and space complexity, even for trivial tasks, hones a crucial skill for more complex challenges.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Embrace the Learning Curve:&lt;/strong&gt; Every problem, no matter how simple or complex, offers a chance to learn something new or reinforce existing knowledge.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Happy coding, and see you in the next LeetCode adventure!&lt;/p&gt;




&lt;p&gt;Author Account: Test&lt;br&gt;
Publishing Time: 2026-05-23 17:14:50&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 20. Valid Parentheses</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sun, 17 May 2026 05:55:53 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-20-valid-parentheses-35l6</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-20-valid-parentheses-35l6</guid>
      <description>&lt;h1&gt;
  
  
  Cracking LeetCode 20: Valid Parentheses - A Beginner's Guide to Stacks!
&lt;/h1&gt;

&lt;p&gt;Hey there, future coding rockstars! 👋 NavyaSree_14 here, ready to tackle another exciting LeetCode challenge with you. Today, we're diving into problem 20: "Valid Parentheses". Don't let the name scare you; it's a fantastic introduction to a super useful data structure: the &lt;strong&gt;Stack&lt;/strong&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem: Are These Parentheses Playing Fair?
&lt;/h3&gt;

&lt;p&gt;Imagine you're writing code, or maybe a complex mathematical expression. You've got different types of brackets: &lt;code&gt;( )&lt;/code&gt;, &lt;code&gt;{ }&lt;/code&gt;, &lt;code&gt;[ ]&lt;/code&gt;. The problem asks us to determine if a given string, made &lt;em&gt;only&lt;/em&gt; of these characters, has "valid" parentheses.&lt;/p&gt;

&lt;p&gt;What makes them valid? Three simple rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Matching Pairs:&lt;/strong&gt; Every open bracket (&lt;code&gt;(&lt;/code&gt;, &lt;code&gt;{&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt;) must be closed by the &lt;em&gt;same type&lt;/em&gt; of bracket (&lt;code&gt;)&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt;, &lt;code&gt;]&lt;/code&gt;). You can't close &lt;code&gt;(&lt;/code&gt; with &lt;code&gt;}&lt;/code&gt;!&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Correct Order:&lt;/strong&gt; Open brackets must be closed in the correct order. Think of it like nesting dolls: the last doll you opened must be the first one you close.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Leftovers:&lt;/strong&gt; Every close bracket must have a corresponding open bracket. And by the end, no open brackets should be left hanging!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at some examples to make it crystal clear:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;s = "()"&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;(&lt;/code&gt; opens, &lt;code&gt;)&lt;/code&gt; closes it. Correct type, correct order. &lt;strong&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;code&gt;s = "()[]{}"&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;(&lt;/code&gt; and &lt;code&gt;)&lt;/code&gt; match. &lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; match. &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt; match. All good! &lt;strong&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;code&gt;s = "(]"&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;(&lt;/code&gt; opens, but &lt;code&gt;]&lt;/code&gt; tries to close it. Wrong type! &lt;strong&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;code&gt;s = "([)]"&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;(&lt;/code&gt; opens. &lt;code&gt;[&lt;/code&gt; opens. Then &lt;code&gt;)&lt;/code&gt; closes &lt;code&gt;(&lt;/code&gt; (correct type) &lt;em&gt;but&lt;/em&gt; &lt;code&gt;[&lt;/code&gt; was opened &lt;em&gt;after&lt;/em&gt; &lt;code&gt;(&lt;/code&gt;, so &lt;code&gt;[&lt;/code&gt; should be closed &lt;em&gt;before&lt;/em&gt; &lt;code&gt;(&lt;/code&gt;. Incorrect order! &lt;strong&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;code&gt;s = "{[]}"&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;{&lt;/code&gt; opens. &lt;code&gt;[&lt;/code&gt; opens. &lt;code&gt;]&lt;/code&gt; closes &lt;code&gt;[&lt;/code&gt; (correct type, correct order for &lt;code&gt;[]&lt;/code&gt;). &lt;code&gt;}&lt;/code&gt; closes &lt;code&gt;{&lt;/code&gt; (correct type, correct order for &lt;code&gt;{}&lt;/code&gt;). &lt;strong&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The string &lt;code&gt;s&lt;/code&gt; will only contain these six characters, and its length can be up to 10,000.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Intuition: "Who Was Last?" 🤔
&lt;/h3&gt;

&lt;p&gt;How do we keep track of which opening bracket needs to be closed &lt;em&gt;next&lt;/em&gt;? When we see an opening bracket, we need to remember it. When we see a closing bracket, we need to check if it matches the &lt;em&gt;most recently opened&lt;/em&gt; and &lt;em&gt;yet-to-be-closed&lt;/em&gt; bracket.&lt;/p&gt;

&lt;p&gt;This "most recently opened, first to close" behavior is the perfect use case for a &lt;strong&gt;Stack&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Imagine a stack of plates. When you add a plate, it goes on top. When you remove a plate, you take the one from the top. That's &lt;strong&gt;Last-In, First-Out (LIFO)&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  When you see an &lt;strong&gt;opening bracket&lt;/strong&gt;, it's like putting a plate on the stack. You remember it.&lt;/li&gt;
&lt;li&gt;  When you see a &lt;strong&gt;closing bracket&lt;/strong&gt;, it's like taking the top plate off. You expect it to match the most recently added (top) opening bracket. If it does, great! If not, or if there are no plates (no opening brackets), then something is wrong!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Approach: A Stack-Powered Validator
&lt;/h3&gt;

&lt;p&gt;Let's break down the step-by-step logic:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Initialize a Stack:&lt;/strong&gt; We'll need an empty list (in Python, lists can act as stacks) to store our open brackets.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Create a Mapping:&lt;/strong&gt; To quickly check if a closing bracket has a valid opening counterpart, we'll create a dictionary (or hash map) that maps each closing bracket to its corresponding opening bracket.

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;')' : '('&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;'}' : '{'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;']' : '['&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Iterate Through the String:&lt;/strong&gt; We'll go through each character in the input string &lt;code&gt;s&lt;/code&gt; one by one.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*   **If the character is an *opening* bracket (`(`, `{`, `[`):**
    *   Push it onto our `stack`. We're remembering that we saw this open bracket.

*   **If the character is a *closing* bracket (`)`, `}`, `]`):**
    *   **Check for an empty stack:** What if we encounter a closing bracket like `)` but our stack is empty? This means there was no corresponding opening bracket. So, it's invalid!
        *   *Pro Tip (from the solution code):* Instead of explicitly checking `if not stack: return False`, we can pop from the stack and assign a special character (like `'#'`) if the stack is empty. This simplifies the next comparison.
    *   **Pop from the stack:** Get the `top` element from our stack. This `top` element represents the most recently encountered opening bracket.
    *   **Compare:** Use our `mapping` to find what the *expected* opening bracket for our current `char` (the closing bracket) should be. Compare this `expected` opening bracket with the `top` element we just popped.
        *   If `mapping[char]` (the expected open bracket) is **NOT equal** to `top` (the actual last open bracket), then it's a mismatch! The parentheses are invalid. Return `False`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Final Check:&lt;/strong&gt; After iterating through the entire string:

&lt;ul&gt;
&lt;li&gt;  If our &lt;code&gt;stack&lt;/code&gt; is &lt;strong&gt;empty&lt;/strong&gt;, it means every opening bracket found its matching closing bracket in the correct order. The string is valid! Return &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  If our &lt;code&gt;stack&lt;/code&gt; is &lt;strong&gt;not empty&lt;/strong&gt;, it means there are some opening brackets left without corresponding closing brackets. The string is invalid! Return &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Code: Bringing it to Life
&lt;/h3&gt;

&lt;p&gt;Here's the Python code implementing this stack-based approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&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;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

        &lt;span class="c1"&gt;# Mapping for quick lookup: close bracket -&amp;gt; open bracket
&lt;/span&gt;        &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&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;if&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# If it's a closing bracket
&lt;/span&gt;                &lt;span class="c1"&gt;# Pop the top element if stack is not empty, else assign a dummy value
&lt;/span&gt;                &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

                &lt;span class="c1"&gt;# Compare the popped element with the expected opening bracket
&lt;/span&gt;                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# If it's an opening bracket
&lt;/span&gt;                &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# After iterating, if the stack is empty, all brackets matched
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="c1"&gt;# 'not stack' returns True if stack is empty, False otherwise
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Time and Space Complexity Analysis
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Time Complexity: O(N)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  We iterate through the input string &lt;code&gt;s&lt;/code&gt; exactly once. For each character, we perform constant-time operations (dictionary lookup, stack push/pop).&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;N&lt;/code&gt; is the length of the input string.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Space Complexity: O(N)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  In the worst-case scenario (e.g., a string like &lt;code&gt;(((((((((&lt;/code&gt;), our stack could end up storing all &lt;code&gt;N/2&lt;/code&gt; opening brackets.&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;mapping&lt;/code&gt; dictionary uses constant space, as it always stores 3 key-value pairs.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Takeaways
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Stacks are for LIFO problems:&lt;/strong&gt; Whenever you need to process items in a "last-in, first-out" manner (like matching pairs, backtracking, undo operations), think of a stack!&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Mapping for quick lookups:&lt;/strong&gt; Using a dictionary (&lt;code&gt;mapping&lt;/code&gt; in our case) makes it super efficient to check relationships between items.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Edge cases matter:&lt;/strong&gt; Always consider what happens with empty inputs, or when the stack is empty at critical points. Our solution handles this gracefully by checking &lt;code&gt;if stack else '#'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  This problem is a classic and frequently asked. Mastering it truly solidifies your understanding of stacks!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's a wrap for "Valid Parentheses"! I hope this breakdown was clear and helpful. Keep practicing, keep coding, and I'll catch you in the next one!&lt;/p&gt;




&lt;p&gt;Authored by NavyaSree_14 | Published on 2026-05-17 11:25:37&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 20. Valid Parentheses</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sun, 17 May 2026 05:42:28 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-20-valid-parentheses-1o0</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-20-valid-parentheses-1o0</guid>
      <description>&lt;h1&gt;
  
  
  Cracking the Code: The Parenthesis Puzzle with Stacks! (LeetCode 20 Explained)
&lt;/h1&gt;

&lt;p&gt;Hey Devs and future competitive programmers! 👋 NavyaSree_14 here, ready to demystify a classic LeetCode problem that's a fantastic introduction to a super useful data structure: the &lt;strong&gt;Stack&lt;/strong&gt;. We're tackling Problem 20: "Valid Parentheses."&lt;/p&gt;

&lt;p&gt;Don't let the simplicity of parentheses fool you – this problem teaches a fundamental pattern you'll see in many other coding challenges!&lt;/p&gt;




&lt;h2&gt;
  
  
  What's the Puzzle? (Problem Explanation)
&lt;/h2&gt;

&lt;p&gt;Imagine you're a compiler, and you need to check if a line of code has all its parentheses, curly braces, and square brackets matched up correctly. That's exactly what this problem asks us to do!&lt;/p&gt;

&lt;p&gt;We're given a string &lt;code&gt;s&lt;/code&gt; that contains only these six characters: &lt;code&gt;'('&lt;/code&gt;, &lt;code&gt;')'&lt;/code&gt;, &lt;code&gt;'{'&lt;/code&gt;, &lt;code&gt;'}'&lt;/code&gt;, &lt;code&gt;'['&lt;/code&gt;, and &lt;code&gt;']'&lt;/code&gt;. Our job is to determine if the string is "valid."&lt;/p&gt;

&lt;p&gt;What makes it valid? Three simple rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Same Type:&lt;/strong&gt; An open bracket (&lt;code&gt;(&lt;/code&gt;, &lt;code&gt;{&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt;) must be closed by the &lt;em&gt;same type&lt;/em&gt; of bracket (e.g., &lt;code&gt;(&lt;/code&gt; must be closed by &lt;code&gt;)&lt;/code&gt;, not &lt;code&gt;}&lt;/code&gt; or &lt;code&gt;]&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Correct Order:&lt;/strong&gt; Open brackets must be closed in the &lt;em&gt;correct order&lt;/em&gt;. Think about nested structures: &lt;code&gt;([{}])&lt;/code&gt; is valid, but &lt;code&gt;([)]&lt;/code&gt; is not, because the &lt;code&gt;]&lt;/code&gt; is trying to close &lt;code&gt;[&lt;/code&gt; before &lt;code&gt;{&lt;/code&gt; is closed.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Every Close Has an Open:&lt;/strong&gt; Every closing bracket must have a corresponding &lt;em&gt;unclosed&lt;/em&gt; open bracket of the same type before it. You can't just have &lt;code&gt;)&lt;/code&gt; without a preceding &lt;code&gt;(&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at some quick examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;s = "()"&lt;/code&gt; -&amp;gt; &lt;strong&gt;True&lt;/strong&gt; (Simple and perfectly matched!)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s = "()[]{}"&lt;/code&gt; -&amp;gt; &lt;strong&gt;True&lt;/strong&gt; (All pairs are distinct and valid)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s = "(]"&lt;/code&gt; -&amp;gt; &lt;strong&gt;False&lt;/strong&gt; (Opening parenthesis closed by a square bracket – wrong type!)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s = "([{}])"&lt;/code&gt; -&amp;gt; &lt;strong&gt;True&lt;/strong&gt; (Nested, but everything matches up beautifully)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s = "([)]"&lt;/code&gt; -&amp;gt; &lt;strong&gt;False&lt;/strong&gt; (Here, the &lt;code&gt;]&lt;/code&gt; closes &lt;code&gt;[&lt;/code&gt; before &lt;code&gt;}&lt;/code&gt; is closed. Incorrect order!)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The string length can be up to 10,000 characters. Ready to solve it?&lt;/p&gt;




&lt;h2&gt;
  
  
  The "Aha!" Moment (Intuition)
&lt;/h2&gt;

&lt;p&gt;When you see an opening bracket, what do you expect? You expect its &lt;em&gt;corresponding closing bracket&lt;/em&gt; to appear later. But here's the kicker: if you encounter &lt;em&gt;another&lt;/em&gt; opening bracket before the first one is closed, you expect that &lt;em&gt;new&lt;/em&gt; opening bracket to be closed &lt;em&gt;first&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This "last one in, first one out" behavior should immediately make you think of a &lt;strong&gt;Stack&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Imagine you're reading the string character by character:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  When you see an &lt;strong&gt;opening bracket&lt;/strong&gt; (&lt;code&gt;(&lt;/code&gt;, &lt;code&gt;{&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt;), you're essentially "expecting" its closure. You can push it onto a stack. It's waiting for its match.&lt;/li&gt;
&lt;li&gt;  When you see a &lt;strong&gt;closing bracket&lt;/strong&gt; (&lt;code&gt;)&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt;, &lt;code&gt;]&lt;/code&gt;), this is where the magic happens. You need to check if it matches the &lt;em&gt;most recently opened&lt;/em&gt; bracket that hasn't been closed yet. Where is that "most recently opened" bracket? Right at the &lt;code&gt;top&lt;/code&gt; of your stack!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the closing bracket matches the top of the stack, awesome! They cancel each other out, so you &lt;code&gt;pop&lt;/code&gt; the opening bracket from the stack. If they don't match, or if there's no opening bracket on the stack at all, then our string is invalid.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step-by-Step Logic (Approach)
&lt;/h2&gt;

&lt;p&gt;Let's break down the stack-based approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize an Empty Stack:&lt;/strong&gt; This stack will store all the &lt;code&gt;opening&lt;/code&gt; brackets we encounter that are still waiting for their match.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create a Mapping:&lt;/strong&gt; We need a quick way to know which opening bracket corresponds to which closing bracket. A dictionary (or hash map) is perfect for this! We'll map closing brackets to their respective opening brackets.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;')' : '('&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;'}' : '{'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;']' : '['&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Iterate Through the String:&lt;/strong&gt; Go through each character &lt;code&gt;char&lt;/code&gt; in the input string &lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*   **Case 1: `char` is a Closing Bracket** (e.g., `')'`, `'}'`, `']'`).
    *   First, check if our `stack` is empty. If it is, and we've found a closing bracket, it means there's no opening bracket to match it. This immediately makes the string `invalid`. Return `False`.
    *   If the stack is *not* empty, `pop` the top element from the stack. Let's call it `top_element`.
    *   Now, compare `top_element` with the *expected* opening bracket for the current `char`. We get this `expected` opening bracket from our mapping (e.g., `mapping[char]`).
    *   If `top_element` is *not equal* to `mapping[char]`, it means the brackets don't match (e.g., we popped a `(` but expected a `[`). The string is `invalid`. Return `False`.

*   **Case 2: `char` is an Opening Bracket** (e.g., `'('`, `'{'`, `'['`).
    *   This is simple! Just `append` (or `push`) this opening bracket onto our `stack`. It's now waiting for its corresponding closing bracket.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Final Check:&lt;/strong&gt; After iterating through the &lt;em&gt;entire&lt;/em&gt; string:

&lt;ul&gt;
&lt;li&gt;  If the &lt;code&gt;stack&lt;/code&gt; is &lt;code&gt;empty&lt;/code&gt;, it means every opening bracket found its correct match and was popped. The string is &lt;code&gt;valid&lt;/code&gt;. Return &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  If the &lt;code&gt;stack&lt;/code&gt; is &lt;em&gt;not empty&lt;/em&gt;, it means there are still some opening brackets left on the stack that never found a closing bracket. The string is &lt;code&gt;invalid&lt;/code&gt;. Return &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach systematically checks for matching types and correct order using the LIFO (Last-In, First-Out) property of a stack!&lt;/p&gt;




&lt;h2&gt;
  
  
  The Code
&lt;/h2&gt;

&lt;p&gt;Here's the Python implementation of our approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Initialize an empty stack to store opening brackets
&lt;/span&gt;        &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

        &lt;span class="c1"&gt;# Create a dictionary to map closing brackets to their corresponding opening brackets
&lt;/span&gt;        &lt;span class="c1"&gt;# This helps in quick lookups
&lt;/span&gt;        &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;# Iterate through each character in the input string
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# If the character is a closing bracket (i.e., it's a key in our mapping)
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;# Get the top element from the stack.
&lt;/span&gt;                &lt;span class="c1"&gt;# If the stack is empty, assign a dummy value like '#'
&lt;/span&gt;                &lt;span class="c1"&gt;# This prevents errors from popping an empty stack and allows easy comparison
&lt;/span&gt;                &lt;span class="n"&gt;top_element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

                &lt;span class="c1"&gt;# Check if the popped element matches the expected opening bracket
&lt;/span&gt;                &lt;span class="c1"&gt;# If they don't match, or if stack was empty (top_element was '#'), it's invalid
&lt;/span&gt;                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;top_element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
            &lt;span class="c1"&gt;# If the character is an opening bracket
&lt;/span&gt;            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;# Push it onto the stack, waiting for its closure
&lt;/span&gt;                &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# After iterating through the entire string, if the stack is empty,
&lt;/span&gt;        &lt;span class="c1"&gt;# all opening brackets have been correctly closed.
&lt;/span&gt;        &lt;span class="c1"&gt;# Otherwise, there are unclosed opening brackets.
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="c1"&gt;# 'not stack' returns True if stack is empty, False otherwise
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;Understanding how efficient our solution is crucial for competitive programming!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  We iterate through the input string &lt;code&gt;s&lt;/code&gt; exactly once. &lt;code&gt;N&lt;/code&gt; is the length of the string.&lt;/li&gt;
&lt;li&gt;  Inside the loop, operations like &lt;code&gt;char in mapping&lt;/code&gt;, &lt;code&gt;stack.pop()&lt;/code&gt;, &lt;code&gt;stack.append()&lt;/code&gt;, and dictionary lookups (&lt;code&gt;mapping[char]&lt;/code&gt;) all take constant time, O(1), on average.&lt;/li&gt;
&lt;li&gt;  Therefore, the total time complexity is directly proportional to the length of the string, making it O(N).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  In the worst-case scenario, the stack could potentially store all the opening brackets if they are all at the beginning of the string (e.g., &lt;code&gt;s = "((((((((("&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  In such a case, the stack could grow up to a size of &lt;code&gt;N/2&lt;/code&gt; (since at most half the string can be opening brackets if they were to eventually be valid).&lt;/li&gt;
&lt;li&gt;  Thus, the space required by the stack is proportional to the input string length, leading to a space complexity of O(N).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;This problem is a fantastic stepping stone! Here's what we learned:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Stacks are for Matching:&lt;/strong&gt; Whenever you need to check for matching pairs (parentheses, XML tags, function calls, etc.) that follow a "last-in, first-out" pattern, a stack is your go-to data structure.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;LIFO Principle:&lt;/strong&gt; The core idea behind stacks – Last In, First Out – is perfectly suited for handling nested structures where the most recently opened item must be the first to be closed.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Hash Maps for Quick Lookups:&lt;/strong&gt; Using a dictionary (or hash map) to store the mapping between opening and closing brackets makes our code clean and efficient (O(1) average time for lookups).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Edge Cases:&lt;/strong&gt; Always consider empty stacks (e.g., &lt;code&gt;)&lt;/code&gt; at the beginning) and non-empty stacks at the end (unmatched opening brackets).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Keep practicing, and you'll find these patterns appearing everywhere! Happy coding!&lt;/p&gt;




&lt;p&gt;Author Account: NavyaSree_14&lt;br&gt;&lt;br&gt;
Time Published: 2026-05-17 11:12:11&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 1. Two Sum</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sun, 17 May 2026 05:40:12 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-5d9f</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-5d9f</guid>
      <description>&lt;h1&gt;
  
  
  Two Sum: Your First Step into LeetCode Magic! ✨
&lt;/h1&gt;

&lt;p&gt;Hey there, future coding superstar! 👋 Welcome to your very first LeetCode adventure! If you're just starting your journey into algorithms and data structures, you've landed in the perfect place. We're going to tackle a legendary problem today: &lt;strong&gt;Two Sum&lt;/strong&gt;. It's often the first problem many programmers encounter, and it's a fantastic way to learn about efficiency and the power of data structures.&lt;/p&gt;

&lt;p&gt;Ready to unlock some coding magic? Let's dive in!&lt;/p&gt;




&lt;h2&gt;
  
  
  🧐 Problem Explanation: What are we trying to do?
&lt;/h2&gt;

&lt;p&gt;Imagine you have a list of numbers, like &lt;code&gt;[2, 7, 11, 15]&lt;/code&gt;. And you're given a specific &lt;code&gt;target&lt;/code&gt; number, say &lt;code&gt;9&lt;/code&gt;. Your mission, should you choose to accept it, is to find &lt;strong&gt;two different numbers&lt;/strong&gt; in that list that add up to your &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once you find them, you don't return the numbers themselves, but their &lt;strong&gt;positions (indices)&lt;/strong&gt; in the original list.&lt;/p&gt;

&lt;p&gt;Let's look at our example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;target = 9&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Can you spot the pair?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;2&lt;/code&gt; is at index &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;7&lt;/code&gt; is at index &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  And hey, &lt;code&gt;2 + 7 = 9&lt;/code&gt;! Bingo!&lt;/li&gt;
&lt;li&gt;  So, our answer would be &lt;code&gt;[0, 1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A few ground rules to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;One Solution, Guaranteed:&lt;/strong&gt; You'll always find exactly one pair that works. No need to worry about multiple answers or no answer at all.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;No Reusing:&lt;/strong&gt; You can't use the same number twice. If &lt;code&gt;nums = [3, 3]&lt;/code&gt; and &lt;code&gt;target = 6&lt;/code&gt;, you use the &lt;code&gt;3&lt;/code&gt; at index &lt;code&gt;0&lt;/code&gt; and the &lt;code&gt;3&lt;/code&gt; at index &lt;code&gt;1&lt;/code&gt; (they're distinct elements, even if their values are the same).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Order Doesn't Matter:&lt;/strong&gt; Returning &lt;code&gt;[0, 1]&lt;/code&gt; or &lt;code&gt;[1, 0]&lt;/code&gt; is perfectly fine.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🤔 Intuition: The "Aha!" Moment
&lt;/h2&gt;

&lt;p&gt;When you first hear this problem, your brain probably jumps to the most straightforward solution: "I'll just try every single combination of two numbers until I find the pair!"&lt;/p&gt;

&lt;p&gt;This is a totally natural and valid starting point! It's called the &lt;strong&gt;brute-force&lt;/strong&gt; approach. Think of it like rummaging through a messy drawer, picking one sock, and then trying to find its matching pair by looking at every other sock.&lt;/p&gt;

&lt;p&gt;But here's the thing about competitive programming and real-world coding: we often want the &lt;em&gt;fastest&lt;/em&gt; and &lt;em&gt;most efficient&lt;/em&gt; way to solve a problem. Is there a shortcut to finding that second sock? What if you had an ultra-organized sock drawer where you could instantly find any sock you needed?&lt;/p&gt;

&lt;p&gt;The "aha!" moment for Two Sum comes when you realize that instead of constantly re-scanning the list for the &lt;em&gt;second&lt;/em&gt; number, you could &lt;strong&gt;remember&lt;/strong&gt; the numbers you've already seen and their positions. This way, when you're looking for a &lt;code&gt;complement&lt;/code&gt; (the number that adds up to the target), you can "ask" your memory if you've already seen it.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Approach: Step-by-Step Logic
&lt;/h2&gt;

&lt;p&gt;Let's break down how we can solve this problem, starting with the simpler (but less efficient) way, then moving to a much smarter solution!&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Brute-Force (Nested Loops) Approach
&lt;/h3&gt;

&lt;p&gt;This is your first instinct, and it's essential to understand it before optimizing!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Start with the first number in the list (let's call it &lt;code&gt;num1&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; Then, take &lt;code&gt;num1&lt;/code&gt; and compare it with &lt;em&gt;every other number&lt;/em&gt; in the rest of the list (let's call these &lt;code&gt;num2&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; If &lt;code&gt;num1 + num2&lt;/code&gt; equals your &lt;code&gt;target&lt;/code&gt;, fantastic! You've found your pair. Return their indices.&lt;/li&gt;
&lt;li&gt; If not, keep trying different &lt;code&gt;num2&lt;/code&gt;s with the current &lt;code&gt;num1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; If you've checked all possible &lt;code&gt;num2&lt;/code&gt;s for the current &lt;code&gt;num1&lt;/code&gt;, move to the next &lt;code&gt;num1&lt;/code&gt; in the original list and repeat the whole process.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Dry Run Example: &lt;code&gt;nums = [2, 7, 11, 15], target = 9&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Outer loop (i = 0, num1 = 2):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Inner loop (j = 1, num2 = 7):&lt;/strong&gt; Is &lt;code&gt;2 + 7 == 9&lt;/code&gt;? YES! Return &lt;code&gt;[0, 1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This works perfectly, but as the list &lt;code&gt;nums&lt;/code&gt; gets bigger, this approach becomes very slow because of all the re-checking.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The Smarter Way: Using a Hash Map (Dictionary)
&lt;/h3&gt;

&lt;p&gt;This is where the magic happens! A &lt;strong&gt;Hash Map&lt;/strong&gt; (also known as a Dictionary in Python, HashMap in Java, or Hash Table) is a data structure that allows you to store &lt;code&gt;key: value&lt;/code&gt; pairs and look up a &lt;code&gt;key&lt;/code&gt; almost instantly! Think of it like a super-fast index for your numbers.&lt;/p&gt;

&lt;p&gt;Here's the optimized strategy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create an empty Hash Map. We'll use it to store numbers we've already seen, along with their indices. The format will be &lt;code&gt;{number: index}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Go through the &lt;code&gt;nums&lt;/code&gt; list one number at a time. For each number, let's call it &lt;code&gt;current_num&lt;/code&gt; and its index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Calculate the &lt;code&gt;complement&lt;/code&gt;:&lt;/strong&gt; This is the number we &lt;em&gt;need&lt;/em&gt; to find to hit our &lt;code&gt;target&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;complement = target - current_num&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Example:&lt;/em&gt; If &lt;code&gt;target = 9&lt;/code&gt; and &lt;code&gt;current_num = 2&lt;/code&gt;, we need &lt;code&gt;complement = 9 - 2 = 7&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Check your Hash Map:&lt;/strong&gt; Look if this &lt;code&gt;complement&lt;/code&gt; is already a &lt;code&gt;key&lt;/code&gt; in your Hash Map.

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;If it IS in the map:&lt;/strong&gt; Wow! You've found your pair! The &lt;code&gt;complement&lt;/code&gt; is one number, and your &lt;code&gt;current_num&lt;/code&gt; is the other.

&lt;ul&gt;
&lt;li&gt;  Return the index stored with the &lt;code&gt;complement&lt;/code&gt; (i.e., &lt;code&gt;hashmap[complement]&lt;/code&gt;) and your current index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;If it's NOT in the map:&lt;/strong&gt; No worries! It just means we haven't encountered its partner yet. Add your &lt;code&gt;current_num&lt;/code&gt; and its index &lt;code&gt;i&lt;/code&gt; to the Hash Map. This way, if a future number needs &lt;code&gt;current_num&lt;/code&gt; as its complement, it will be found instantly.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; Continue this process until you find the pair (which, remember, you're guaranteed to do!).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Dry Run Example: &lt;code&gt;nums = [2, 7, 11, 15], target = 9&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Current &lt;code&gt;i&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;Current &lt;code&gt;num&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;&lt;code&gt;complement = target - num&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Is &lt;code&gt;complement&lt;/code&gt; in &lt;code&gt;hashmap&lt;/code&gt;?&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;hashmap&lt;/code&gt; (after action)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;9 - 2 = 7&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Add &lt;code&gt;2:0&lt;/code&gt; to &lt;code&gt;hashmap&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2:0}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;7&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;9 - 7 = 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes! (&lt;code&gt;2&lt;/code&gt; is in &lt;code&gt;hashmap&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Return &lt;code&gt;[hashmap[2], 1]&lt;/code&gt; which is &lt;code&gt;[0, 1]&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And we found the answer much quicker!&lt;/p&gt;




&lt;h2&gt;
  
  
  💻 Code (Python)
&lt;/h2&gt;

&lt;p&gt;Here's the Python code implementing the Hash Map approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="c1"&gt;# Initialize an empty hash map (dictionary in Python)
&lt;/span&gt;        &lt;span class="c1"&gt;# This map will store: {number_value: its_index}
&lt;/span&gt;        &lt;span class="n"&gt;hashmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

        &lt;span class="c1"&gt;# Iterate through the list of numbers.
&lt;/span&gt;        &lt;span class="c1"&gt;# 'enumerate' gives us both the index (i) and the value (num) at each step.
&lt;/span&gt;        &lt;span class="k"&gt;for&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;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&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="c1"&gt;# Calculate the 'complement' needed.
&lt;/span&gt;            &lt;span class="c1"&gt;# If current 'num' is X and 'target' is T, we need T - X.
&lt;/span&gt;            &lt;span class="n"&gt;complement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;

            &lt;span class="c1"&gt;# Check if this 'complement' is already a key in our hashmap.
&lt;/span&gt;            &lt;span class="c1"&gt;# This is a very fast O(1) average time operation!
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;complement&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;# If found, we've got our pair!
&lt;/span&gt;                &lt;span class="c1"&gt;# Return the index of the complement (retrieved from the hashmap)
&lt;/span&gt;                &lt;span class="c1"&gt;# and the index of the current number (i).
&lt;/span&gt;                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;complement&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;# If the complement isn't found, it means the current 'num'
&lt;/span&gt;            &lt;span class="c1"&gt;# hasn't found its partner yet. So, we add 'num' and its index 'i'
&lt;/span&gt;            &lt;span class="c1"&gt;# to the hashmap. This makes it available for future numbers
&lt;/span&gt;            &lt;span class="c1"&gt;# that might need 'num' as their complement.
&lt;/span&gt;            &lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&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="c1"&gt;# The problem guarantees exactly one solution, so this part of the code
&lt;/span&gt;        &lt;span class="c1"&gt;# should technically never be reached. However, in other scenarios,
&lt;/span&gt;        &lt;span class="c1"&gt;# you might raise an error or return an empty list if no solution is found.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⏱️ Time &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;Understanding how efficient your code is is a crucial skill!&lt;/p&gt;

&lt;h3&gt;
  
  
  Brute-Force Approach (Nested Loops)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Time Complexity: O(n^2)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  We have two loops, one nested inside the other. If the list has &lt;code&gt;n&lt;/code&gt; elements, the outer loop runs &lt;code&gt;n&lt;/code&gt; times, and the inner loop runs up to &lt;code&gt;n-1&lt;/code&gt; times for each outer loop iteration. This results in approximately &lt;code&gt;n * n = n^2&lt;/code&gt; operations. As &lt;code&gt;n&lt;/code&gt; grows, &lt;code&gt;n^2&lt;/code&gt; grows &lt;em&gt;very&lt;/em&gt; quickly, making it inefficient for large lists.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Space Complexity: O(1)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  We're not using any additional data structures that scale with the input size, just a few variables.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Hash Map Approach
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Time Complexity: O(n)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  We iterate through the list only &lt;strong&gt;once&lt;/strong&gt;. For each number, we perform a constant number of operations: calculate the complement, check if it's in the hash map, and potentially add the current number to the hash map.&lt;/li&gt;
&lt;li&gt;  Looking up an item in a hash map and inserting an item into it both take, on average, &lt;code&gt;O(1)&lt;/code&gt; (constant) time.&lt;/li&gt;
&lt;li&gt;  Therefore, &lt;code&gt;n&lt;/code&gt; operations, each taking &lt;code&gt;O(1)&lt;/code&gt; time, results in an overall &lt;code&gt;O(n)&lt;/code&gt; time complexity. This is significantly faster than &lt;code&gt;O(n^2)&lt;/code&gt; for larger inputs!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Space Complexity: O(n)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  In the worst case, we might have to add almost all &lt;code&gt;n&lt;/code&gt; numbers and their indices to our hash map before finding the desired pair. This means the space used by the hash map grows proportionally with the number of elements in the input list.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🌟 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Start Simple:&lt;/strong&gt; Don't be afraid to think of the brute-force solution first. It helps you understand the problem thoroughly.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Hash Maps are Powerful:&lt;/strong&gt; For problems that involve quick lookups, checking for existence, or mapping values to other values, Hash Maps (dictionaries) are incredibly efficient and often the key to optimizing your solution.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Think in Complements:&lt;/strong&gt; Many "find a pair/sum" problems can be broken down into calculating what &lt;code&gt;complement&lt;/code&gt; you need and then efficiently searching for it.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Time-Space Trade-off:&lt;/strong&gt; We improved our time complexity from &lt;code&gt;O(n^2)&lt;/code&gt; to &lt;code&gt;O(n)&lt;/code&gt; by using more space (&lt;code&gt;O(n)&lt;/code&gt; for the hash map). This is a very common and powerful trade-off in algorithm design!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congratulations! You've just conquered your first LeetCode problem with an optimized solution. This foundational problem teaches you concepts that you'll apply to many more challenges down the road. Keep practicing, and happy coding!&lt;/p&gt;




&lt;p&gt;Author: NavyaSree_14&lt;br&gt;
Published: 2026-05-17 11:09:40&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 1. Two Sum</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sat, 16 May 2026 15:24:46 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-5eo3</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-5eo3</guid>
      <description>&lt;h1&gt;
  
  
  Conquering Your First LeetCode Challenge: Two Sum Demystified!
&lt;/h1&gt;

&lt;p&gt;Hey there, aspiring coder! 👋 Ever heard of LeetCode? It's a fantastic platform to sharpen your problem-solving skills, and we're about to tackle one of its most famous (and foundational!) problems: &lt;strong&gt;Two Sum&lt;/strong&gt;. Don't let the number "1" fool you; while it's often the first problem people encounter, it's packed with key concepts that will serve you well in your coding journey.&lt;/p&gt;

&lt;p&gt;Let's dive in and unravel this classic together!&lt;/p&gt;




&lt;h3&gt;
  
  
  The Problem: Finding Our Pair
&lt;/h3&gt;

&lt;p&gt;Imagine you have a list of numbers, like &lt;code&gt;[2, 7, 11, 15]&lt;/code&gt;, and a specific &lt;code&gt;target&lt;/code&gt; number, say &lt;code&gt;9&lt;/code&gt;. Your mission, should you choose to accept it, is to find &lt;em&gt;two different numbers&lt;/em&gt; in that list that add up to the &lt;code&gt;target&lt;/code&gt;. Once you find them, you need to tell us their positions (their "indices") in the original list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here's the official breakdown:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Given:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  An array of integers &lt;code&gt;nums&lt;/code&gt; (e.g., &lt;code&gt;[2, 7, 11, 15]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;  An integer &lt;code&gt;target&lt;/code&gt; (e.g., &lt;code&gt;9&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Return the &lt;code&gt;indices&lt;/code&gt; of the two numbers in &lt;code&gt;nums&lt;/code&gt; that sum up to &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Assumptions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  There will always be exactly one pair that solves the problem.&lt;/li&gt;
&lt;li&gt;  You cannot use the same number twice (meaning, the two numbers must come from different positions in the array).&lt;/li&gt;
&lt;li&gt;  The order of the returned indices doesn't matter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's look at an example to make it crystal clear:&lt;/strong&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;target = 9&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thinking Process:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; We look at &lt;code&gt;2&lt;/code&gt;. What number do we need to add to &lt;code&gt;2&lt;/code&gt; to get &lt;code&gt;9&lt;/code&gt;? We need &lt;code&gt;7&lt;/code&gt; (&lt;code&gt;9 - 2 = 7&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; Is &lt;code&gt;7&lt;/code&gt; in our &lt;code&gt;nums&lt;/code&gt; array? Yes, it is!&lt;/li&gt;
&lt;li&gt; The index of &lt;code&gt;2&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; The index of &lt;code&gt;7&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Since &lt;code&gt;nums[0] + nums[1] == 9&lt;/code&gt;, we found our pair!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[0, 1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;See? Not so scary, right?&lt;/p&gt;




&lt;h3&gt;
  
  
  The "Aha!" Moment: Beyond Brute Force
&lt;/h3&gt;

&lt;p&gt;When you first encounter a problem like this, a common initial thought (and a totally valid one!) is to try every single combination. You might think:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  "Let's take the first number (&lt;code&gt;2&lt;/code&gt;).

&lt;ul&gt;
&lt;li&gt;  Add it to the second (&lt;code&gt;7&lt;/code&gt;): &lt;code&gt;2 + 7 = 9&lt;/code&gt;. Bingo!&lt;/li&gt;
&lt;li&gt;  (If not found, try &lt;code&gt;2 + 11&lt;/code&gt;, &lt;code&gt;2 + 15&lt;/code&gt;, etc.)"&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  "Then take the second number (&lt;code&gt;7&lt;/code&gt;).

&lt;ul&gt;
&lt;li&gt;  Add it to the third (&lt;code&gt;11&lt;/code&gt;): &lt;code&gt;7 + 11 = 18&lt;/code&gt;. No.&lt;/li&gt;
&lt;li&gt;  (And so on...)"&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This approach, known as &lt;strong&gt;brute force&lt;/strong&gt;, involves using nested loops to check every possible pair. It works, but for very large lists, it can be &lt;em&gt;slow&lt;/em&gt;. Imagine a list with 10,000 numbers! You'd be doing tens of millions of checks! 😵&lt;/p&gt;

&lt;p&gt;So, the "aha!" moment comes when you realize: &lt;strong&gt;instead of &lt;em&gt;searching&lt;/em&gt; for the &lt;code&gt;other_number&lt;/code&gt;, what if we could &lt;em&gt;instantly know&lt;/em&gt; if it exists and where it is?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where a super-powered data structure comes into play: the &lt;strong&gt;Hash Map&lt;/strong&gt; (or Dictionary in Python, HashMap in Java/JavaScript, etc.).&lt;/p&gt;

&lt;p&gt;If we know we need &lt;code&gt;complement = target - current_number&lt;/code&gt;, and we want to find &lt;code&gt;complement&lt;/code&gt; &lt;em&gt;fast&lt;/em&gt;, a hash map is our best friend. It lets us store values (like numbers from our list) and retrieve their associated information (like their index) almost instantly!&lt;/p&gt;




&lt;h3&gt;
  
  
  Our Smart Approach: The Hash Map Helper
&lt;/h3&gt;

&lt;p&gt;Here's how we can use a hash map to solve Two Sum efficiently:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Build a Map of Numbers to Their Indices&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, we'll iterate through our &lt;code&gt;nums&lt;/code&gt; array &lt;em&gt;once&lt;/em&gt;. As we go, we'll build a hash map where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Keys&lt;/strong&gt; are the numbers from &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Values&lt;/strong&gt; are their corresponding indices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why do this? Because it gives us a quick way to ask, "Hey, does the number &lt;code&gt;X&lt;/code&gt; exist in &lt;code&gt;nums&lt;/code&gt;, and if so, what's its index?"&lt;/p&gt;

&lt;p&gt;Let's use &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt; and &lt;code&gt;target = 9&lt;/code&gt; again:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Iteration&lt;/th&gt;
&lt;th&gt;&lt;code&gt;nums[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;hashmap&lt;/code&gt; after adding &lt;code&gt;nums[i]: i&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2: 0}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2: 0, 7: 1}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2: 0, 7: 1, 11: 2}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2: 0, 7: 1, 11: 2, 15: 3}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Find the Complement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that our hash map is fully built, we iterate through &lt;code&gt;nums&lt;/code&gt; &lt;em&gt;again&lt;/em&gt;. For each number &lt;code&gt;nums[i]&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Calculate the &lt;code&gt;complement&lt;/code&gt;&lt;/strong&gt;: This is the number we &lt;em&gt;need&lt;/em&gt; to find to reach our &lt;code&gt;target&lt;/code&gt;. &lt;code&gt;complement = target - nums[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Check the hash map&lt;/strong&gt;: Does our &lt;code&gt;complement&lt;/code&gt; exist as a key in the &lt;code&gt;hashmap&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Crucial Check&lt;/strong&gt;: If it &lt;em&gt;does&lt;/em&gt; exist, we also need to make sure that the index stored for the &lt;code&gt;complement&lt;/code&gt; (&lt;code&gt;hashmap[complement]&lt;/code&gt;) is &lt;em&gt;not the same&lt;/em&gt; as our current index &lt;code&gt;i&lt;/code&gt;. Remember, we can't use the same element twice!&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Found It!&lt;/strong&gt;: If both conditions are true, we've found our pair! We return &lt;code&gt;[i, hashmap[complement]]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's trace this step with our example: &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;, &lt;code&gt;target = 9&lt;/code&gt;, &lt;code&gt;hashmap = {2: 0, 7: 1, 11: 2, 15: 3}&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;nums[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;complement = target - nums[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;complement&lt;/code&gt; in &lt;code&gt;hashmap&lt;/code&gt;?&lt;/th&gt;
&lt;th&gt;&lt;code&gt;hashmap[complement]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;hashmap[complement] != i&lt;/code&gt;?&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;9 - 2 = 7&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;1 != 0&lt;/code&gt; (True)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Return &lt;code&gt;[0, 1]&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And just like that, we found our solution efficiently!&lt;/p&gt;




&lt;h3&gt;
  
  
  The Code: Python Magic ✨
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;List&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="c1"&gt;# Step 1: Build the hash map
&lt;/span&gt;        &lt;span class="c1"&gt;# Stores {number: index}
&lt;/span&gt;        &lt;span class="n"&gt;hashmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; 
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&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;hashmap&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;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;i&lt;/span&gt;

        &lt;span class="c1"&gt;# Step 2: Iterate again to find the complement
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&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;complement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&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;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

            &lt;span class="c1"&gt;# Check if the complement exists in our hashmap
&lt;/span&gt;            &lt;span class="c1"&gt;# AND make sure its index isn't the same as our current 'i'
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;complement&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hashmap&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;complement&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;return&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;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;complement&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

        &lt;span class="c1"&gt;# This line should technically not be reached given the problem constraints
&lt;/span&gt;        &lt;span class="c1"&gt;# ("exactly one solution exists"), but it's good practice for robust code.
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

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

&lt;/div&gt;






&lt;h3&gt;
  
  
  The Nitty-Gritty: Time &amp;amp; Space Complexity
&lt;/h3&gt;

&lt;p&gt;Understanding how efficient your code is (or isn't!) is a fundamental skill. We measure this using &lt;strong&gt;Time Complexity&lt;/strong&gt; (how execution time grows with input size) and &lt;strong&gt;Space Complexity&lt;/strong&gt; (how memory usage grows).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity: O(n)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Explanation:&lt;/strong&gt; We iterate through the &lt;code&gt;nums&lt;/code&gt; array twice.

&lt;ul&gt;
&lt;li&gt;  The first loop builds the hash map, doing &lt;code&gt;n&lt;/code&gt; operations (each insertion into a hash map takes, on average, O(1) time). So, O(n) for the first loop.&lt;/li&gt;
&lt;li&gt;  The second loop performs &lt;code&gt;n&lt;/code&gt; lookups (each &lt;code&gt;complement in hashmap&lt;/code&gt; and &lt;code&gt;hashmap[complement]&lt;/code&gt; takes, on average, O(1) time). So, O(n) for the second loop.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  Adding these up, O(n) + O(n) simplifies to &lt;strong&gt;O(n)&lt;/strong&gt;. This is considered very efficient!&lt;/li&gt;

&lt;li&gt;  &lt;em&gt;(Remember the follow-up question? "Can you come up with an algorithm that is less than O(n^2) time complexity?" We did it! Brute force would have been O(n^2).)&lt;/em&gt;
&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity: O(n)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Explanation:&lt;/strong&gt; We create a hash map to store elements from the &lt;code&gt;nums&lt;/code&gt; array. In the worst case (all numbers are unique), the hash map will store &lt;code&gt;n&lt;/code&gt; key-value pairs. Therefore, the space required grows linearly with the input size &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Key Takeaways for Your Coding Journey
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Hash Maps are Powerful:&lt;/strong&gt; They excel at fast lookups. Whenever you need to quickly check if an item exists or retrieve its associated data, think hash map!&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The "Complement" Idea:&lt;/strong&gt; Many problems can be reframed by thinking, "What &lt;code&gt;X&lt;/code&gt; do I need to reach &lt;code&gt;Y&lt;/code&gt;?" (i.e., &lt;code&gt;X = Y - current_item&lt;/code&gt;). This is a common pattern in competitive programming.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Optimize Beyond Brute Force:&lt;/strong&gt; While brute force is a great starting point to understand a problem, always challenge yourself to find more efficient solutions, especially when dealing with larger inputs. &lt;code&gt;O(n)&lt;/code&gt; is often a significant upgrade from &lt;code&gt;O(n^2)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Understand Constraints:&lt;/strong&gt; The problem statement's constraints and assumptions (like "exactly one solution" and "cannot use the same element twice") are vital. They guide your solution and help you avoid unnecessary checks or edge cases.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;And there you have it! You've successfully navigated LeetCode's Problem #1, "Two Sum," armed with the power of hash maps. This problem is a gateway to many more exciting algorithmic challenges. Keep practicing, keep learning, and happy coding!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Author: p1Hzd8mRM8&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Published: 2026-05-16 20:53:55&lt;/em&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 1. Two Sum</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sat, 16 May 2026 11:08:22 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-ikg</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-ikg</guid>
      <description>&lt;h1&gt;
  
  
  Your First LeetCode Journey: Conquering the Classic Two Sum Problem!
&lt;/h1&gt;

&lt;p&gt;Hey everyone! 👋 NavyaSree_14 here, excited to kick off our coding adventure with LeetCode's most famous warm-up challenge: &lt;strong&gt;Two Sum&lt;/strong&gt;. If you've just started your DSA journey, this problem is a fantastic stepping stone. It's simple to understand, yet introduces a powerful optimization technique that's vital for competitive programming and interviews.&lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Explanation
&lt;/h2&gt;

&lt;p&gt;Imagine you're at a treasure hunt. You have a list of numbers, let's call it &lt;code&gt;nums&lt;/code&gt;, and a specific "target" number you're looking for. Your mission? Find &lt;strong&gt;two different numbers&lt;/strong&gt; in &lt;code&gt;nums&lt;/code&gt; that add up exactly to &lt;code&gt;target&lt;/code&gt;. Once you find them, you need to tell us their positions (their "indices") in the original list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  There will always be &lt;em&gt;exactly one&lt;/em&gt; pair of numbers that sums to the target. No need to worry about multiple answers or no answer!&lt;/li&gt;
&lt;li&gt;  You can't use the same number twice. If &lt;code&gt;nums[0]&lt;/code&gt; is part of the pair, you can't use &lt;code&gt;nums[0]&lt;/code&gt; again as the second number.&lt;/li&gt;
&lt;li&gt;  The order of the indices you return doesn't matter. &lt;code&gt;[0, 1]&lt;/code&gt; is the same as &lt;code&gt;[1, 0]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at some examples:&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;target = 9&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[0, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Why?&lt;/strong&gt; Because &lt;code&gt;nums[0]&lt;/code&gt; (which is &lt;code&gt;2&lt;/code&gt;) + &lt;code&gt;nums[1]&lt;/code&gt; (which is &lt;code&gt;7&lt;/code&gt;) = &lt;code&gt;9&lt;/code&gt;. We return their indices: &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums = [3, 2, 4]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;target = 6&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[1, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Why?&lt;/strong&gt; &lt;code&gt;nums[1]&lt;/code&gt; (which is &lt;code&gt;2&lt;/code&gt;) + &lt;code&gt;nums[2]&lt;/code&gt; (which is &lt;code&gt;4&lt;/code&gt;) = &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See? Simple enough!&lt;/p&gt;

&lt;h2&gt;
  
  
  Intuition: The "Aha!" Moment 💡
&lt;/h2&gt;

&lt;p&gt;How would you solve this manually? You'd probably pick a number, say &lt;code&gt;2&lt;/code&gt;, and then look through the rest of the list for &lt;code&gt;7&lt;/code&gt; (because &lt;code&gt;9 - 2 = 7&lt;/code&gt;). If you find &lt;code&gt;7&lt;/code&gt;, great! If not, you move to the next number, say &lt;code&gt;7&lt;/code&gt;, and look for &lt;code&gt;2&lt;/code&gt; (&lt;code&gt;9 - 7 = 2&lt;/code&gt;), and so on.&lt;/p&gt;

&lt;p&gt;This "pick one, then search the rest" approach works, but it can be slow if the list is very long. For every number you pick, you might have to scan almost the entire remaining list. This is what we call a &lt;strong&gt;brute-force&lt;/strong&gt; approach, and its time complexity is O(n²), which isn't ideal for large inputs.&lt;/p&gt;

&lt;p&gt;The "aha!" moment comes when you realize: &lt;strong&gt;Can I avoid repeatedly scanning the list?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of &lt;em&gt;searching&lt;/em&gt; for the &lt;code&gt;complement&lt;/code&gt; (the &lt;code&gt;target - current_num&lt;/code&gt;) in the &lt;em&gt;rest&lt;/em&gt; of the array, what if I could instantly &lt;em&gt;know&lt;/em&gt; if I've seen that &lt;code&gt;complement&lt;/code&gt; before, and if so, what its index was?&lt;/p&gt;

&lt;p&gt;This is where hashmaps (or dictionaries in Python) shine! A hashmap lets you store key-value pairs and quickly check if a key exists, and retrieve its value, usually in O(1) (constant) time on average.&lt;/p&gt;

&lt;p&gt;So, the new intuition is: &lt;strong&gt;As I iterate through the numbers, for each number, I calculate what its "partner" (complement) would need to be. Then, I quickly check if I've already encountered that partner in my journey so far.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach: Step-by-Step Logic
&lt;/h2&gt;

&lt;p&gt;We'll use a single pass through the array, leveraging a hashmap (dictionary in Python) to keep track of numbers we've already processed and their indices.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize an empty hashmap:&lt;/strong&gt; Let's call it &lt;code&gt;num_map&lt;/code&gt;. This map will store numbers as keys and their indices as values. &lt;code&gt;hashmap = {number: index}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Iterate through the &lt;code&gt;nums&lt;/code&gt; array:&lt;/strong&gt; We need both the index (&lt;code&gt;i&lt;/code&gt;) and the value (&lt;code&gt;num&lt;/code&gt;) of each element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;For each &lt;code&gt;num&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Calculate the &lt;code&gt;complement&lt;/code&gt;:&lt;/strong&gt; This is the value we need to find to sum up to &lt;code&gt;target&lt;/code&gt;. So, &lt;code&gt;complement = target - num&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Check if the &lt;code&gt;complement&lt;/code&gt; is already in our &lt;code&gt;num_map&lt;/code&gt;:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;If YES:&lt;/strong&gt; Fantastic! We've found our pair! The &lt;code&gt;complement&lt;/code&gt; was encountered earlier, and its index is &lt;code&gt;num_map[complement]&lt;/code&gt;. The current number's index is &lt;code&gt;i&lt;/code&gt;. We return &lt;code&gt;[num_map[complement], i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;If NO:&lt;/strong&gt; The &lt;code&gt;complement&lt;/code&gt; isn't in our &lt;code&gt;num_map&lt;/code&gt; yet. This means the current &lt;code&gt;num&lt;/code&gt; doesn't form a pair with any number we've seen &lt;em&gt;so far&lt;/em&gt;. So, we add the current &lt;code&gt;num&lt;/code&gt; and its index &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;num_map&lt;/code&gt;. This way, &lt;code&gt;num&lt;/code&gt; can act as a &lt;code&gt;complement&lt;/code&gt; for a future number we encounter. &lt;code&gt;num_map[num] = i&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why does this work?&lt;/strong&gt; Because we are guaranteed exactly one solution. Once we find the pair, we return immediately.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's trace Example 1: &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;, &lt;code&gt;target = 9&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Iteration&lt;/th&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;num&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;complement&lt;/code&gt; (&lt;code&gt;target - num&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;num_map&lt;/code&gt; (before check)&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;complement&lt;/code&gt; in &lt;code&gt;num_map&lt;/code&gt;?&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;num_map&lt;/code&gt; (after action)&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Add &lt;code&gt;2:0&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2:0}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2:0}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Return &lt;code&gt;[num_map[2], 1]&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[0,1]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Boom! We found it in just two steps.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;List&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="c1"&gt;# Initialize an empty hashmap (dictionary in Python)
&lt;/span&gt;        &lt;span class="c1"&gt;# to store numbers we've seen and their indices.
&lt;/span&gt;        &lt;span class="c1"&gt;# Format: {number: index}
&lt;/span&gt;        &lt;span class="n"&gt;hashmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

        &lt;span class="c1"&gt;# Iterate through the input list 'nums' using enumerate
&lt;/span&gt;        &lt;span class="c1"&gt;# to get both the index 'i' and the value 'num'
&lt;/span&gt;        &lt;span class="k"&gt;for&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;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&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="c1"&gt;# Calculate the 'complement' needed to reach the target
&lt;/span&gt;            &lt;span class="c1"&gt;# complement = target - current_num
&lt;/span&gt;            &lt;span class="n"&gt;complement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;

            &lt;span class="c1"&gt;# Check if this 'complement' already exists in our hashmap.
&lt;/span&gt;            &lt;span class="c1"&gt;# If it does, it means we've seen its partner before!
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;complement&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;# We found the two numbers!
&lt;/span&gt;                &lt;span class="c1"&gt;# The index of the 'complement' is stored in hashmap[complement]
&lt;/span&gt;                &lt;span class="c1"&gt;# The index of the current 'num' is 'i'
&lt;/span&gt;                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;complement&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;# If the complement is NOT in the hashmap,
&lt;/span&gt;            &lt;span class="c1"&gt;# it means the current 'num' doesn't form a pair with any
&lt;/span&gt;            &lt;span class="c1"&gt;# number encountered *so far*.
&lt;/span&gt;            &lt;span class="c1"&gt;# So, we add the current 'num' and its index to the hashmap.
&lt;/span&gt;            &lt;span class="c1"&gt;# This makes it available as a 'complement' for future numbers.
&lt;/span&gt;            &lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&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="c1"&gt;# The problem guarantees that exactly one solution exists,
&lt;/span&gt;        &lt;span class="c1"&gt;# so this line will theoretically never be reached.
&lt;/span&gt;        &lt;span class="c1"&gt;# However, for completeness in a real-world scenario, you might
&lt;/span&gt;        &lt;span class="c1"&gt;# raise an error or return an empty list if no solution was found.
&lt;/span&gt;        &lt;span class="c1"&gt;# return []
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Time &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;Understanding how efficient your solution is, is crucial!&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Complexity: O(n)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  We iterate through the &lt;code&gt;nums&lt;/code&gt; list exactly once.&lt;/li&gt;
&lt;li&gt;  Inside the loop, operations like calculating the &lt;code&gt;complement&lt;/code&gt;, checking if an element is &lt;code&gt;in&lt;/code&gt; the &lt;code&gt;hashmap&lt;/code&gt;, and adding an element to the &lt;code&gt;hashmap&lt;/code&gt; (&lt;code&gt;hashmap[num] = i&lt;/code&gt;) all take, on average, &lt;strong&gt;O(1)&lt;/strong&gt; (constant) time.&lt;/li&gt;
&lt;li&gt;  Since we perform these O(1) operations &lt;code&gt;n&lt;/code&gt; times (where &lt;code&gt;n&lt;/code&gt; is the length of &lt;code&gt;nums&lt;/code&gt;), the total time complexity is &lt;code&gt;n * O(1)&lt;/code&gt;, which simplifies to &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  This is a significant improvement over the brute-force &lt;code&gt;O(n^2)&lt;/code&gt; approach!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Space Complexity: O(n)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  In the worst-case scenario, we might have to store all &lt;code&gt;n&lt;/code&gt; elements from the &lt;code&gt;nums&lt;/code&gt; array into our &lt;code&gt;hashmap&lt;/code&gt;. This happens if the target pair is at the very end of the array, or if the &lt;code&gt;complement&lt;/code&gt; is always found after the current &lt;code&gt;num&lt;/code&gt; is added.&lt;/li&gt;
&lt;li&gt;  Therefore, the space required by the &lt;code&gt;hashmap&lt;/code&gt; can grow up to &lt;code&gt;n&lt;/code&gt; elements, making the space complexity &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a classic &lt;strong&gt;time-space trade-off&lt;/strong&gt;: we use extra space (the hashmap) to achieve a much faster time complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Hashmaps are your friends!&lt;/strong&gt; They are incredibly powerful data structures for quick lookups and can often transform &lt;code&gt;O(n^2)&lt;/code&gt; problems into &lt;code&gt;O(n)&lt;/code&gt; problems.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Think about complements:&lt;/strong&gt; Many "find a pair" or "find a sum" problems can be reframed as finding a &lt;code&gt;complement&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;One pass is often better than two:&lt;/strong&gt; If you can achieve the result in a single iteration by storing necessary information, it's usually more efficient.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;LeetCode #1 is foundational:&lt;/strong&gt; Master this concept, and you're well on your way to tackling more complex problems!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Keep practicing, and don't be afraid to experiment with different approaches. Happy coding!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Author Account: NavyaSree_14 | Published: 2026-05-16 16:37:57&lt;/em&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 20. Valid Parentheses</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sat, 16 May 2026 10:55:25 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-20-valid-parentheses-350j</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-20-valid-parentheses-350j</guid>
      <description>&lt;h1&gt;
  
  
  The Great Parentheses Puzzle: Cracking LeetCode 20 with Stacks!
&lt;/h1&gt;

&lt;p&gt;Hey there, future coding superstar! 👋 NavyaSree_14 here, and today we're diving into a classic LeetCode problem that might seem simple on the surface but teaches us a fundamental data structure concept: &lt;strong&gt;Valid Parentheses (Problem 20)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Don't let the "LeetCode" tag intimidate you! We're going to break this down step-by-step, making it as clear as possible. Think of it as a fun puzzle that will level up your problem-solving skills. Ready to unravel some brackets? Let's go!&lt;/p&gt;




&lt;h2&gt;
  
  
  What's the Puzzle? (Problem Explanation)
&lt;/h2&gt;

&lt;p&gt;Imagine you're writing a math equation or some code. You use parentheses like &lt;code&gt;()&lt;/code&gt;, curly braces &lt;code&gt;{}&lt;/code&gt; and square brackets &lt;code&gt;[]&lt;/code&gt; all the time. But what makes them "correctly" used?&lt;/p&gt;

&lt;p&gt;That's exactly what this problem asks us to determine! We're given a string &lt;code&gt;s&lt;/code&gt; that contains &lt;em&gt;only&lt;/em&gt; these six characters: &lt;code&gt;(&lt;/code&gt;, &lt;code&gt;)&lt;/code&gt;, &lt;code&gt;{&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt;, &lt;code&gt;]&lt;/code&gt;. Our mission is to figure out if the arrangement of these brackets is &lt;strong&gt;valid&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, what makes a string of brackets valid? Three simple rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Matching Pairs:&lt;/strong&gt; Every open bracket (&lt;code&gt;(&lt;/code&gt;, &lt;code&gt;{&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt;) must be closed by the &lt;em&gt;same type&lt;/em&gt; of bracket (so &lt;code&gt;(&lt;/code&gt; needs &lt;code&gt;)&lt;/code&gt;, not &lt;code&gt;]&lt;/code&gt; or &lt;code&gt;}&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Correct Order:&lt;/strong&gt; Open brackets must be closed in the correct sequence. Think of it like peeling an onion: the last bracket opened must be the first one closed. &lt;code&gt;([{}])&lt;/code&gt; is valid, but &lt;code&gt;([)]&lt;/code&gt; is not.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Leftovers:&lt;/strong&gt; Every close bracket &lt;em&gt;must&lt;/em&gt; have a corresponding open bracket. You can't just have &lt;code&gt;))&lt;/code&gt; without an opening &lt;code&gt;((&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at a few quick examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;"()"&lt;/code&gt; -&amp;gt; &lt;strong&gt;True&lt;/strong&gt; (Simple and perfectly matched)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;"()[]{}"&lt;/code&gt; -&amp;gt; &lt;strong&gt;True&lt;/strong&gt; (Multiple pairs, all valid)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;"(]"&lt;/code&gt; -&amp;gt; &lt;strong&gt;False&lt;/strong&gt; (An open parenthesis closed by a square bracket – wrong type!)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;"([)]"&lt;/code&gt; -&amp;gt; &lt;strong&gt;False&lt;/strong&gt; (Open parenthesis, then open square, but then square is closed &lt;em&gt;before&lt;/em&gt; parenthesis. Incorrect order!)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;"{[]}"&lt;/code&gt; -&amp;gt; &lt;strong&gt;True&lt;/strong&gt; (Looks complex, but &lt;code&gt;[]&lt;/code&gt; is inside &lt;code&gt;{}&lt;/code&gt;, all matched correctly!)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;"["&lt;/code&gt; -&amp;gt; &lt;strong&gt;False&lt;/strong&gt; (An open bracket without a closing one!)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The "Aha!" Moment (Intuition)
&lt;/h2&gt;

&lt;p&gt;When you read a sequence of brackets like &lt;code&gt;"{[()]}"&lt;/code&gt;, how do you intuitively know it's valid?&lt;/p&gt;

&lt;p&gt;You probably keep track of what's "open" and what you're "expecting" to close.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  You see &lt;code&gt;{&lt;/code&gt;, you think "Okay, I need a &lt;code&gt;}&lt;/code&gt; eventually."&lt;/li&gt;
&lt;li&gt;  Then &lt;code&gt;[&lt;/code&gt;, you think "Now I also need a &lt;code&gt;]&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; that &lt;code&gt;}&lt;/code&gt;."&lt;/li&gt;
&lt;li&gt;  Then &lt;code&gt;(&lt;/code&gt;, you think "And a &lt;code&gt;)&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; the &lt;code&gt;]&lt;/code&gt;."&lt;/li&gt;
&lt;li&gt;  When you see &lt;code&gt;)&lt;/code&gt;, you check: "Is the last thing I opened a &lt;code&gt;(&lt;/code&gt;? Yes! Great, match found. Forget about &lt;code&gt;(&lt;/code&gt;. Now I'm back to needing a &lt;code&gt;]&lt;/code&gt;."&lt;/li&gt;
&lt;li&gt;  Then &lt;code&gt;]&lt;/code&gt;, you check: "Is the last thing I opened a &lt;code&gt;[&lt;/code&gt;? Yes! Perfect. Forget about &lt;code&gt;[&lt;/code&gt;. Now I'm back to needing a &lt;code&gt;}&lt;/code&gt;."&lt;/li&gt;
&lt;li&gt;  Finally &lt;code&gt;}&lt;/code&gt;, you check: "Is the last thing I opened a &lt;code&gt;{&lt;/code&gt;? Yes! Awesome. Forget about &lt;code&gt;{&lt;/code&gt;. Nothing left."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This pattern of "remembering the last opened item and checking if the current closed item matches it" is a classic use case for a &lt;strong&gt;Stack&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;A stack is like a pile of plates: you add new plates to the top (push), and when you want to take one, you always take the one from the top (pop). It's &lt;strong&gt;LIFO (Last-In, First-Out)&lt;/strong&gt;. This is exactly what we need for brackets: the &lt;em&gt;last&lt;/em&gt; bracket we opened is the &lt;em&gt;first&lt;/em&gt; one we expect to close.&lt;/p&gt;




&lt;h2&gt;
  
  
  Our Game Plan (Approach)
&lt;/h2&gt;

&lt;p&gt;We'll use a stack to keep track of all the &lt;em&gt;open&lt;/em&gt; brackets we encounter.&lt;/p&gt;

&lt;p&gt;Here's the step-by-step breakdown:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Initialize a Stack:&lt;/strong&gt; Create an empty list (which will act as our stack in Python).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Create a Mapping:&lt;/strong&gt; We need a quick way to know which opening bracket corresponds to which closing bracket. A dictionary (hash map) is perfect for this! We'll map closing brackets to their opening counterparts: &lt;code&gt;')' : '('&lt;/code&gt;, &lt;code&gt;'}' : '{'&lt;/code&gt;, &lt;code&gt;']' : '['&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Iterate Through the String:&lt;/strong&gt; Go through each character in the input string &lt;code&gt;s&lt;/code&gt; one by one.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*   **If it's an Opening Bracket (`(`, `{`, `[`):**
    *   Simply push this bracket onto our stack. We're "remembering" that we've opened it and will expect its corresponding closing bracket later.

*   **If it's a Closing Bracket (`)`, `}`, `]`):**
    *   **Check for an Empty Stack:** First, check if our stack is empty. If it is, it means we've encountered a closing bracket *without any corresponding open bracket*. This is invalid! Return `False`.
    *   **Pop and Compare:** If the stack is *not* empty, pop the top element from the stack. This `top_element` is the *most recently opened* bracket.
    *   Now, compare this `top_element` with the *expected* opening bracket for our *current closing bracket*. We'll use our `mapping` dictionary for this. If `mapping[current_closing_bracket]` (e.g., `'('` for `')'`) is *not* equal to `top_element`, then we have a mismatch (e.g., `(]` or `{[)]`). This is invalid! Return `False`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Final Check:&lt;/strong&gt; After checking all characters in the string:

&lt;ul&gt;
&lt;li&gt;  If our stack is &lt;strong&gt;empty&lt;/strong&gt;, it means every opening bracket we pushed onto the stack eventually found its matching closing bracket. The string is &lt;strong&gt;valid&lt;/strong&gt;! Return &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  If our stack is &lt;strong&gt;not empty&lt;/strong&gt;, it means there are still some opening brackets left on the stack that never found their closing partners. The string is &lt;strong&gt;invalid&lt;/strong&gt;! Return &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  The Code (Python)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Our stack to keep track of opening brackets
&lt;/span&gt;        &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

        &lt;span class="c1"&gt;# A dictionary to map closing brackets to their corresponding opening brackets
&lt;/span&gt;        &lt;span class="c1"&gt;# This helps us quickly check if a closing bracket matches the top of the stack.
&lt;/span&gt;        &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;# Iterate through each character in the input string
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# If the character is a CLOSING bracket (i.e., it's a key in our mapping)
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;# Pop the top element from the stack if it's not empty.
&lt;/span&gt;                &lt;span class="c1"&gt;# If the stack is empty, it means we encountered a closing bracket
&lt;/span&gt;                &lt;span class="c1"&gt;# without any open bracket preceding it. We use '#' as a dummy value
&lt;/span&gt;                &lt;span class="c1"&gt;# to ensure the comparison fails correctly in that case.
&lt;/span&gt;                &lt;span class="n"&gt;top_element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

                &lt;span class="c1"&gt;# Check if the popped opening bracket matches the expected opening bracket
&lt;/span&gt;                &lt;span class="c1"&gt;# for the current closing bracket.
&lt;/span&gt;                &lt;span class="c1"&gt;# If they don't match (or if stack was empty, top_element is '#'),
&lt;/span&gt;                &lt;span class="c1"&gt;# then the sequence is invalid.
&lt;/span&gt;                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;top_element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
            &lt;span class="c1"&gt;# If the character is an OPENING bracket
&lt;/span&gt;            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;# Push all opening brackets onto the stack to remember them.
&lt;/span&gt;                &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# After iterating through the entire string:
&lt;/span&gt;        &lt;span class="c1"&gt;# If the stack is empty, it means all opening brackets found their matching
&lt;/span&gt;        &lt;span class="c1"&gt;# closing brackets in the correct order. The string is valid.
&lt;/span&gt;        &lt;span class="c1"&gt;# If the stack is not empty, it means there are unclosed opening brackets.
&lt;/span&gt;        &lt;span class="c1"&gt;# So, 'not stack' will return True if empty, False if not empty.
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  How Fast &amp;amp; How Much Memory? (Complexity Analysis)
&lt;/h2&gt;

&lt;p&gt;Understanding how efficient our solution is is super important!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;N&lt;/code&gt; is the length of the input string &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  We iterate through the string exactly once. For each character, we perform constant-time operations (dictionary lookup, stack push/pop).&lt;/li&gt;
&lt;li&gt;  Therefore, the time taken grows linearly with the length of the string.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  In the worst-case scenario (e.g., &lt;code&gt;(((((((((&lt;/code&gt;), our stack could end up storing all &lt;code&gt;N&lt;/code&gt; opening brackets if they never find their match.&lt;/li&gt;
&lt;li&gt;  So, the space required for the stack can grow linearly with the length of the string.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Stacks are your friends for order-dependent matching!&lt;/strong&gt; Whenever you need to match opening/closing items, process things in reverse order of appearance, or manage recursive structures, think of a stack.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Dictionaries for quick lookups:&lt;/strong&gt; Using &lt;code&gt;mapping&lt;/code&gt; for &lt;code&gt;closing -&amp;gt; opening&lt;/code&gt; bracket pairs makes our code clean and efficient for comparisons.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Edge cases matter:&lt;/strong&gt; Remember to handle scenarios like an empty stack when a closing bracket appears, or a non-empty stack at the very end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This problem is a fantastic introduction to stacks and a common interview question. Mastering it sets a strong foundation for more complex problems!&lt;/p&gt;

&lt;p&gt;Happy coding! Keep those brackets balanced! 😉&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Authored by NavyaSree_14. Published on 2026-05-16 16:25:05.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 1. Two Sum</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Sat, 16 May 2026 10:51:25 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-40lh</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-1-two-sum-40lh</guid>
      <description>&lt;h1&gt;
  
  
  Your First LeetCode Victory: Cracking "Two Sum" with Python!
&lt;/h1&gt;

&lt;p&gt;Hey there, aspiring coders and LeetCode adventurers! 👋 Welcome to your first step into the exciting world of algorithmic problem-solving. Today, we're tackling a true classic, often the very first problem many developers encounter: &lt;strong&gt;"Two Sum"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Don't let the "LeetCode" name intimidate you! We'll break it down step-by-step, uncover the magic behind the solution, and get you confident in your coding journey. Think of this as your friendly guide to an "aha!" moment. ✨&lt;/p&gt;




&lt;h2&gt;
  
  
  🧐 The Problem: What Are We Looking For?
&lt;/h2&gt;

&lt;p&gt;Imagine you're given a list of numbers, like a shuffled deck of cards, and a specific "target" number. Your mission, should you choose to accept it, is to find &lt;em&gt;two different numbers&lt;/em&gt; in that list that, when added together, perfectly equal your target.&lt;/p&gt;

&lt;p&gt;But there's a twist! Instead of just saying "Found them!", you need to tell us &lt;em&gt;where&lt;/em&gt; they are in the list – their positions, or &lt;strong&gt;indices&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's the official breakdown:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Given:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  An array (list) of integers called &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  An integer called &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Return:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The indices of the two numbers that add up to &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important Guarantees (to make our lives easier!):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  There will always be &lt;em&gt;exactly one&lt;/em&gt; pair of numbers that sum to the target. No need to worry about multiple solutions or no solutions.&lt;/li&gt;
&lt;li&gt;  You cannot use the same number twice (even if it appears multiple times in the list, you pick two distinct positions).&lt;/li&gt;
&lt;li&gt;  You can return the indices in any order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at some examples to make it super clear:&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;target = 9&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[0, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Why?&lt;/strong&gt; Because &lt;code&gt;nums[0]&lt;/code&gt; (which is &lt;code&gt;2&lt;/code&gt;) + &lt;code&gt;nums[1]&lt;/code&gt; (which is &lt;code&gt;7&lt;/code&gt;) = &lt;code&gt;9&lt;/code&gt;. We return their positions: &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums = [3, 2, 4]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;target = 6&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[1, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Why?&lt;/strong&gt; Because &lt;code&gt;nums[1]&lt;/code&gt; (which is &lt;code&gt;2&lt;/code&gt;) + &lt;code&gt;nums[2]&lt;/code&gt; (which is &lt;code&gt;4&lt;/code&gt;) = &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See? Not so scary, right? Now, let's figure out how to solve it!&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 Intuition: How Would You Find It?
&lt;/h2&gt;

&lt;p&gt;Before jumping to code, let's think like a human.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The "Brute Force" Way (Our first thought):&lt;/strong&gt;&lt;br&gt;
If you had &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt; and &lt;code&gt;target = 9&lt;/code&gt;, you might do this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Take &lt;code&gt;2&lt;/code&gt; (at index &lt;code&gt;0&lt;/code&gt;).

&lt;ul&gt;
&lt;li&gt;  Pair it with &lt;code&gt;7&lt;/code&gt; (at index &lt;code&gt;1&lt;/code&gt;): &lt;code&gt;2 + 7 = 9&lt;/code&gt;. &lt;strong&gt;Aha! Found it!&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  (If not, you'd continue: &lt;code&gt;2 + 11 = 13&lt;/code&gt; (no), &lt;code&gt;2 + 15 = 17&lt;/code&gt; (no)).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; Then you'd move to &lt;code&gt;7&lt;/code&gt; (at index &lt;code&gt;1&lt;/code&gt;).

&lt;ul&gt;
&lt;li&gt;  Pair it with &lt;code&gt;11&lt;/code&gt; (at index &lt;code&gt;2&lt;/code&gt;): &lt;code&gt;7 + 11 = 18&lt;/code&gt; (no).&lt;/li&gt;
&lt;li&gt;  Pair it with &lt;code&gt;15&lt;/code&gt; (at index &lt;code&gt;3&lt;/code&gt;): &lt;code&gt;7 + 15 = 22&lt;/code&gt; (no).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; ...and so on.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach works! You're checking &lt;em&gt;every possible pair&lt;/em&gt;. But imagine a list with thousands of numbers. Checking every number with every other number would take a &lt;em&gt;very long time&lt;/em&gt;. This is what we call an &lt;strong&gt;O(n^2)&lt;/strong&gt; solution (quadratic time complexity), which means the time it takes grows very quickly as the list gets larger. Can we do better?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The "Aha!" Moment (The Smart Way):&lt;/strong&gt;&lt;br&gt;
Let's rethink. We're looking for &lt;code&gt;num1 + num2 = target&lt;/code&gt;.&lt;br&gt;
This can be rewritten as &lt;code&gt;num2 = target - num1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, as we look at &lt;code&gt;num1&lt;/code&gt;, we immediately know &lt;em&gt;exactly what &lt;code&gt;num2&lt;/code&gt; we need&lt;/em&gt;.&lt;br&gt;
Instead of blindly searching for &lt;code&gt;num2&lt;/code&gt; by checking every subsequent number, what if we could instantly ask: "Hey, have I &lt;em&gt;already seen&lt;/em&gt; the &lt;code&gt;num2&lt;/code&gt; I need among the numbers I've processed so far?"&lt;/p&gt;

&lt;p&gt;If we have a super-fast way to remember numbers we've seen and their positions, we could find the &lt;code&gt;num2&lt;/code&gt; instantly! This "super-fast way to remember and look up" is exactly what a &lt;strong&gt;hash map&lt;/strong&gt; (or a &lt;code&gt;dictionary&lt;/code&gt; in Python) is for!&lt;/p&gt;


&lt;h2&gt;
  
  
  🚶 The Approach: Step-by-Step with a Hash Map
&lt;/h2&gt;

&lt;p&gt;Here's how we'll use a hash map to solve "Two Sum" efficiently:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initialize an empty hash map (dictionary):&lt;/strong&gt; We'll call it &lt;code&gt;seen_numbers&lt;/code&gt;. This map will store key-value pairs where the &lt;code&gt;key&lt;/code&gt; is a number we've encountered, and the &lt;code&gt;value&lt;/code&gt; is its &lt;code&gt;index&lt;/code&gt; in the &lt;code&gt;nums&lt;/code&gt; array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Example: &lt;code&gt;{number: index}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Iterate through the &lt;code&gt;nums&lt;/code&gt; array:&lt;/strong&gt; Go through each number one by one. For each number, we'll keep track of its actual &lt;code&gt;value&lt;/code&gt; and its &lt;code&gt;index&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For each &lt;code&gt;num&lt;/code&gt; at &lt;code&gt;index&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
a.  &lt;strong&gt;Calculate the &lt;code&gt;complement&lt;/code&gt;:&lt;/strong&gt; This is the number we &lt;em&gt;need&lt;/em&gt; to find to make the sum equal to &lt;code&gt;target&lt;/code&gt;.&lt;br&gt;
    &lt;code&gt;complement = target - num&lt;/code&gt;&lt;br&gt;
b.  &lt;strong&gt;Check if &lt;code&gt;complement&lt;/code&gt; is in &lt;code&gt;seen_numbers&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
    *   &lt;strong&gt;IF YES:&lt;/strong&gt; Fantastic! We've found our pair. The &lt;code&gt;complement&lt;/code&gt; is in our hash map, and its index is &lt;code&gt;seen_numbers[complement]&lt;/code&gt;. The &lt;code&gt;current num&lt;/code&gt; is what we're looking at right now, and its index is &lt;code&gt;i&lt;/code&gt;. We return &lt;code&gt;[seen_numbers[complement], i]&lt;/code&gt;. We're done!&lt;br&gt;
    *   &lt;strong&gt;IF NO:&lt;/strong&gt; No worries! The &lt;code&gt;complement&lt;/code&gt; isn't among the numbers we've seen &lt;em&gt;yet&lt;/em&gt;. So, we add the &lt;em&gt;current number (&lt;code&gt;num&lt;/code&gt;)&lt;/em&gt; and its &lt;em&gt;index (&lt;code&gt;i&lt;/code&gt;)&lt;/em&gt; to our &lt;code&gt;seen_numbers&lt;/code&gt; map. This way, if a future number needs &lt;code&gt;num&lt;/code&gt; as its complement, it can find it quickly. &lt;code&gt;seen_numbers[num] = i&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the problem guarantees there's exactly one solution, our loop will always find a pair and return it.&lt;/p&gt;

&lt;p&gt;Let's trace &lt;code&gt;nums = [2, 7, 11, 15], target = 9&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;num&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;complement = 9 - num&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;complement&lt;/code&gt; in &lt;code&gt;seen_numbers&lt;/code&gt;?&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;&lt;code&gt;seen_numbers&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;9 - 2 = 7&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No (map is empty)&lt;/td&gt;
&lt;td&gt;Add &lt;code&gt;2: 0&lt;/code&gt; to &lt;code&gt;seen_numbers&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{2: 0}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;&lt;code&gt;9 - 7 = 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes (2 is in map, at index 0)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Return &lt;code&gt;[seen_numbers[2], 1]&lt;/code&gt; which is &lt;code&gt;[0, 1]&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;(Function exits)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Boom! We found it on the second step. Much faster than checking &lt;code&gt;2&lt;/code&gt; with &lt;code&gt;7&lt;/code&gt;, &lt;code&gt;11&lt;/code&gt;, &lt;code&gt;15&lt;/code&gt; and then &lt;code&gt;7&lt;/code&gt; with &lt;code&gt;11&lt;/code&gt;, &lt;code&gt;15&lt;/code&gt;, etc.&lt;/p&gt;


&lt;h2&gt;
  
  
  💻 The Code
&lt;/h2&gt;

&lt;p&gt;Here's the Python implementation of our approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;List&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="c1"&gt;# Initialize an empty hash map (Python dictionary)
&lt;/span&gt;        &lt;span class="c1"&gt;# It will store numbers we've seen and their indices: {number: index}
&lt;/span&gt;        &lt;span class="n"&gt;hashmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; 

        &lt;span class="c1"&gt;# Iterate through the array 'nums' along with their indices
&lt;/span&gt;        &lt;span class="k"&gt;for&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;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&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="c1"&gt;# Calculate the 'complement' needed to reach the target
&lt;/span&gt;            &lt;span class="n"&gt;complement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;

            &lt;span class="c1"&gt;# Check if this 'complement' is already in our hash map
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;complement&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;# If yes, we found the pair!
&lt;/span&gt;                &lt;span class="c1"&gt;# The index of the complement is hashmap[complement]
&lt;/span&gt;                &lt;span class="c1"&gt;# The index of the current number is 'i'
&lt;/span&gt;                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;complement&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;# If the complement was NOT found, it means the current 'num'
&lt;/span&gt;            &lt;span class="c1"&gt;# is not the second part of a pair with any number we've seen yet.
&lt;/span&gt;            &lt;span class="c1"&gt;# So, we add the current 'num' and its 'index' to the hash map,
&lt;/span&gt;            &lt;span class="c1"&gt;# so future numbers can check against it.
&lt;/span&gt;            &lt;span class="n"&gt;hashmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&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="c1"&gt;# This line is technically unreachable due to the problem constraint
&lt;/span&gt;        &lt;span class="c1"&gt;# "Only one valid answer exists." meaning we'll always return inside the loop.
&lt;/span&gt;        &lt;span class="c1"&gt;# However, for completeness in a real-world scenario, you might raise an error
&lt;/span&gt;        &lt;span class="c1"&gt;# or return an empty list if no solution were guaranteed.
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⏱️ Time &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;Understanding how efficient our code is crucial for competitive programming and good software design.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  We iterate through the &lt;code&gt;nums&lt;/code&gt; array &lt;strong&gt;once&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  Inside the loop, operations like calculating the &lt;code&gt;complement&lt;/code&gt;, checking if an element &lt;code&gt;in&lt;/code&gt; a hash map, and adding an element to a hash map (&lt;code&gt;hashmap[num] = i&lt;/code&gt;) all take, on average, &lt;strong&gt;constant time (O(1))&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  Since we do a constant amount of work for each of the N elements, the total time complexity is &lt;strong&gt;O(N)&lt;/strong&gt;. This is fantastic! It means if the list doubles in size, the time to solve it roughly doubles, which is very efficient.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  In the worst-case scenario, our hash map &lt;code&gt;hashmap&lt;/code&gt; might end up storing almost all the numbers from the &lt;code&gt;nums&lt;/code&gt; array and their indices. This would happen if the two numbers that sum to the target are the very last two numbers in the array.&lt;/li&gt;
&lt;li&gt;  Therefore, the space used by the hash map grows proportionally to the number of elements in &lt;code&gt;nums&lt;/code&gt;. So, the space complexity is &lt;strong&gt;O(N)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This O(N) time and O(N) space solution is a significant improvement over the brute-force O(N^2) time complexity!&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Hash Maps are Your Friends:&lt;/strong&gt; When you need to quickly check if an element exists or retrieve its associated value (like an index), hash maps (dictionaries in Python) are incredibly powerful. They offer average O(1) lookups and insertions.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The "Complement" Pattern:&lt;/strong&gt; For problems asking you to find pairs that sum to a target, thinking about the &lt;code&gt;complement&lt;/code&gt; (&lt;code&gt;target - current_number&lt;/code&gt;) can often lead to an efficient solution using a hash map.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Optimize Beyond Brute Force:&lt;/strong&gt; Always consider simpler, less efficient solutions first (like brute force) to understand the problem, then challenge yourself to find more optimized approaches. O(N^2) often becomes O(N) with the right data structure.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Read Constraints Carefully:&lt;/strong&gt; "Exactly one solution" and "cannot use the same element twice" simplify the problem by removing edge cases you might otherwise need to handle.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Congratulations! You've just conquered LeetCode's "Two Sum" problem using an efficient, industry-standard approach. This problem is a foundational building block, and mastering it sets you up for success in many more algorithmic challenges. Keep practicing, keep learning, and happy coding! 🚀&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Author Account: NavyaSree_14&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Time Published: 2026-05-16 16:21:00&lt;/em&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 153. Find Minimum in Rotated Sorted Array</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Fri, 15 May 2026 11:06:52 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-153-find-minimum-in-rotated-sorted-array-3hie</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-153-find-minimum-in-rotated-sorted-array-3hie</guid>
      <description>&lt;h1&gt;
  
  
  Master the Mystery: Finding the Minimum in a Rotated Sorted Array!
&lt;/h1&gt;

&lt;p&gt;Hey LeetCoders and aspiring developers! 👋 Vansh2710 here, ready to demystify another classic algorithm problem that often pops up in interviews and challenges our problem-solving skills. Today, we're tackling LeetCode 153: "Find Minimum in Rotated Sorted Array."&lt;/p&gt;

&lt;p&gt;This problem is a fantastic introduction to how binary search, a technique you might think is only for perfectly sorted arrays, can be cleverly adapted to solve more complex scenarios. Ready to dive in? Let's go!&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem: A Twisted Tale of Sorted Numbers
&lt;/h2&gt;

&lt;p&gt;Imagine you have a perfectly sorted array of unique numbers, like &lt;code&gt;[0, 1, 2, 4, 5, 6, 7]&lt;/code&gt;. Simple enough, right? The minimum is clearly &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, here's the twist: this array gets "rotated." This means some number of elements from the end are moved to the beginning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; &lt;code&gt;[0, 1, 2, 4, 5, 6, 7]&lt;/code&gt; might become &lt;code&gt;[4, 5, 6, 7, 0, 1, 2]&lt;/code&gt; if it was rotated 4 times. Or, &lt;code&gt;[7, 0, 1, 2, 4, 5, 6]&lt;/code&gt; if rotated once.&lt;/p&gt;

&lt;p&gt;Your mission, should you choose to accept it, is to find the minimum element in this &lt;em&gt;rotated sorted array&lt;/em&gt;. The kicker? Your algorithm &lt;strong&gt;must run in O(log n) time&lt;/strong&gt;. This immediately screams "binary search" to a competitive programmer, but how do we apply it to a "broken" sorted array? That's the fun part!&lt;/p&gt;




&lt;h2&gt;
  
  
  The "Aha!" Moment: Binary Search's Hidden Power
&lt;/h2&gt;

&lt;p&gt;At first glance, an array like &lt;code&gt;[4,5,6,7,0,1,2]&lt;/code&gt; doesn't look sorted. But wait! It's &lt;em&gt;partially&lt;/em&gt; sorted. It's actually two sorted arrays mashed together. The minimum element is the "pivot" where the ascending order breaks.&lt;/p&gt;

&lt;p&gt;Consider &lt;code&gt;[4,5,6,7,0,1,2]&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The left part: &lt;code&gt;[4,5,6,7]&lt;/code&gt; (sorted)&lt;/li&gt;
&lt;li&gt;  The right part: &lt;code&gt;[0,1,2]&lt;/code&gt; (sorted)&lt;/li&gt;
&lt;li&gt;  The minimum &lt;code&gt;0&lt;/code&gt; is the first element of the right part.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our "aha!" moment is realizing that we can still use the core idea of binary search: &lt;strong&gt;discarding half of the search space in each step&lt;/strong&gt;. The trick is figuring out &lt;em&gt;which&lt;/em&gt; half to discard.&lt;/p&gt;

&lt;p&gt;The key insight: &lt;strong&gt;If we compare the middle element (&lt;code&gt;nums[mid]&lt;/code&gt;) with the rightmost element (&lt;code&gt;nums[e]&lt;/code&gt;), we can tell which "side" the minimum element is on.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  If &lt;code&gt;nums[mid] &amp;gt; nums[e]&lt;/code&gt;: This means &lt;code&gt;mid&lt;/code&gt; is in the "larger" sorted part (e.g., &lt;code&gt;7&lt;/code&gt; in &lt;code&gt;[4,5,6,7,0,1,2]&lt;/code&gt;). The minimum &lt;em&gt;must&lt;/em&gt; be in the elements to the right of &lt;code&gt;mid&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  If &lt;code&gt;nums[mid] &amp;lt;= nums[e]&lt;/code&gt;: This means &lt;code&gt;mid&lt;/code&gt; is in the "smaller" sorted part (e.g., &lt;code&gt;0&lt;/code&gt; in &lt;code&gt;[4,5,6,7,0,1,2]&lt;/code&gt;). The minimum is either &lt;code&gt;mid&lt;/code&gt; itself or somewhere to its left.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This simple comparison is our compass in the rotated array jungle!&lt;/p&gt;




&lt;h2&gt;
  
  
  The Approach: Navigating with Binary Search
&lt;/h2&gt;

&lt;p&gt;Let's break down the binary search logic step-by-step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initialize Pointers&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;s&lt;/code&gt; (start) to &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;e&lt;/code&gt; (end) to &lt;code&gt;nums.size() - 1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Loop Condition&lt;/strong&gt;: Continue as long as &lt;code&gt;s &amp;lt; e&lt;/code&gt;. When &lt;code&gt;s&lt;/code&gt; equals &lt;code&gt;e&lt;/code&gt;, we've narrowed down our search to a single element, which must be the minimum.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Calculate Midpoint&lt;/strong&gt;: &lt;code&gt;mid = s + (e - s) / 2&lt;/code&gt;. This prevents potential integer overflow compared to &lt;code&gt;(s + e) / 2&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Decision Time! Compare &lt;code&gt;nums[mid]&lt;/code&gt; with &lt;code&gt;nums[e]&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*   **Case 1: `nums[mid] &amp;gt; nums[e]`**
    *   This tells us that the pivot (minimum element) *must* be located somewhere to the **right** of `mid`. Why? Because `nums[mid]` is part of the "larger" sorted segment, and `nums[e]` is part of the "smaller" segment. For `nums[mid]` to be greater than `nums[e]`, the break point (the minimum) has to be between `mid` and `e` (inclusive of `e`).
    *   So, we update `s = mid + 1`.

*   **Case 2: `nums[mid] &amp;lt;= nums[e]`**
    *   This implies two possibilities:
        1.  `nums[mid]` is the minimum itself.
        2.  The minimum is somewhere in the left half of the array (including `mid`).
    *   In both scenarios, `mid` or an element to its left could be the minimum. We can safely discard the right half *after* `mid`.
    *   So, we update `e = mid`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Termination&lt;/strong&gt;: When the &lt;code&gt;while&lt;/code&gt; loop finishes (&lt;code&gt;s == e&lt;/code&gt;), the pointer &lt;code&gt;s&lt;/code&gt; (or &lt;code&gt;e&lt;/code&gt;) will be pointing to the minimum element. Return &lt;code&gt;nums[s]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's quickly trace &lt;code&gt;[3,4,5,1,2]&lt;/code&gt; (Example 1):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Initial: &lt;code&gt;s=0, e=4, nums = [3,4,5,1,2]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Iteration 1&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;mid = 0 + (4-0)/2 = 2&lt;/code&gt;. &lt;code&gt;nums[mid] = nums[2] = 5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;nums[mid] (5) &amp;gt; nums[e] (2)&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Minimum is to the right. &lt;code&gt;s = mid + 1 = 3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  State: &lt;code&gt;s=3, e=4&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Iteration 2&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;mid = 3 + (4-3)/2 = 3&lt;/code&gt;. &lt;code&gt;nums[mid] = nums[3] = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;nums[mid] (1) &amp;lt;= nums[e] (2)&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Minimum is at or to the left. &lt;code&gt;e = mid = 3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  State: &lt;code&gt;s=3, e=3&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Loop ends&lt;/strong&gt; because &lt;code&gt;s&lt;/code&gt; is no longer less than &lt;code&gt;e&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;  Return &lt;code&gt;nums[s] = nums[3] = 1&lt;/code&gt;. Correct!&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Code 🧑‍💻
&lt;/h2&gt;

&lt;p&gt;Here's the C++ implementation following our logic:&lt;br&gt;
&lt;/p&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;findMin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&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;s&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;e&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;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="c1"&gt;// Loop until s and e meet (or cross, but in this specific binary search variant, they meet)&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;s&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;e&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;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&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="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="c1"&gt;// Calculate midpoint to prevent overflow&lt;/span&gt;

            &lt;span class="c1"&gt;// If nums[mid] is greater than nums[e], it means mid is in the 'left' sorted part&lt;/span&gt;
            &lt;span class="c1"&gt;// and the minimum must be in the elements to the right of mid.&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;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;e&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&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="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// Otherwise, nums[mid] is less than or equal to nums[e].&lt;/span&gt;
            &lt;span class="c1"&gt;// This means mid is in the 'right' sorted part (or is the minimum).&lt;/span&gt;
            &lt;span class="c1"&gt;// The minimum could be mid itself, or something to its left.&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;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&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;// When the loop terminates, s == e, and this index points to the minimum element.&lt;/span&gt;
        &lt;span class="k"&gt;return&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;s&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 &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Time Complexity: O(log n)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  Each step of our binary search effectively halves the search space. This is the hallmark of logarithmic time complexity. Regardless of how large &lt;code&gt;n&lt;/code&gt; gets, the number of operations grows very slowly.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Space Complexity: O(1)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  We are only using a few extra variables (&lt;code&gt;s&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, &lt;code&gt;mid&lt;/code&gt;) to keep track of our pointers. The amount of memory used doesn't scale with the input size &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Binary Search Adaptability&lt;/strong&gt;: Don't limit binary search to perfectly sorted arrays. Look for properties that allow you to discard half of the search space.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Pivot Point&lt;/strong&gt;: In rotated sorted arrays, the minimum element acts as a "pivot" or a "break" in the otherwise sorted order.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Comparison Strategy&lt;/strong&gt;: Comparing &lt;code&gt;nums[mid]&lt;/code&gt; with &lt;code&gt;nums[e]&lt;/code&gt; is a powerful technique to determine which half contains the minimum in this specific problem.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Edge Cases&lt;/strong&gt;: The algorithm gracefully handles cases where the array isn't rotated at all (e.g., &lt;code&gt;[1,2,3,4,5]&lt;/code&gt; where &lt;code&gt;nums[mid] &amp;lt;= nums[e]&lt;/code&gt; will always be true, eventually &lt;code&gt;e&lt;/code&gt; will be 0 and &lt;code&gt;nums[0]&lt;/code&gt; is returned).&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;And there you have it! Finding the minimum in a rotated sorted array using an elegant O(log n) binary search solution. This problem is a fantastic way to deepen your understanding of binary search and its versatility. Keep practicing, and you'll master these algorithmic patterns in no time!&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Author Account&lt;/strong&gt;: Vansh2710&lt;br&gt;
&lt;strong&gt;Time Published&lt;/strong&gt;: 2026-05-15 16:35:29&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 2784. Check if Array is Good</title>
      <dc:creator>Vansh Aggarwal</dc:creator>
      <pubDate>Thu, 14 May 2026 17:16:05 +0000</pubDate>
      <link>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-2784-check-if-array-is-good-14h2</link>
      <guid>https://dev.to/vansh_aggarwal_5fb2fff667/leetcode-solution-2784-check-if-array-is-good-14h2</guid>
      <description>&lt;h1&gt;
  
  
  Cracking LeetCode 2784: Is This Array "Good"? Let's Find Out!
&lt;/h1&gt;

&lt;p&gt;Hey everyone! Vansh2710 here, your friendly neighborhood competitive programmer and technical writing enthusiast! Today, we're diving into a fun little LeetCode problem that's perfect for beginners to sharpen their logical thinking: &lt;strong&gt;2784. Check if Array is Good&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Don't let the "permutation" jargon scare you. We'll break it down step by step and discover an elegant solution together. Ready to level up your LeetCode game? Let's go!&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Problem Explanation: What Makes an Array "Good"?
&lt;/h2&gt;

&lt;p&gt;Imagine you have a special kind of array called &lt;code&gt;base[n]&lt;/code&gt;. This &lt;code&gt;base[n]&lt;/code&gt; array always looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[1, 2, ..., n - 1, n, n]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In plain English, it's an array of length &lt;code&gt;n + 1&lt;/code&gt; that contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  All numbers from &lt;code&gt;1&lt;/code&gt; up to &lt;code&gt;n - 1&lt;/code&gt; appear &lt;strong&gt;exactly once&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  The number &lt;code&gt;n&lt;/code&gt; appears &lt;strong&gt;exactly twice&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  If &lt;code&gt;n = 1&lt;/code&gt;, then &lt;code&gt;base[1] = [1, 1]&lt;/code&gt;. (Length 1+1=2)&lt;/li&gt;
&lt;li&gt;  If &lt;code&gt;n = 3&lt;/code&gt;, then &lt;code&gt;base[3] = [1, 2, 3, 3]&lt;/code&gt;. (Length 3+1=4)&lt;/li&gt;
&lt;li&gt;  If &lt;code&gt;n = 5&lt;/code&gt;, then &lt;code&gt;base[5] = [1, 2, 3, 4, 5, 5]&lt;/code&gt;. (Length 5+1=6)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem asks us to determine if a &lt;em&gt;given&lt;/em&gt; array &lt;code&gt;nums&lt;/code&gt; is a &lt;strong&gt;permutation&lt;/strong&gt; of one of these &lt;code&gt;base[n]&lt;/code&gt; arrays. "Permutation" just means it contains the same numbers, just possibly in a different order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's look at the examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;nums = [2, 1, 3]&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The largest number is &lt;code&gt;3&lt;/code&gt;. So, if this were a &lt;code&gt;base[n]&lt;/code&gt; array, &lt;code&gt;n&lt;/code&gt; must be &lt;code&gt;3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;base[3]&lt;/code&gt; should be &lt;code&gt;[1, 2, 3, 3]&lt;/code&gt;, which has 4 elements.&lt;/li&gt;
&lt;li&gt;  But &lt;code&gt;nums&lt;/code&gt; has only 3 elements. So, it cannot be &lt;code&gt;base[3]&lt;/code&gt;. Output: &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;nums = [1, 3, 3, 2]&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The largest number is &lt;code&gt;3&lt;/code&gt;. So, &lt;code&gt;n&lt;/code&gt; must be &lt;code&gt;3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;base[3]&lt;/code&gt; is &lt;code&gt;[1, 2, 3, 3]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;nums&lt;/code&gt; also has 4 elements, just like &lt;code&gt;base[3]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Can we rearrange &lt;code&gt;[1, 3, 3, 2]&lt;/code&gt; to get &lt;code&gt;[1, 2, 3, 3]&lt;/code&gt;? Yes! They contain the exact same numbers with the same counts. Output: &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;nums = [3, 4, 4, 1, 2, 1]&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Largest number is &lt;code&gt;4&lt;/code&gt;. So, &lt;code&gt;n&lt;/code&gt; must be &lt;code&gt;4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;base[4]&lt;/code&gt; should be &lt;code&gt;[1, 2, 3, 4, 4]&lt;/code&gt;, which has 5 elements.&lt;/li&gt;
&lt;li&gt;  But &lt;code&gt;nums&lt;/code&gt; has 6 elements. It's too long! Output: &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;See? It's all about checking the elements, their counts, and the array's length!&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 Intuition: The "Aha!" Moment
&lt;/h2&gt;

&lt;p&gt;When you're trying to figure out if an array is a "permutation" of another specific array structure, the first thing that often comes to mind is &lt;strong&gt;sorting&lt;/strong&gt;. If two arrays are permutations of each other, they will be identical once sorted.&lt;/p&gt;

&lt;p&gt;So, if &lt;code&gt;nums&lt;/code&gt; is a "good" array, after sorting it, it should exactly match &lt;code&gt;[1, 2, ..., n - 1, n, n]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But what is &lt;code&gt;n&lt;/code&gt;? The problem examples give us a HUGE hint: &lt;strong&gt;&lt;code&gt;n&lt;/code&gt; is always the maximum value present in the &lt;code&gt;nums&lt;/code&gt; array.&lt;/strong&gt; If the &lt;code&gt;nums&lt;/code&gt; array is supposed to be &lt;code&gt;base[n]&lt;/code&gt;, then &lt;code&gt;n&lt;/code&gt; is the highest number appearing twice, and &lt;code&gt;n-1&lt;/code&gt; is the next highest. So, &lt;code&gt;n&lt;/code&gt; must be the largest element.&lt;/p&gt;

&lt;p&gt;Once we figure out &lt;code&gt;n&lt;/code&gt; (by finding the maximum value in &lt;code&gt;nums&lt;/code&gt;), we can determine two key properties that &lt;code&gt;nums&lt;/code&gt; &lt;em&gt;must&lt;/em&gt; satisfy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Length Check:&lt;/strong&gt; &lt;code&gt;nums&lt;/code&gt; must have &lt;code&gt;n + 1&lt;/code&gt; elements.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Content Check:&lt;/strong&gt; After sorting, &lt;code&gt;nums&lt;/code&gt; must look exactly like &lt;code&gt;[1, 2, ..., n - 1, n, n]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If &lt;code&gt;nums&lt;/code&gt; passes both these checks, it's a "good" array!&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Approach: Step-by-Step Logic
&lt;/h2&gt;

&lt;p&gt;Let's put our intuition into a concrete plan:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Determine the Candidate &lt;code&gt;n&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Find the maximum element in the input array &lt;code&gt;nums&lt;/code&gt;. Let's call this &lt;code&gt;max_val&lt;/code&gt;. This &lt;code&gt;max_val&lt;/code&gt; is our candidate &lt;code&gt;n&lt;/code&gt; from &lt;code&gt;base[n]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initial Length Check&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The &lt;code&gt;base[n]&lt;/code&gt; array always has a length of &lt;code&gt;n + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Check if the &lt;code&gt;nums.size()&lt;/code&gt; (the actual number of elements in &lt;code&gt;nums&lt;/code&gt;) is equal to &lt;code&gt;max_val + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  If &lt;code&gt;nums.size()&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt; equal to &lt;code&gt;max_val + 1&lt;/code&gt;, then &lt;code&gt;nums&lt;/code&gt; cannot be a permutation of &lt;code&gt;base[max_val]&lt;/code&gt;. Immediately return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sort the Array&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Sort the &lt;code&gt;nums&lt;/code&gt; array in non-decreasing order. This makes it easy to check the pattern &lt;code&gt;[1, 2, ..., n - 1, n, n]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verify Elements in Order&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  After sorting, we expect the elements to be &lt;code&gt;1, 2, ..., max_val - 1, max_val, max_val&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Loop from &lt;code&gt;i = 0&lt;/code&gt; up to &lt;code&gt;max_val - 2&lt;/code&gt; (which means checking elements &lt;code&gt;nums[0]&lt;/code&gt; through &lt;code&gt;nums[max_val - 2]&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  In this loop, for each &lt;code&gt;i&lt;/code&gt;, check if &lt;code&gt;nums[i]&lt;/code&gt; is equal to &lt;code&gt;i + 1&lt;/code&gt;. If it's not, it means we don't have the sequence &lt;code&gt;1, 2, 3, ...&lt;/code&gt;. Return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Special Case: What if &lt;code&gt;max_val&lt;/code&gt; is 1?&lt;/strong&gt; If &lt;code&gt;max_val = 1&lt;/code&gt;, then &lt;code&gt;base[1] = [1, 1]&lt;/code&gt;. The loop &lt;code&gt;i&lt;/code&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;max_val - 2&lt;/code&gt; (i.e., &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;-1&lt;/code&gt;) won't run, which is correct. We only need to check the two &lt;code&gt;1&lt;/code&gt;s.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verify the Duplicate &lt;code&gt;n&lt;/code&gt; (or &lt;code&gt;max_val&lt;/code&gt;)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  After the loop, we need to check the last two elements. These should both be &lt;code&gt;max_val&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Since &lt;code&gt;nums.size()&lt;/code&gt; is &lt;code&gt;max_val + 1&lt;/code&gt;, the elements are at indices &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;max_val&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  So, we need to check if &lt;code&gt;nums[max_val - 1]&lt;/code&gt; is equal to &lt;code&gt;max_val&lt;/code&gt; AND &lt;code&gt;nums[max_val]&lt;/code&gt; is equal to &lt;code&gt;max_val&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  If both these conditions are true, then &lt;code&gt;nums&lt;/code&gt; has the required duplicate &lt;code&gt;max_val&lt;/code&gt;. If not, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;All Checks Passed&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  If we've made it this far, it means &lt;code&gt;nums&lt;/code&gt; has the correct length, and its sorted elements match the &lt;code&gt;base[max_val]&lt;/code&gt; pattern. Return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach covers all the conditions systematically and efficiently!&lt;/p&gt;




&lt;h2&gt;
  
  
  💻 Code: Bringing the Logic to Life
&lt;/h2&gt;

&lt;p&gt;Here's the C++ implementation of our approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt; // Required for std::sort and std::max_element&lt;/span&gt;&lt;span class="cp"&gt;
&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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&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;current_size&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;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Step 1: Determine the candidate 'n' (max_val)&lt;/span&gt;
        &lt;span class="c1"&gt;// If the array is empty, it can't be good (constraints say nums.length &amp;gt;= 1)&lt;/span&gt;
        &lt;span class="c1"&gt;// If only one element, max_element will still work.&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max_val&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;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;x&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="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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;max_val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;max_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&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;// An alternative using std::max_element (requires &amp;lt;algorithm&amp;gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;// int max_val = *std::max_element(nums.begin(), nums.end());&lt;/span&gt;


        &lt;span class="c1"&gt;// Step 2: Initial Length Check&lt;/span&gt;
        &lt;span class="c1"&gt;// base[max_val] has length max_val + 1&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;current_size&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;max_val&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="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Step 3: Sort the Array&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&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;begin&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;end&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

        &lt;span class="c1"&gt;// Step 4: Verify Elements 1 to (max_val - 1)&lt;/span&gt;
        &lt;span class="c1"&gt;// Loop up to max_val - 1 (exclusive, so indices 0 to max_val - 2)&lt;/span&gt;
        &lt;span class="c1"&gt;// For example, if max_val = 3, we check nums[0] == 1, nums[1] == 2.&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;max_val&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="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="k"&gt;if&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;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;i&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="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&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;// Step 5: Verify the Duplicate 'n' (max_val)&lt;/span&gt;
        &lt;span class="c1"&gt;// The last two elements should both be max_val.&lt;/span&gt;
        &lt;span class="c1"&gt;// These are at indices max_val - 1 and max_val (since current_size = max_val + 1)&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;max_val&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="n"&gt;max_val&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;max_val&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;max_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;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Step 6: All Checks Passed&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&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 &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;Let's break down the efficiency of our solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Finding &lt;code&gt;max_val&lt;/code&gt;&lt;/strong&gt;: We iterate through &lt;code&gt;nums&lt;/code&gt; once. This takes &lt;code&gt;O(N)&lt;/code&gt; time, where &lt;code&gt;N&lt;/code&gt; is &lt;code&gt;nums.size()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Sorting &lt;code&gt;nums&lt;/code&gt;&lt;/strong&gt;: Using &lt;code&gt;std::sort&lt;/code&gt; on a &lt;code&gt;std::vector&lt;/code&gt; takes &lt;code&gt;O(N log N)&lt;/code&gt; time on average.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Checking elements after sorting&lt;/strong&gt;: We loop through &lt;code&gt;nums&lt;/code&gt; at most &lt;code&gt;N&lt;/code&gt; times (specifically &lt;code&gt;max_val&lt;/code&gt; times, which is roughly &lt;code&gt;N&lt;/code&gt;). This takes &lt;code&gt;O(N)&lt;/code&gt; time.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Overall&lt;/strong&gt;: The dominant factor is the sorting step. Therefore, the total time complexity is &lt;strong&gt;&lt;code&gt;O(N log N)&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;&lt;code&gt;max_val&lt;/code&gt; and &lt;code&gt;current_size&lt;/code&gt; variables&lt;/strong&gt;: These use &lt;code&gt;O(1)&lt;/code&gt; constant space.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;code&gt;std::sort&lt;/code&gt;&lt;/strong&gt;: For &lt;code&gt;std::vector&lt;/code&gt;, &lt;code&gt;std::sort&lt;/code&gt; typically uses an introspection sort algorithm, which has &lt;code&gt;O(log N)&lt;/code&gt; auxiliary space complexity for its recursive calls (or &lt;code&gt;O(N)&lt;/code&gt; in worst-case for heapsort partition, but often considered in-place or &lt;code&gt;O(log N)&lt;/code&gt; for the stack). For competitive programming contexts, it's often effectively treated as &lt;code&gt;O(1)&lt;/code&gt; &lt;em&gt;additional&lt;/em&gt; space if it sorts in-place. Let's consider it as &lt;strong&gt;&lt;code&gt;O(1)&lt;/code&gt;&lt;/strong&gt; (ignoring recursion stack space if small enough, or &lt;code&gt;O(log N)&lt;/code&gt; more precisely for a typical implementation).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Deconstruct the Problem:&lt;/strong&gt; Break down complex definitions (like &lt;code&gt;base[n]&lt;/code&gt;) into simpler rules (length, distinct elements, duplicate element).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Leverage Hints:&lt;/strong&gt; The example explanations often provide crucial clues, like how to determine &lt;code&gt;n&lt;/code&gt; (it's the max element!).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Sorting is Your Friend:&lt;/strong&gt; For problems involving permutations or specific ordered structures, sorting is often the most straightforward way to normalize the input for easy validation.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Edge Cases Matter:&lt;/strong&gt; Always consider small &lt;code&gt;n&lt;/code&gt; values (like &lt;code&gt;n=1&lt;/code&gt;) to ensure your logic holds. Our loop condition &lt;code&gt;i &amp;lt; max_val - 1&lt;/code&gt; correctly handles &lt;code&gt;max_val = 1&lt;/code&gt; by not running the loop, which is perfect as &lt;code&gt;base[1]&lt;/code&gt; only needs &lt;code&gt;[1, 1]&lt;/code&gt; checks.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Systematic Checks:&lt;/strong&gt; Go through each requirement (length, distinct elements, duplicate elements) one by one. If any check fails, you can immediately return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This problem is a fantastic warm-up to understanding how to translate descriptive array properties into a clear, implementable algorithm. Keep practicing!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Vansh2710&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Published: 2026-05-14 22:44:58&lt;/em&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
