<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://robinali34.github.io/blog_leetcode_rust/feed.xml" rel="self" type="application/atom+xml" /><link href="https://robinali34.github.io/blog_leetcode_rust/" rel="alternate" type="text/html" /><updated>2026-09-23T05:37:47+00:00</updated><id>https://robinali34.github.io/blog_leetcode_rust/feed.xml</id><title type="html">Robina Li</title><subtitle>Technical Blog - Exploring algorithms, data structures, and software engineering insights (Rust)</subtitle><entry><title type="html">[Easy] 1. Two Sum</title><link href="https://robinali34.github.io/blog_leetcode_rust/2026/09/22/easy-1-two-sum/" rel="alternate" type="text/html" title="[Easy] 1. Two Sum" /><published>2026-09-22T00:00:00+00:00</published><updated>2026-09-22T00:00:00+00:00</updated><id>https://robinali34.github.io/blog_leetcode_rust/2026/09/22/easy-1-two-sum</id><content type="html" xml:base="https://robinali34.github.io/blog_leetcode_rust/2026/09/22/easy-1-two-sum/"><![CDATA[<p>Given an array of integers <code class="language-plaintext highlighter-rouge">nums</code> and an integer <code class="language-plaintext highlighter-rouge">target</code>, return <em>the indices of the two numbers such that they add up to <code class="language-plaintext highlighter-rouge">target</code></em>.</p>

<p>You may assume that each input has <strong>exactly one solution</strong>, and you may not use the same element twice.</p>

<p>You can return the answer in any order.</p>

<blockquote>
  <p><strong>Pattern:</strong> Hash map complementary lookup</p>
</blockquote>

<h2 id="examples">Examples</h2>

<p><strong>Example 1:</strong></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
</code></pre></div></div>

<p><strong>Example 2:</strong></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Input: nums = [3,2,4], target = 6
Output: [1,2]
</code></pre></div></div>

<p><strong>Example 3:</strong></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Input: nums = [3,3], target = 6
Output: [0,1]
</code></pre></div></div>

<h2 id="constraints">Constraints</h2>

<ul>
  <li><code class="language-plaintext highlighter-rouge">2 &lt;= nums.length &lt;= 10^4</code></li>
  <li><code class="language-plaintext highlighter-rouge">-10^9 &lt;= nums[i] &lt;= 10^9</code></li>
  <li><code class="language-plaintext highlighter-rouge">-10^9 &lt;= target &lt;= 10^9</code></li>
  <li>Only one valid answer exists.</li>
</ul>

<h2 id="common-approaches">Common Approaches</h2>

<table>
  <thead>
    <tr>
      <th>Approach</th>
      <th>Time</th>
      <th>Space</th>
      <th>Notes</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><strong>Hash map of index</strong> <em>(this problem)</em></td>
      <td>O(n)</td>
      <td>O(n)</td>
      <td>Store value → index while scanning</td>
    </tr>
    <tr>
      <td>Two pointers on a sorted copy</td>
      <td>O(n log n)</td>
      <td>O(n)</td>
      <td>Must keep original indices</td>
    </tr>
    <tr>
      <td>Brute force</td>
      <td>O(n²)</td>
      <td>O(1)</td>
      <td>Check every pair</td>
    </tr>
  </tbody>
</table>

<h2 id="thinking-process">Thinking Process</h2>

<p>For each value <code class="language-plaintext highlighter-rouge">x</code> at index <code class="language-plaintext highlighter-rouge">j</code>, the complement is <code class="language-plaintext highlighter-rouge">target - x</code>. If we have already seen that complement, we are done. Otherwise remember <code class="language-plaintext highlighter-rouge">x</code> at <code class="language-plaintext highlighter-rouge">j</code>.</p>

<ul>
  <li>Signal: two numbers that add up to a target on an unsorted array</li>
  <li>Key idea: one pass, hash map of values already seen</li>
  <li>Edge cases: the complement at index <code class="language-plaintext highlighter-rouge">0</code>; duplicates (<code class="language-plaintext highlighter-rouge">[3,3]</code>, target <code class="language-plaintext highlighter-rouge">6</code>); negatives</li>
</ul>

<h2 id="solution--on-time-on-space">Solution — O(n) time, O(n) space</h2>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">use</span> <span class="nn">std</span><span class="p">::</span><span class="nn">collections</span><span class="p">::</span><span class="n">HashMap</span><span class="p">;</span>

<span class="k">impl</span> <span class="n">Solution</span> <span class="p">{</span>
    <span class="k">pub</span> <span class="k">fn</span> <span class="nf">two_sum</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="nb">Vec</span><span class="o">&lt;</span><span class="nb">i32</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="nb">i32</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="nb">Vec</span><span class="o">&lt;</span><span class="nb">i32</span><span class="o">&gt;</span> <span class="p">{</span>
        <span class="k">let</span> <span class="k">mut</span> <span class="n">idx</span> <span class="o">=</span> <span class="nn">HashMap</span><span class="p">::</span><span class="nf">new</span><span class="p">();</span>
        <span class="k">for</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">)</span> <span class="k">in</span> <span class="n">nums</span><span class="nf">.iter</span><span class="p">()</span><span class="nf">.enumerate</span><span class="p">()</span> <span class="p">{</span>
            <span class="k">if</span> <span class="k">let</span> <span class="nf">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="n">i</span><span class="p">)</span> <span class="o">=</span> <span class="n">idx</span><span class="nf">.get</span><span class="p">(</span><span class="o">&amp;</span><span class="p">(</span><span class="n">target</span> <span class="o">-</span> <span class="n">x</span><span class="p">))</span> <span class="p">{</span>
                <span class="k">return</span> <span class="nd">vec!</span><span class="p">[</span><span class="n">i</span> <span class="k">as</span> <span class="nb">i32</span><span class="p">,</span> <span class="n">j</span> <span class="k">as</span> <span class="nb">i32</span><span class="p">];</span>
            <span class="p">}</span>
            <span class="n">idx</span><span class="nf">.insert</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">j</span><span class="p">);</span>
        <span class="p">}</span>
        <span class="nd">unreachable!</span><span class="p">()</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="solution-explanation">Solution Explanation</h3>

<p>Walk <code class="language-plaintext highlighter-rouge">nums</code> once. <code class="language-plaintext highlighter-rouge">idx</code> maps a value to the index where we last saw it.</p>

<p>At index <code class="language-plaintext highlighter-rouge">j</code> with value <code class="language-plaintext highlighter-rouge">x</code>, look up <code class="language-plaintext highlighter-rouge">target - x</code>. If that index <code class="language-plaintext highlighter-rouge">i</code> exists, <code class="language-plaintext highlighter-rouge">[i, j]</code> is the pair. If not, record <code class="language-plaintext highlighter-rouge">idx[x] = j</code> and continue.</p>

<p>Check <code class="language-plaintext highlighter-rouge">unless i.nil?</code> rather than <code class="language-plaintext highlighter-rouge">if i</code>, because index <code class="language-plaintext highlighter-rouge">0</code> is falsey in Ruby.</p>

<p><strong>Walkthrough</strong> — <code class="language-plaintext highlighter-rouge">nums = [2,7,11,15]</code>, <code class="language-plaintext highlighter-rouge">target = 9</code>:</p>

<table>
  <thead>
    <tr>
      <th>j</th>
      <th>x</th>
      <th><code class="language-plaintext highlighter-rouge">target - x</code></th>
      <th><code class="language-plaintext highlighter-rouge">idx</code> before</th>
      <th>action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>2</td>
      <td>7</td>
      <td><code class="language-plaintext highlighter-rouge">{}</code></td>
      <td>miss, set <code class="language-plaintext highlighter-rouge">idx[2] = 0</code></td>
    </tr>
    <tr>
      <td>1</td>
      <td>7</td>
      <td>2</td>
      <td><code class="language-plaintext highlighter-rouge">{2=&gt;0}</code></td>
      <td>hit <code class="language-plaintext highlighter-rouge">i = 0</code>, return <code class="language-plaintext highlighter-rouge">[0, 1]</code></td>
    </tr>
  </tbody>
</table>

<p><strong>Time:</strong> O(n) · <strong>Space:</strong> O(n)</p>

<h2 id="common-mistakes">Common Mistakes</h2>

<ul>
  <li>Recording <code class="language-plaintext highlighter-rouge">idx[x] = j</code> before the lookup — that matches an element with itself when <code class="language-plaintext highlighter-rouge">2 * x == target</code></li>
  <li>Returning values instead of indices</li>
  <li>Using <code class="language-plaintext highlighter-rouge">return [i, j] if i</code> — fails when the complement lives at index <code class="language-plaintext highlighter-rouge">0</code></li>
</ul>

<h2 id="key-takeaways">Key Takeaways</h2>

<ul>
  <li>Unsorted two-sum is a hash map problem, not two pointers</li>
  <li>Store the complement you still need, keyed by value, with the index as the payload</li>
</ul>

<h2 id="related-problems">Related Problems</h2>

<ul>
  <li><a href="https://www.leetcode.com/problems/3sum/">LC 15: 3Sum</a></li>
  <li><a href="https://www.leetcode.com/problems/two-sum-ii-input-array-is-sorted/">LC 167: Two Sum II - Input Array Is Sorted</a></li>
  <li><a href="https://www.leetcode.com/problems/contains-duplicate/">LC 217: Contains Duplicate</a></li>
  <li><a href="https://www.leetcode.com/problems/subarray-sum-equals-k/">LC 560: Subarray Sum Equals K</a></li>
</ul>

<h2 id="references">References</h2>

<ul>
  <li><a href="https://www.leetcode.com/problems/two-sum/">LC 1: Two Sum on LeetCode</a></li>
  <li><a href="https://www.leetcode.com/problems/two-sum/discuss/">LeetCode Discuss</a></li>
  <li><a href="https://www.leetcode.com/problems/two-sum/editorial/">LeetCode Editorial</a></li>
</ul>]]></content><author><name></name></author><category term="leetcode" /><category term="easy" /><category term="array" /><category term="hash" /><category term="ruby" /><category term="leetcode" /><category term="easy" /><category term="array" /><category term="hash" /><category term="two-sum" /><summary type="html"><![CDATA[Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target.]]></summary></entry></feed>