<?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: Kushal Jain</title>
    <description>The latest articles on DEV Community by Kushal Jain (@kushal_jain_cb4a4542aa92a).</description>
    <link>https://dev.to/kushal_jain_cb4a4542aa92a</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%2F1623383%2Fb414b5c7-d7a2-4d83-96cf-411148094f58.png</url>
      <title>DEV Community: Kushal Jain</title>
      <link>https://dev.to/kushal_jain_cb4a4542aa92a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kushal_jain_cb4a4542aa92a"/>
    <language>en</language>
    <item>
      <title>LeetCode Solution: 1. Two Sum</title>
      <dc:creator>Kushal Jain</dc:creator>
      <pubDate>Sun, 24 May 2026 10:31:32 +0000</pubDate>
      <link>https://dev.to/kushal_jain_cb4a4542aa92a/leetcode-solution-1-two-sum-53a7</link>
      <guid>https://dev.to/kushal_jain_cb4a4542aa92a/leetcode-solution-1-two-sum-53a7</guid>
      <description>&lt;h1&gt;
  
  
  Your First LeetCode Journey: Conquering 'Two Sum' (Problem 1) with Ease!
&lt;/h1&gt;

&lt;p&gt;Hey fellow developers! 👋 Kushalx here, and today we're diving into the absolute cornerstone of algorithmic problem-solving: LeetCode's &lt;em&gt;very first&lt;/em&gt; problem, &lt;strong&gt;Two Sum&lt;/strong&gt;. If you're just starting your LeetCode adventure, this is the perfect place to begin. It's a fantastic problem that introduces powerful concepts you'll use again and again.&lt;/p&gt;

&lt;p&gt;Let's break it down, understand the "why" behind the solution, and get you ready to crush future challenges!&lt;/p&gt;




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

&lt;p&gt;Imagine you have a list of numbers, and a specific "target" number. Your mission, should you choose to accept it, is to find two numbers within that list that add up to your target. Once you find them, you need to return their positions (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;You are given:&lt;/p&gt;

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

&lt;p&gt;You need to:&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;  You can assume that &lt;em&gt;each input would have exactly one solution&lt;/em&gt;. No need to worry about multiple pairs or no solution existing!&lt;/li&gt;
&lt;li&gt;  You &lt;em&gt;may not use the same element twice&lt;/em&gt;. If &lt;code&gt;nums[0]&lt;/code&gt; is part of the sum, you can't use &lt;code&gt;nums[0]&lt;/code&gt; again as the second number.&lt;/li&gt;
&lt;li&gt;  You can return the answer in any order (e.g., &lt;code&gt;[0, 1]&lt;/code&gt; or &lt;code&gt;[1, 0]&lt;/code&gt; is fine).&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ul&gt;
&lt;li&gt;  Here, &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;) equals &lt;code&gt;9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  So, the output should be &lt;code&gt;[0, 1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;nums = [3, 2, 4]&lt;/code&gt;, &lt;code&gt;target = 6&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&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;) equals &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Output: &lt;code&gt;[1, 2]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;nums = [3, 3]&lt;/code&gt;, &lt;code&gt;target = 6&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums[0]&lt;/code&gt; (which is &lt;code&gt;3&lt;/code&gt;) + &lt;code&gt;nums[1]&lt;/code&gt; (which is &lt;code&gt;3&lt;/code&gt;) equals &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Output: &lt;code&gt;[0, 1]&lt;/code&gt;. Notice we use two different elements, even if they have the same value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints to keep in mind:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The list &lt;code&gt;nums&lt;/code&gt; will have at least 2 numbers and at most 10,000 numbers.&lt;/li&gt;
&lt;li&gt;  Numbers in &lt;code&gt;nums&lt;/code&gt; and the &lt;code&gt;target&lt;/code&gt; can be quite large or small (between -10^9 and 10^9).&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  2. Intuition
&lt;/h2&gt;

&lt;p&gt;When faced with a problem like this, a common first thought is often the most straightforward one: "Let's check every possible pair!"&lt;/p&gt;
&lt;h3&gt;
  
  
  The Brute-Force Idea (and why it's not ideal)
&lt;/h3&gt;

&lt;p&gt;If we wanted to check every single pair, we could do something like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Take the first number (&lt;code&gt;nums[0]&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; Add it to every other number (&lt;code&gt;nums[1]&lt;/code&gt;, &lt;code&gt;nums[2]&lt;/code&gt;, ...).&lt;/li&gt;
&lt;li&gt; If any sum equals &lt;code&gt;target&lt;/code&gt;, we're done!&lt;/li&gt;
&lt;li&gt; If not, take the second number (&lt;code&gt;nums[1]&lt;/code&gt;) and add it to every number &lt;em&gt;after&lt;/em&gt; it (&lt;code&gt;nums[2]&lt;/code&gt;, &lt;code&gt;nums[3]&lt;/code&gt;, ...).&lt;/li&gt;
&lt;li&gt; Continue this process until you find a pair.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach works! It would definitely find the answer.&lt;br&gt;
However, consider a list with 10,000 numbers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  For the first number, you might check up to 9,999 other numbers.&lt;/li&gt;
&lt;li&gt;  For the second, up to 9,998.&lt;/li&gt;
&lt;li&gt;  And so on...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This quickly adds up to a &lt;em&gt;lot&lt;/em&gt; of operations. In computer science terms, this is an &lt;strong&gt;O(n^2)&lt;/strong&gt; solution (pronounced "O of n squared"), where &lt;code&gt;n&lt;/code&gt; is the number of elements. For &lt;code&gt;n = 10,000&lt;/code&gt;, &lt;code&gt;n^2&lt;/code&gt; is &lt;code&gt;100,000,000&lt;/code&gt; (100 million)! While computers are fast, this can become too slow for larger inputs. The "Follow-up" explicitly asks for something &lt;em&gt;less than O(n^2)&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Smarter Idea: What are we &lt;em&gt;really&lt;/em&gt; looking for?
&lt;/h3&gt;

&lt;p&gt;Let's rethink. If we have a &lt;code&gt;currentNumber&lt;/code&gt; and we know our &lt;code&gt;target&lt;/code&gt;, what's the &lt;em&gt;other&lt;/em&gt; number we need to find?&lt;br&gt;
It's simply &lt;code&gt;target - currentNumber&lt;/code&gt;. We can call this the &lt;code&gt;complement&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, for each &lt;code&gt;currentNumber&lt;/code&gt; in our list, we need to quickly answer the question: "Has its &lt;code&gt;complement&lt;/code&gt; appeared &lt;em&gt;before&lt;/em&gt; in the list, and if so, what was its index?"&lt;/p&gt;

&lt;p&gt;If we can answer that question very, very fast, we've got a winner!&lt;/p&gt;


&lt;h2&gt;
  
  
  3. Approach
&lt;/h2&gt;

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

&lt;p&gt;Hash maps allow you to store &lt;code&gt;key-value&lt;/code&gt; pairs and, crucially, look up a value by its key in &lt;strong&gt;average O(1) time&lt;/strong&gt; (constant time!). This is incredibly fast, like flipping open a dictionary directly to the word you want.&lt;/p&gt;

&lt;p&gt;Here's the refined strategy using a hash map:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Initialize an empty Hash Map:&lt;/strong&gt; This map will store numbers we've seen so far as &lt;em&gt;keys&lt;/em&gt; and their &lt;em&gt;indices&lt;/em&gt; as &lt;em&gt;values&lt;/em&gt;. (e.g., &lt;code&gt;{number: index}&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Iterate through the &lt;code&gt;nums&lt;/code&gt; array:&lt;/strong&gt; We'll go through each number one by one, keeping track of its index.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;For each &lt;code&gt;currentNumber&lt;/code&gt; at &lt;code&gt;index i&lt;/code&gt;:&lt;/strong&gt;
a.  &lt;strong&gt;Calculate the &lt;code&gt;complement&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;complement = target - currentNumber&lt;/code&gt;.
b.  &lt;strong&gt;Check if the &lt;code&gt;complement&lt;/code&gt; is already in our Hash Map:&lt;/strong&gt;
    *   If &lt;code&gt;numMap.containsKey(complement)&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;:
        *   &lt;strong&gt;Eureka!&lt;/strong&gt; We've found our pair! The &lt;code&gt;complement&lt;/code&gt; was seen before, and its index is &lt;code&gt;numMap.get(complement)&lt;/code&gt;. The &lt;code&gt;currentNumber&lt;/code&gt; is at &lt;code&gt;index i&lt;/code&gt;.
        *   Return &lt;code&gt;[numMap.get(complement), i]&lt;/code&gt;.
    *   If the &lt;code&gt;complement&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; in the Hash Map:
        *   This &lt;code&gt;currentNumber&lt;/code&gt; isn't the &lt;em&gt;second&lt;/em&gt; part of a pair we've seen yet. So, we add the &lt;code&gt;currentNumber&lt;/code&gt; itself to our map along with its index, in case it becomes the &lt;em&gt;first&lt;/em&gt; part of a pair for a number we see later.
        *   &lt;code&gt;numMap.put(currentNumber, i)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the problem guarantees exactly one solution, we know we'll find a pair and return early from the loop. We don't need to worry about the loop finishing without a return.&lt;/p&gt;


&lt;h2&gt;
  
  
  4. Code
&lt;/h2&gt;

&lt;p&gt;Let's put that approach into action with some Java code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.HashMap&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Map&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Create a HashMap to store numbers and their indices.&lt;/span&gt;
        &lt;span class="c1"&gt;// Key: The number itself&lt;/span&gt;
        &lt;span class="c1"&gt;// Value: The index of that number in the 'nums' array&lt;/span&gt;
        &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Iterate through the array 'nums' using an index 'i'&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&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="o"&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;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&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="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;currentNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&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="c1"&gt;// Get the current number&lt;/span&gt;

            &lt;span class="c1"&gt;// Calculate the 'complement' needed to reach the target&lt;/span&gt;
            &lt;span class="c1"&gt;// If currentNum + complement = target, then complement = target - currentNum&lt;/span&gt;
            &lt;span class="kt"&gt;int&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;currentNum&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// Check if the 'complement' already exists as a key in our map.&lt;/span&gt;
            &lt;span class="c1"&gt;// If it does, it means we've seen this 'complement' before,&lt;/span&gt;
            &lt;span class="c1"&gt;// and its index is stored as the value associated with the 'complement' key.&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;containsKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;complement&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// We found our pair!&lt;/span&gt;
                &lt;span class="c1"&gt;// The index of the complement is numMap.get(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="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;numMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// If the complement is NOT found in the map, it means the currentNum&lt;/span&gt;
            &lt;span class="c1"&gt;// hasn't found its partner yet. So, we add currentNum to the map&lt;/span&gt;
            &lt;span class="c1"&gt;// along with its index. This way, if a future number's complement&lt;/span&gt;
            &lt;span class="c1"&gt;// is this currentNum, we'll find it.&lt;/span&gt;
            &lt;span class="n"&gt;numMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentNum&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="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// According to the problem constraints, there will always be exactly one solution.&lt;/span&gt;
        &lt;span class="c1"&gt;// Therefore, this line should theoretically never be reached.&lt;/span&gt;
        &lt;span class="c1"&gt;// It's good practice to have a default return, or throw an exception&lt;/span&gt;
        &lt;span class="c1"&gt;// if the problem didn't guarantee a solution.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;Understanding how efficient your code is, is a crucial skill in competitive programming and software engineering.&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; array exactly once.&lt;/li&gt;
&lt;li&gt;  Inside the loop, operations like &lt;code&gt;numMap.containsKey()&lt;/code&gt;, &lt;code&gt;numMap.get()&lt;/code&gt;, and &lt;code&gt;numMap.put()&lt;/code&gt; take &lt;strong&gt;average O(1)&lt;/strong&gt; (constant) time. In the worst-case (rare for typical hash map implementations), these operations can degrade to O(n), but for practical purposes and competitive programming, we consider them O(1) on average.&lt;/li&gt;
&lt;li&gt;  Since we perform a constant number of O(1) operations for each of the &lt;code&gt;n&lt;/code&gt; elements, the total time complexity is &lt;strong&gt;O(n)&lt;/strong&gt;. This is a significant improvement over the O(n^2) brute-force 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 iterate through almost the entire &lt;code&gt;nums&lt;/code&gt; array before finding the pair (e.g., the last two numbers form the target).&lt;/li&gt;
&lt;li&gt;  In such a case, the &lt;code&gt;numMap&lt;/code&gt; would store up to &lt;code&gt;n-1&lt;/code&gt; elements.&lt;/li&gt;
&lt;li&gt;  Therefore, the space required by the hash map grows linearly with the input size &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Thus, the space complexity is &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Hash Maps are your best friend for fast lookups!&lt;/strong&gt; Whenever you need to quickly check if an element exists or retrieve associated data, think hash map (or dictionary). They convert O(n) or O(n^2) search problems into average O(1).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;The "Complement" Strategy:&lt;/strong&gt; Many problems involving sums or differences can be simplified by thinking about what &lt;em&gt;other&lt;/em&gt; number you need to reach a target. &lt;code&gt;complement = target - currentNumber&lt;/code&gt; is a powerful pattern.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Trade-offs between Time and Space:&lt;/strong&gt; We achieved better time complexity (O(n)) by using extra space (O(n)) for the hash map. This is a very common trade-off in algorithms. Sometimes, you'll optimize for space, sometimes for time, depending on the constraints.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Don't start with the most complex solution.&lt;/strong&gt; Start with the brute-force idea, understand its limitations, and then think about how data structures can help optimize it.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Submission Details
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Author Account:&lt;/strong&gt; Kushalx&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Publishing Time:&lt;/strong&gt; 2026-05-24 16:01:11&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Problem:&lt;/strong&gt; 1. Two Sum&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This problem is a fantastic stepping stone. Master it, and you've got a solid foundation for many more LeetCode challenges! Keep coding, keep learning, and happy problem-solving! ✨&lt;/p&gt;

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