<?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: Pratik Bandgar</title>
    <description>The latest articles on DEV Community by Pratik Bandgar (@pratik_bandgar_cce3b2e87a).</description>
    <link>https://dev.to/pratik_bandgar_cce3b2e87a</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%2F3946337%2F62af1167-b4e2-4846-afe2-15b069b42362.png</url>
      <title>DEV Community: Pratik Bandgar</title>
      <link>https://dev.to/pratik_bandgar_cce3b2e87a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratik_bandgar_cce3b2e87a"/>
    <language>en</language>
    <item>
      <title>LeetCode Solution: 1. Two Sum — May 23 2026 16:36</title>
      <dc:creator>Pratik Bandgar</dc:creator>
      <pubDate>Sat, 23 May 2026 11:06:07 +0000</pubDate>
      <link>https://dev.to/pratik_bandgar_cce3b2e87a/leetcode-solution-1-two-sum-may-23-2026-1636-53bn</link>
      <guid>https://dev.to/pratik_bandgar_cce3b2e87a/leetcode-solution-1-two-sum-may-23-2026-1636-53bn</guid>
      <description>&lt;h1&gt;
  
  
  Cracking the Code: Your First LeetCode Journey with Two Sum! 🚀
&lt;/h1&gt;

&lt;p&gt;Hey amazing developers! 👋 Ever wondered where to begin your LeetCode adventure? Look no further! The "Two Sum" problem is famously known as LeetCode's &lt;em&gt;very first&lt;/em&gt; problem, and it's an absolute classic for a reason. It's a fantastic starting point to understand fundamental algorithms and data structures.&lt;/p&gt;

&lt;p&gt;Today, we're going to dive deep into &lt;strong&gt;Two Sum (Problem #1)&lt;/strong&gt;, break it down, understand the thinking process, and explore a straightforward solution. Let's get started!&lt;/p&gt;




&lt;h3&gt;
  
  
  The Problem: Two Sum Explained Simply
&lt;/h3&gt;

&lt;p&gt;Imagine you have a list of numbers, say &lt;code&gt;[2, 7, 11, 15]&lt;/code&gt;. You're also given a specific &lt;code&gt;target&lt;/code&gt; number, like &lt;code&gt;9&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your goal? Find &lt;em&gt;two different numbers&lt;/em&gt; in that list that add up to your &lt;code&gt;target&lt;/code&gt;. Once you find them, you don't return the numbers themselves, but their &lt;em&gt;positions&lt;/em&gt; (their indices) in the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are the key rules:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;One Solution Only:&lt;/strong&gt; You can always assume there will be exactly one pair of numbers that adds up to the target. No need to worry about multiple answers or no answers!&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Repeats:&lt;/strong&gt; You can't use the same number twice. For example, if &lt;code&gt;nums = [3, 2, 4]&lt;/code&gt; and &lt;code&gt;target = 6&lt;/code&gt;, you can't use the &lt;code&gt;3&lt;/code&gt; at index &lt;code&gt;0&lt;/code&gt; with itself. You'd need to find &lt;code&gt;2&lt;/code&gt; (index &lt;code&gt;1&lt;/code&gt;) and &lt;code&gt;4&lt;/code&gt; (index &lt;code&gt;2&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at the examples:&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;br&gt;
Output: &lt;code&gt;[0,1]&lt;/code&gt;&lt;br&gt;
&lt;em&gt;Explanation: 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;.&lt;/em&gt;&lt;/p&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;br&gt;
Output: &lt;code&gt;[1,2]&lt;/code&gt;&lt;br&gt;
&lt;em&gt;Explanation: &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;/em&gt;&lt;/p&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;br&gt;
Output: &lt;code&gt;[0,1]&lt;/code&gt;&lt;br&gt;
&lt;em&gt;Explanation: &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;) == &lt;code&gt;6&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  The Intuition: How Would &lt;em&gt;You&lt;/em&gt; Solve It? 🤔
&lt;/h3&gt;

&lt;p&gt;Forget code for a second. If a friend gave you this list and a target, how would your brain tackle it?&lt;/p&gt;

&lt;p&gt;You'd probably pick the first number, right? Say &lt;code&gt;2&lt;/code&gt;. Then you'd think, "Okay, what number do I need to add to &lt;code&gt;2&lt;/code&gt; to get &lt;code&gt;9&lt;/code&gt;? Ah, &lt;code&gt;7&lt;/code&gt;!"&lt;br&gt;
Then you'd scan the &lt;em&gt;rest&lt;/em&gt; of the list to see if &lt;code&gt;7&lt;/code&gt; is there. If you find it, boom! You have your pair.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;7&lt;/code&gt; wasn't there, or if &lt;code&gt;2&lt;/code&gt; didn't lead to a pair, you'd move to the &lt;em&gt;next&lt;/em&gt; number in your list, say &lt;code&gt;7&lt;/code&gt;. Then you'd ask, "What number do I need to add to &lt;code&gt;7&lt;/code&gt; to get &lt;code&gt;9&lt;/code&gt;? &lt;code&gt;2&lt;/code&gt;!" You'd scan the &lt;em&gt;remaining&lt;/em&gt; numbers for &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This "pick one, then check all others" approach is exactly the core intuition for our first solution!&lt;/p&gt;


&lt;h3&gt;
  
  
  Our Approach: Brute Force (The Straightforward Way)
&lt;/h3&gt;

&lt;p&gt;The intuition above translates directly into a programming strategy called &lt;strong&gt;Brute Force&lt;/strong&gt;. It simply means trying every single possible combination until we find the one that works.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pick a First Number:&lt;/strong&gt; We'll use a loop to go through our &lt;code&gt;nums&lt;/code&gt; array, picking each number one by one. Let's say &lt;code&gt;nums[i]&lt;/code&gt; is the current "first number" we're considering.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Look for its Partner:&lt;/strong&gt; For each &lt;code&gt;nums[i]&lt;/code&gt;, we need to find another number &lt;code&gt;nums[j]&lt;/code&gt; in the &lt;em&gt;rest&lt;/em&gt; of the array such that &lt;code&gt;nums[i] + nums[j] == target&lt;/code&gt;. So, we'll use a &lt;em&gt;second&lt;/em&gt; loop, nested inside the first one, to iterate through all possible &lt;code&gt;nums[j]&lt;/code&gt; values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Crucial Check: Different Elements:&lt;/strong&gt; Remember the rule: "You may not use the same element twice." This means &lt;code&gt;i&lt;/code&gt; (the index of our first number) &lt;em&gt;must not&lt;/em&gt; be equal to &lt;code&gt;j&lt;/code&gt; (the index of our second number).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Found It!&lt;/strong&gt; If &lt;code&gt;nums[i] + nums[j]&lt;/code&gt; equals &lt;code&gt;target&lt;/code&gt; AND &lt;code&gt;i&lt;/code&gt; is not equal to &lt;code&gt;j&lt;/code&gt;, we've found our pair! We can immediately return their indices &lt;code&gt;[i, j]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the problem guarantees there's always exactly one solution, we know our loops will eventually find the correct pair and return.&lt;/p&gt;


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

&lt;p&gt;Let's translate that logic into C++ code.&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="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;&lt;/span&gt; &lt;span class="n"&gt;twoSum&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Outer loop: iterates through each number, considering it as the first element of our pair.&lt;/span&gt;
        &lt;span class="c1"&gt;// 'i' will represent the index of the first number.&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;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="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;// Inner loop: iterates through the remaining numbers to find a partner for 'nums[i]'.&lt;/span&gt;
            &lt;span class="c1"&gt;// We start 'j' from 'i' (or i+1 to optimize slightly and avoid i==j check, but the problem's&lt;/span&gt;
            &lt;span class="c1"&gt;// given solution starts j from i and explicitly checks i!=j, so we'll stick to that for now).&lt;/span&gt;
            &lt;span class="c1"&gt;// 'j' will represent the index of the second number.&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;j&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;j&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;j&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;// The provided solution started j from i; let's adhere to that to illustrate its exact logic.&lt;/span&gt;
                                                    &lt;span class="c1"&gt;// Correction: The provided solution has `j = i`, let's use that.&lt;/span&gt;
                                                    &lt;span class="c1"&gt;// No, the solution actually has `for(int j = i; j &amp;lt; nums.size(); j++)`.&lt;/span&gt;
                                                    &lt;span class="c1"&gt;// I will adapt the comments to fit this provided code, as requested.&lt;/span&gt;
                &lt;span class="c1"&gt;// Check if the sum of the current pair of numbers equals the target&lt;/span&gt;
                &lt;span class="c1"&gt;// AND ensure that we are not using the same element twice (i.e., their indices must be different).&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;j&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 both conditions are met, we've found our unique pair!&lt;/span&gt;
                    &lt;span class="c1"&gt;// Return their indices as a vector.&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;j&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="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// This line should technically never be reached because the problem guarantees&lt;/span&gt;
        &lt;span class="c1"&gt;// that exactly one solution always exists. It's good practice for function completion.&lt;/span&gt;
        &lt;span class="k"&gt;return&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;p&gt;&lt;strong&gt;Self-correction based on the provided solution:&lt;/strong&gt;&lt;br&gt;
The provided solution is &lt;code&gt;for(int j = i; j &amp;lt; nums.size(); j++)&lt;/code&gt;. This is a slight optimization because it avoids redundant checks (e.g., checking &lt;code&gt;(nums[0], nums[1])&lt;/code&gt; and then later &lt;code&gt;(nums[1], nums[0])&lt;/code&gt;). The &lt;code&gt;i != j&lt;/code&gt; check inside the &lt;code&gt;if&lt;/code&gt; statement correctly handles the "cannot use the same element twice" rule. I will adjust the code block to reflect the exact provided solution and refine the comments.&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="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;&lt;/span&gt; &lt;span class="n"&gt;twoSum&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Outer loop: 'i' represents the index of the first number we pick.&lt;/span&gt;
        &lt;span class="c1"&gt;// We iterate through each element in the 'nums' array.&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;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="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;// Inner loop: 'j' represents the index of the second number we pick.&lt;/span&gt;
            &lt;span class="c1"&gt;// We start 'j' from 'i'. This ensures we check all pairs without redundant checks&lt;/span&gt;
            &lt;span class="c1"&gt;// like (nums[0], nums[1]) and then (nums[1], nums[0]), as the latter will be covered.&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;j&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="n"&gt;j&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;j&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;// Check if the sum of the numbers at indices 'i' and 'j' equals the target.&lt;/span&gt;
                &lt;span class="c1"&gt;// ALSO, ensure that we are not using the same element twice.&lt;/span&gt;
                &lt;span class="c1"&gt;// The condition `i != j` is crucial because `j` starts from `i`,&lt;/span&gt;
                &lt;span class="c1"&gt;// so `i == j` would mean comparing an element with itself.&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;j&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 both conditions are met, we found our pair!&lt;/span&gt;
                    &lt;span class="c1"&gt;// Return their indices as a vector.&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;j&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="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// According to the problem constraints, this return statement should theoretically&lt;/span&gt;
        &lt;span class="c1"&gt;// never be reached, as there is always exactly one solution.&lt;/span&gt;
        &lt;span class="c1"&gt;// It's a placeholder for compilation purposes.&lt;/span&gt;
        &lt;span class="k"&gt;return&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;p&gt;This is a more accurate representation and explanation of the &lt;em&gt;provided&lt;/em&gt; solution.&lt;/p&gt;




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

&lt;p&gt;Understanding how efficient your code is, is a critical skill in competitive programming!&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;n&lt;/code&gt; represents the number of elements in our &lt;code&gt;nums&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;  Our solution uses &lt;strong&gt;nested loops&lt;/strong&gt;. The outer loop runs &lt;code&gt;n&lt;/code&gt; times. For each iteration of the outer loop, the inner loop runs roughly &lt;code&gt;n&lt;/code&gt; times (specifically, &lt;code&gt;n-i&lt;/code&gt; times).&lt;/li&gt;
&lt;li&gt;  This means, in the worst case, we are performing approximately &lt;code&gt;n * n&lt;/code&gt; operations. This kind of performance is called &lt;strong&gt;quadratic time complexity&lt;/strong&gt;, denoted as &lt;code&gt;O(n^2)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  The problem description actually gives you a hint for improvement: "Can you come up with an algorithm that is less than O(n^2) time complexity?" Our current solution isn't that, but it's a great starting point!&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 are only using a few extra variables (&lt;code&gt;i&lt;/code&gt;, &lt;code&gt;j&lt;/code&gt;, &lt;code&gt;target&lt;/code&gt;, &lt;code&gt;nums.size()&lt;/code&gt;) which take up a constant amount of memory, regardless of how large the &lt;code&gt;nums&lt;/code&gt; array gets.&lt;/li&gt;
&lt;li&gt;  We are not creating any new data structures that grow significantly with the input size. This is called &lt;strong&gt;constant space complexity&lt;/strong&gt;, denoted as &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;Start Simple (Brute Force):&lt;/strong&gt; Don't be afraid to implement the most straightforward solution first. It helps you understand the problem thoroughly and provides a baseline.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Nested Loops for Pairs:&lt;/strong&gt; When you need to check every possible pair in an array, nested loops are a common pattern.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Read Constraints Carefully:&lt;/strong&gt; Details like "cannot use the same element twice" (&lt;code&gt;i != j&lt;/code&gt;) and "exactly one solution" are crucial for correct and efficient problem-solving.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Complexity Matters:&lt;/strong&gt; &lt;code&gt;O(n^2)&lt;/code&gt; is often acceptable for small inputs, but for larger inputs, you'll need more optimized approaches. The "Two Sum" problem has a famous &lt;code&gt;O(n)&lt;/code&gt; solution using hash maps (dictionaries) – a fantastic next step once you master this basic approach!&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;That's it for our deep dive into LeetCode's Two Sum! You've just tackled your first (or refreshed your knowledge of) a fundamental problem. Keep practicing, keep learning, and happy coding!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; pratik_bandgar9009&lt;br&gt;
&lt;strong&gt;Published:&lt;/strong&gt; 2026-05-23 16:35:43&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>LeetCode Solution: 1. Two Sum — May 23 2026 16:35</title>
      <dc:creator>Pratik Bandgar</dc:creator>
      <pubDate>Sat, 23 May 2026 11:05:11 +0000</pubDate>
      <link>https://dev.to/pratik_bandgar_cce3b2e87a/leetcode-solution-1-two-sum-may-23-2026-1635-5hno</link>
      <guid>https://dev.to/pratik_bandgar_cce3b2e87a/leetcode-solution-1-two-sum-may-23-2026-1635-5hno</guid>
      <description>&lt;h1&gt;
  
  
  Two Sum: Your First LeetCode Adventure Begins! 🚀
&lt;/h1&gt;

&lt;p&gt;Hello, future problem-solvers and coding enthusiasts! 👋&lt;/p&gt;

&lt;p&gt;If you're just dipping your toes into the exciting world of LeetCode, congratulations! You've found yourself at the perfect starting point: &lt;strong&gt;Problem #1, "Two Sum."&lt;/strong&gt; This isn't just any problem; it's often the very first one many developers encounter, and for good reason. It's a fantastic introduction to algorithmic thinking, array manipulation, and understanding basic complexity.&lt;/p&gt;

&lt;p&gt;Don't worry if you're a beginner – we'll break it down step-by-step, making sure you grasp every concept. Let's dive in!&lt;/p&gt;




&lt;h2&gt;
  
  
  🧐 Problem Explanation: Find the Perfect Pair!
&lt;/h2&gt;

&lt;p&gt;Imagine you're at a party, and you have a list of people, each wearing a badge with a number on it. Your mission is to find &lt;em&gt;exactly two people&lt;/em&gt; whose numbers, when added together, equal a specific "target" number. Once you find them, you just need to tell us &lt;em&gt;where&lt;/em&gt; they are in the line (their positions/indices).&lt;/p&gt;

&lt;p&gt;Here's the formal description: ""&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt; An array of integers, &lt;code&gt;nums&lt;/code&gt;. (Think of this as your list of people with numbers).&lt;/li&gt;
&lt;li&gt; An integer, &lt;code&gt;target&lt;/code&gt;. (This is the sum you're trying to achieve).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt;&lt;br&gt;
Return the &lt;em&gt;indices&lt;/em&gt; (positions) of the two numbers in &lt;code&gt;nums&lt;/code&gt; that add up to &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Exactly one solution:&lt;/strong&gt; You're guaranteed to find one pair that works. No need to worry about multiple pairs or no pairs at all!&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;No duplicates:&lt;/strong&gt; You can't use the same number twice. 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;  &lt;strong&gt;Order doesn't matter:&lt;/strong&gt; &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 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;code&gt;[0, 1]&lt;/code&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  Why? 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;) equals &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;/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;code&gt;[1, 2]&lt;/code&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  Why? 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;) equals &lt;code&gt;6&lt;/code&gt;. We return &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;nums = [3, 3]&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;code&gt;[0, 1]&lt;/code&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  Why? Because &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;. Even though the numbers are the same, they are at &lt;em&gt;different indices&lt;/em&gt;, so it's a valid pair!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




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

&lt;p&gt;Okay, put your computer science hat aside for a moment. If you had to solve this manually with a small list of numbers and a target, how would you do it?&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt; You'd probably start with the first number, &lt;code&gt;2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Then, you'd ask: "If I use &lt;code&gt;2&lt;/code&gt;, what number do I need to reach &lt;code&gt;9&lt;/code&gt;?" The answer is &lt;code&gt;9 - 2 = 7&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Now, you'd scan the &lt;em&gt;rest&lt;/em&gt; of the list (&lt;code&gt;7, 11, 15&lt;/code&gt;) to see if &lt;code&gt;7&lt;/code&gt; exists.&lt;/li&gt;
&lt;li&gt; Aha! &lt;code&gt;7&lt;/code&gt; is right there at index &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; You found your pair: &lt;code&gt;2&lt;/code&gt; (at index &lt;code&gt;0&lt;/code&gt;) and &lt;code&gt;7&lt;/code&gt; (at index &lt;code&gt;1&lt;/code&gt;). Mission accomplished!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What if &lt;code&gt;2&lt;/code&gt; didn't work? You'd move to the next number, &lt;code&gt;7&lt;/code&gt;, and repeat the process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Take &lt;code&gt;7&lt;/code&gt;. What do I need? &lt;code&gt;9 - 7 = 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Scan the rest of the list (&lt;code&gt;11, 15&lt;/code&gt;) for &lt;code&gt;2&lt;/code&gt;. (Actually, you'd also scan previous elements, but need to be careful not to use the &lt;em&gt;same&lt;/em&gt; index).&lt;/li&gt;
&lt;li&gt; Wait, we already considered &lt;code&gt;7&lt;/code&gt; with &lt;code&gt;2&lt;/code&gt;. We don't want to re-check pairs we've already tried, or use &lt;code&gt;7&lt;/code&gt; with itself.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This "try every combination" idea is the core of our first approach!&lt;/p&gt;




&lt;h2&gt;
  
  
  🚶‍♂️ Approach: The Brute-Force Way
&lt;/h2&gt;

&lt;p&gt;The most straightforward way to find a pair is to simply check &lt;em&gt;every single possible pair&lt;/em&gt; of numbers in the array. This is often called the "brute-force" approach.&lt;/p&gt;

&lt;p&gt;Here's how we'll do it step-by-step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Pick a Number:&lt;/strong&gt; Start with the first number in the &lt;code&gt;nums&lt;/code&gt; array. Let's call its index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Pick Another Number:&lt;/strong&gt; Now, pick &lt;em&gt;another&lt;/em&gt; number from the array. This second number must be &lt;em&gt;after&lt;/em&gt; the first number (&lt;code&gt;i&lt;/code&gt;) to ensure we don't use the same element twice (e.g., &lt;code&gt;nums[i]&lt;/code&gt; and &lt;code&gt;nums[i]&lt;/code&gt;) and to avoid checking the same pair twice (e.g., checking &lt;code&gt;(nums[0], nums[1])&lt;/code&gt; and then later &lt;code&gt;(nums[1], nums[0])&lt;/code&gt;). Let's call its index &lt;code&gt;j&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Check the Sum:&lt;/strong&gt; Add &lt;code&gt;nums[i]&lt;/code&gt; and &lt;code&gt;nums[j]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Is it the Target?&lt;/strong&gt; If their sum equals &lt;code&gt;target&lt;/code&gt;, we've found our pair! We can immediately return their indices, &lt;code&gt;[i, j]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Keep Looking:&lt;/strong&gt; If it's not the target, we move on to the next possible &lt;code&gt;j&lt;/code&gt; for the current &lt;code&gt;i&lt;/code&gt;. If we run out of &lt;code&gt;j&lt;/code&gt;'s for the current &lt;code&gt;i&lt;/code&gt;, we move to the next &lt;code&gt;i&lt;/code&gt; and start checking &lt;code&gt;j&lt;/code&gt;'s from there.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since we are guaranteed that "exactly one solution exists," our loops will eventually find the pair and return.&lt;/p&gt;




&lt;h2&gt;
  
  
  💻 Code: Bringing the Brute Force to Life (C++)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;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;&lt;/span&gt; &lt;span class="n"&gt;twoSum&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Outer loop: iterates through each element with index 'i'&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;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="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;// Inner loop: iterates through each element with index 'j'&lt;/span&gt;
            &lt;span class="c1"&gt;// We start 'j' from 'i' to ensure we consider all combinations,&lt;/span&gt;
            &lt;span class="c1"&gt;// but carefully handle the "same element twice" constraint inside the loop.&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;j&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="n"&gt;j&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;j&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;// Check if the sum of nums[i] and nums[j] equals the target&lt;/span&gt;
                &lt;span class="c1"&gt;// AND ensure that 'i' and 'j' are not the same index.&lt;/span&gt;
                &lt;span class="c1"&gt;// The 'j = i' in the inner loop means we start checking from the current 'i'.&lt;/span&gt;
                &lt;span class="c1"&gt;// If we didn't add 'i != j', it would incorrectly sum an element with itself.&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;j&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 both conditions are met, we found our pair!&lt;/span&gt;
                    &lt;span class="c1"&gt;// Return their indices as a vector.&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;j&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="p"&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;// So, this line should ideally never be reached in a valid test case.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;// Return an empty vector if no solution is found (for safety, though not expected)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;A note on &lt;code&gt;j = i&lt;/code&gt; vs. &lt;code&gt;j = i + 1&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
You might see other solutions where the inner loop starts with &lt;code&gt;j = i + 1&lt;/code&gt;. Both are valid ways to prevent using the same element twice and avoid redundant checks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  If &lt;code&gt;j = i + 1&lt;/code&gt;, then &lt;code&gt;i != j&lt;/code&gt; is &lt;em&gt;always&lt;/em&gt; true, so you don't need &lt;code&gt;(i != j)&lt;/code&gt; in the &lt;code&gt;if&lt;/code&gt; condition. This can be slightly cleaner.&lt;/li&gt;
&lt;li&gt;  My provided code with &lt;code&gt;j = i&lt;/code&gt; and &lt;code&gt;(i != j)&lt;/code&gt; explicitly handles the case where &lt;code&gt;j&lt;/code&gt; &lt;em&gt;could&lt;/em&gt; be &lt;code&gt;i&lt;/code&gt;, but then discards it. It works perfectly fine!&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Understanding how "expensive" your code is in terms of time and memory is crucial in competitive programming.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Outer Loop:&lt;/strong&gt; The &lt;code&gt;for (int i = 0; i &amp;lt; nums.size(); i++)&lt;/code&gt; loop runs &lt;code&gt;n&lt;/code&gt; times, where &lt;code&gt;n&lt;/code&gt; is the number of elements in the &lt;code&gt;nums&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Inner Loop:&lt;/strong&gt; For each iteration of the outer loop, the &lt;code&gt;for (int j = i; j &amp;lt; nums.size(); j++)&lt;/code&gt; loop also runs up to &lt;code&gt;n&lt;/code&gt; times in the worst case (when &lt;code&gt;i&lt;/code&gt; is small).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Total Operations:&lt;/strong&gt; Since we have nested loops, for roughly every element &lt;code&gt;i&lt;/code&gt;, we are checking against every other element &lt;code&gt;j&lt;/code&gt;. This leads to approximately &lt;code&gt;n * n&lt;/code&gt; (or &lt;code&gt;n²&lt;/code&gt;) operations.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Big O Notation:&lt;/strong&gt; We express this as &lt;strong&gt;O(n²)&lt;/strong&gt;, which stands for "Order n-squared." This means the time taken grows quadratically with the input size. For small arrays, it's fine, but for very large arrays (e.g., &lt;code&gt;10^5&lt;/code&gt; elements), &lt;code&gt;10^10&lt;/code&gt; operations would be too slow!&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Extra Memory:&lt;/strong&gt; We are not using any additional data structures (like maps, hash sets, or new arrays) that grow in size with the input &lt;code&gt;n&lt;/code&gt;. We're just using a few variables (&lt;code&gt;i&lt;/code&gt;, &lt;code&gt;j&lt;/code&gt;, &lt;code&gt;target&lt;/code&gt;, &lt;code&gt;nums[i]&lt;/code&gt;, &lt;code&gt;nums[j]&lt;/code&gt;) to store temporary values.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Big O Notation:&lt;/strong&gt; We express this as &lt;strong&gt;O(1)&lt;/strong&gt;, which means "constant space." The amount of memory used does not depend on the size of the input array.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Brute Force is a Starting Point:&lt;/strong&gt; Often, the first solution you think of (like checking every possibility) is a brute-force approach. It might not be the most efficient, but it's great for getting started and understanding the problem.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Nested Loops = O(n²):&lt;/strong&gt; A common pattern: if you see nested &lt;code&gt;for&lt;/code&gt; loops where each loop iterates over the input data, it's a strong indicator of O(n²) time complexity.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Constraints Matter:&lt;/strong&gt; The problem statement's guarantee of "exactly one solution" simplifies things, as we don't need to handle cases where no pair is found.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;The Follow-Up is a Hint:&lt;/strong&gt; LeetCode problems often include "Follow-up" questions (like "Can you come up with an algorithm that is less than O(n²) time complexity?"). This is a clear invitation to think about optimizations! For "Two Sum," there's a much faster O(n) solution using hash maps/dictionaries – but that's a story for another blog post! 😉&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You've just tackled your first LeetCode problem with a solid, understandable solution! This is a fantastic step on your coding journey. Keep practicing, keep learning, and happy coding!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Submission Details:&lt;/strong&gt;&lt;br&gt;
Author Account: pratik_bandgar9009&lt;br&gt;
Time Published: 2026-05-23 16:34:50&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>Pratik Bandgar</dc:creator>
      <pubDate>Sat, 23 May 2026 10:58:44 +0000</pubDate>
      <link>https://dev.to/pratik_bandgar_cce3b2e87a/leetcode-solution-1-two-sum-14om</link>
      <guid>https://dev.to/pratik_bandgar_cce3b2e87a/leetcode-solution-1-two-sum-14om</guid>
      <description>&lt;h1&gt;
  
  
  Two Sum: Your First LeetCode Challenge &amp;amp; Why It Matters!
&lt;/h1&gt;

&lt;p&gt;Hello, future coding rockstars! 👋 Are you ready to embark on your LeetCode journey? There's no better place to start than with problem number one: &lt;strong&gt;Two Sum&lt;/strong&gt;. This isn't just any problem; it's a rite of passage, a classic, and a fantastic way to grasp fundamental algorithmic thinking.&lt;/p&gt;

&lt;p&gt;Let's dive in and conquer it together!&lt;/p&gt;

&lt;h2&gt;
  
  
  🤝 Problem Explanation: Finding the Perfect Pair
&lt;/h2&gt;

&lt;p&gt;Imagine you have a list of numbers, like &lt;code&gt;[2, 7, 11, 15]&lt;/code&gt;. Now, imagine I give you a &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 two different numbers in that list that, when added together, equal our &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But there's a twist! You don't return the numbers themselves; you return their &lt;em&gt;positions&lt;/em&gt; (their indices) in the list.&lt;/p&gt;

&lt;p&gt;Let's look at our example:&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;  &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;  &lt;code&gt;11&lt;/code&gt; is at index &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;15&lt;/code&gt; is at index &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Can you spot the pair that adds up to &lt;code&gt;9&lt;/code&gt;?&lt;br&gt;
Bingo! &lt;code&gt;2 + 7 = 9&lt;/code&gt;.&lt;br&gt;
Since &lt;code&gt;2&lt;/code&gt; is at index &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;7&lt;/code&gt; is at index &lt;code&gt;1&lt;/code&gt;, our answer would be &lt;code&gt;[0, 1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A few crucial points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Exactly one solution:&lt;/strong&gt; You'll always find one perfect pair. No need to worry about multiple pairs or no pair at all.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Don't use the same element twice:&lt;/strong&gt; If &lt;code&gt;nums = [3, 3]&lt;/code&gt;, &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;em&gt;other&lt;/em&gt; &lt;code&gt;3&lt;/code&gt; at index &lt;code&gt;1&lt;/code&gt;. You can't use &lt;code&gt;nums[0]&lt;/code&gt; with &lt;code&gt;nums[0]&lt;/code&gt; to make &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Any order:&lt;/strong&gt; &lt;code&gt;[0, 1]&lt;/code&gt; or &lt;code&gt;[1, 0]&lt;/code&gt; are both valid for our example.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🤔 The Intuition: The Brute-Force Detective
&lt;/h2&gt;

&lt;p&gt;How would you, a human, solve this if you had a small list of numbers and a target?&lt;/p&gt;

&lt;p&gt;You'd probably pick the first number, say &lt;code&gt;2&lt;/code&gt;. Then, you'd look at &lt;em&gt;every other number&lt;/em&gt; in the list (&lt;code&gt;7&lt;/code&gt;, &lt;code&gt;11&lt;/code&gt;, &lt;code&gt;15&lt;/code&gt;) and ask: "Does &lt;code&gt;2&lt;/code&gt; plus any of these equal &lt;code&gt;9&lt;/code&gt;?"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;2 + 7 = 9&lt;/code&gt;? Yes! Found it!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If &lt;code&gt;2&lt;/code&gt; didn't work, you'd move to the next number, &lt;code&gt;7&lt;/code&gt;. Then you'd check &lt;code&gt;7&lt;/code&gt; against all subsequent numbers, and so on. This "check every possible pair" strategy is what we call &lt;strong&gt;brute force&lt;/strong&gt;. It's straightforward, and it's often the first idea that comes to mind for many problems.&lt;/p&gt;
&lt;h2&gt;
  
  
  🚶‍♂️ Our Approach: The Nested Loop Saga
&lt;/h2&gt;

&lt;p&gt;To translate our human intuition into code, we need to systematically check every possible pair. How do we do that? With &lt;strong&gt;nested loops&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Here's the step-by-step breakdown of how the provided solution works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Outer Loop (Picking the First Number):&lt;/strong&gt; We start with a loop that iterates through each number in our &lt;code&gt;nums&lt;/code&gt; array. Let's say this loop uses an index &lt;code&gt;i&lt;/code&gt;. This &lt;code&gt;i&lt;/code&gt; will represent the index of our &lt;em&gt;first&lt;/em&gt; potential number.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For i from 0 to (length of nums - 1):
    Let current_num_1 = nums[i]
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inner Loop (Finding the Partner):&lt;/strong&gt; Inside our first loop, we start &lt;em&gt;another&lt;/em&gt; loop. This inner loop will iterate through the remaining numbers in &lt;code&gt;nums&lt;/code&gt;, looking for a partner for &lt;code&gt;nums[i]&lt;/code&gt;. Let's say this loop uses an index &lt;code&gt;j&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;*   Crucially, `j` starts from `i`. Why `i`? Because we want to check numbers from the current position onwards.
*   To ensure we don't use the *same element twice* (as required by the problem), we'll add a condition: `i != j`. This makes sure we're always comparing two distinct positions.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```
For j from i to (length of nums - 1):
    Let current_num_2 = nums[j]
```
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Check:&lt;/strong&gt; Inside the inner loop, after we've picked &lt;code&gt;nums[i]&lt;/code&gt; and &lt;code&gt;nums[j]&lt;/code&gt;, we perform our core check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Is &lt;code&gt;nums[i] + nums[j]&lt;/code&gt; equal to our &lt;code&gt;target&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;  AND, is &lt;code&gt;i&lt;/code&gt; not equal to &lt;code&gt;j&lt;/code&gt; (to make sure we're using two different elements)?
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;If (current_num_1 + current_num_2 == target) AND (i != j):
    We found our pair! Return the indices [i, j].
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Return (The Unlikely Scenario):&lt;/strong&gt; According to the problem constraints, we are guaranteed to find exactly one solution. So, the code will always return &lt;code&gt;[i, j]&lt;/code&gt; inside the loops and never reach the &lt;code&gt;return {};&lt;/code&gt; statement at the end. It's good practice to have a fallback return, but in this specific problem, it's theoretically unreachable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's walk through &lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;, &lt;code&gt;target = 9&lt;/code&gt; again with this approach:&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;j&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;nums[j]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;i != j&lt;/code&gt;?&lt;/th&gt;
&lt;th&gt;&lt;code&gt;nums[i] + nums[j]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;== target&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;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Skip&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&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;True&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Yes!&lt;/td&gt;
&lt;td&gt;Return &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;And that's it! We found the solution on the very first "real" check.&lt;/p&gt;

&lt;h2&gt;
  
  
  💻 Code: The Brute-Force in C++
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;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;&lt;/span&gt; &lt;span class="n"&gt;twoSum&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Outer loop iterates through each number from the start&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;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="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;// Inner loop iterates through numbers starting from the current 'i'&lt;/span&gt;
            &lt;span class="c1"&gt;// This ensures we check all pairs without repeating (e.g., (2,7) vs (7,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;j&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="n"&gt;j&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;j&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;// Check if the sum equals the target AND&lt;/span&gt;
                &lt;span class="c1"&gt;// if we are not using the same element twice (i.e., different indices)&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;j&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 both conditions are met, we found our pair!&lt;/span&gt;
                    &lt;span class="c1"&gt;// Return their indices in a vector.&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;j&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="p"&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;// so this line should theoretically never be reached.&lt;/span&gt;
        &lt;span class="k"&gt;return&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;Understanding how efficient your code is, is crucial for competitive programming and software development.&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 have two nested loops.&lt;/li&gt;
&lt;li&gt;  The outer loop runs &lt;code&gt;n&lt;/code&gt; times (where &lt;code&gt;n&lt;/code&gt; is the number of elements in &lt;code&gt;nums&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  The inner loop, in the worst case, also runs &lt;code&gt;n&lt;/code&gt; times for each iteration of the outer loop.&lt;/li&gt;
&lt;li&gt;  So, &lt;code&gt;n * n = n²&lt;/code&gt; operations. This is generally considered a "slower" approach for larger inputs, but it's a perfectly valid starting point.&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 are not using any extra data structures that grow with the input size &lt;code&gt;n&lt;/code&gt;. We only use a few variables (&lt;code&gt;i&lt;/code&gt;, &lt;code&gt;j&lt;/code&gt;, &lt;code&gt;nums[i]&lt;/code&gt;, &lt;code&gt;nums[j]&lt;/code&gt;) to store current values.&lt;/li&gt;
&lt;li&gt;  This means our solution uses a constant amount of extra memory, regardless of how large the input array &lt;code&gt;nums&lt;/code&gt; is. Pretty efficient on memory!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Brute Force is a Starting Point:&lt;/strong&gt; Don't be afraid to start with the most straightforward approach that comes to mind. It's a great way to understand the problem and get &lt;em&gt;a&lt;/em&gt; solution working.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Nested Loops for Pairwise Checks:&lt;/strong&gt; When you need to check every combination of two elements in a list, nested loops are your go-to pattern.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Complexity Matters:&lt;/strong&gt; Always think about how your solution scales. O(n²) might be fine for small inputs, but for larger datasets, you'll often need something faster.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Read Constraints Carefully:&lt;/strong&gt; "Exactly one solution" and "not use the same element twice" are critical details that guide your logic.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This problem has a famous "Follow-up" asking for an algorithm less than O(n²) time complexity. This is where Hash Maps (or Dictionaries in Python) shine, allowing you to solve it in O(n) time! But that's a story for another day, once you've mastered the basics!&lt;/p&gt;

&lt;p&gt;Congratulations on tackling your first LeetCode problem! Keep coding, keep learning, and you'll be solving complex algorithms in no time.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Authored by &lt;a href="https://dev.to/pratik_bandgar9009"&gt;pratik_bandgar9009&lt;/a&gt; | Published: 2026-05-23 16:28:17&lt;/em&gt;&lt;/p&gt;

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