<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by M Ssaid on Medium]]></title>
        <description><![CDATA[Stories by M Ssaid on Medium]]></description>
        <link>https://medium.com/@m.ssaid356?source=rss-6aadfa2171a3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*83uhUqX_Ur74fDQqYSgIcA.png</url>
            <title>Stories by M Ssaid on Medium</title>
            <link>https://medium.com/@m.ssaid356?source=rss-6aadfa2171a3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 00:21:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@m.ssaid356/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[LeetCode 1: Two Sum — Explained for Beginners (With C# )]]></title>
            <link>https://medium.com/@m.ssaid356/leetcode-1-two-sum-explained-for-beginners-with-c-bcdc0ce1b3b1?source=rss-6aadfa2171a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcdc0ce1b3b1</guid>
            <category><![CDATA[coding-interview-problems]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[problems]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[practice]]></category>
            <dc:creator><![CDATA[M Ssaid]]></dc:creator>
            <pubDate>Wed, 18 Jun 2025 22:13:12 GMT</pubDate>
            <atom:updated>2025-06-18T22:15:43.368Z</atom:updated>
            <content:encoded><![CDATA[<h3>LeetCode 1: Two Sum — Explained for Beginners (With C# )</h3><p><em>By Mostafa Said — Self-taught Developer | .NET &amp; C# Enthusiast</em></p><h3>🚀 Introduction</h3><p>As a self-taught developer diving deep into <strong>LeetCode</strong> to strengthen my <strong>C# coding skills</strong>, the <strong>“Two Sum”</strong> problem was one of the first challenges I tackled. It’s deceptively simple — but packed with fundamental problem-solving concepts like hash maps, loops, and conditional logic.</p><p>This post walks you through how to <strong>solve the Two Sum problem in C#</strong>, with a visual breakdown and beginner-friendly reasoning.</p><h3>📌 Problem Statement</h3><blockquote><em>Given an array of integers </em><em>nums and an integer </em><em>target, return </em><strong><em>indices</em></strong><em> of the two numbers such that they add up to </em><em>target.</em></blockquote><h3>❗ Constraints:</h3><ul><li>Each input has <strong>exactly one solution</strong></li><li>You <strong>cannot use the same element twice</strong></li><li>Return the <strong>indices</strong>, not the actual numbers</li></ul><h3>❓ Why Not Use Two Pointers?</h3><p>Many beginner C# developers think of using two pointers. But here’s the catch:</p><blockquote><em>❌ The array is not sorted — two pointers only work efficiently on </em><strong><em>sorted arrays</em></strong><em>.</em></blockquote><p>So sorting is off the table too because:</p><ul><li>It changes the <strong>original indices</strong>, which we need for the return value.</li></ul><h3>💡 Optimal Approach: Use a Hash Map</h3><p>Instead of brute-forcing all pairs, use a <strong>dictionary (hash map)</strong> to store each number’s index while traversing the array.</p><h3>🔁 Key Insight:</h3><p>If num1 + num2 = target,<br> then target - num1 = num2.</p><p>So during the iteration:</p><ul><li>For each number, calculate diff = target - currentNum</li><li>Check if diff already exists in the map</li><li>If yes → You found the pair!</li></ul><h3>🧠 Step-by-Step Algorithm (in Plain English)</h3><ol><li>Initialize an empty Dictionary&lt;int, int&gt; to map numbers to their indices.</li><li>Loop through the array.</li><li>For each element:</li></ol><ul><li>Calculate the difference = target - currentNum</li><li>If the dictionary contains that difference, return both indices.</li><li>If not, store the current number and its index in the dictionary.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PD3kKlTrQHNKlsKZWFr2TA.png" /><figcaption>Two Sum problem in C# using a hash map</figcaption></figure><h3>👨‍💻 C# Code Implementation</h3><pre>public int[] TwoSum(int[] nums, int target) {<br>    var map = new Dictionary&lt;int, int&gt;();</pre><pre>for (int i = 0; i &lt; nums.Length; i++) {<br>        int diff = target - nums[i];<br>        if (map.ContainsKey(diff)) {<br>            return new int[] { map[diff], i };<br>        }<br>        if (!map.ContainsKey(nums[i])) {<br>            map[nums[i]] = i;<br>        }<br>    }<br>    return new int[0]; // Not expected to reach here due to constraints<br>}</pre><p>✅ This solution runs in <strong>O(n)</strong> time and <strong>O(n)</strong> space — optimal for large inputs.</p><h3>📊 Visual Diagram</h3><p><em>(Insert this visual or create your own using draw.io, Figma, or Canva)</em></p><p><strong>Title:</strong> “Two Sum in Action — C# Hash Map Approach”</p><p><strong>Diagram Idea:</strong></p><ul><li>Show an array [2, 7, 11, 15]</li><li>Highlight current index and target = 9</li><li>Visually track:</li><li>Calculating 9 - 2 = 7</li><li>Adding 2 to map</li><li>Next element 7 → found in map → return indices [0, 1]</li></ul><h3>🧩 Why This is a Great Beginner Problem</h3><ul><li>Teaches you about <strong>hash maps</strong></li><li>Encourages thinking in <strong>time-space tradeoffs</strong></li><li>Appears in <strong>real-world coding interviews</strong> frequently</li><li>Great intro to <strong>algorithmic patterns</strong> (like lookup-based solutions)</li></ul><h3>📘 Key Takeaways</h3><ul><li>Use dictionaries in C# for <strong>constant-time lookups</strong></li><li>Avoid sorting if <strong>indices</strong> matter</li><li>Practice writing and explaining your logic — not just coding it</li></ul><h3>🧠 Bonus Tip</h3><p>Before jumping into code, <strong>write out your thought process</strong> in comments or a journal. It’ll make debugging and learning much easier.</p><h3>🙌 Conclusion</h3><p>Solving the <strong>Two Sum</strong> problem in C# gave me a much clearer understanding of algorithmic thinking. If you’re just starting out with <strong>LeetCode and .NET</strong>, I highly recommend you take your time with these early problems — they build a strong foundation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcdc0ce1b3b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving Two Sum in C# — Performance Tips from Brute Force to Optimal Hash Map]]></title>
            <link>https://medium.com/@m.ssaid356/solving-two-sum-in-c-performance-tips-from-brute-force-to-optimal-hash-map-47de2e7fa0fc?source=rss-6aadfa2171a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/47de2e7fa0fc</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[coding-interviews]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[M Ssaid]]></dc:creator>
            <pubDate>Wed, 18 Jun 2025 20:17:52 GMT</pubDate>
            <atom:updated>2025-06-18T20:24:52.007Z</atom:updated>
            <content:encoded><![CDATA[<h3>Solving Two Sum in C# — Performance Tips from Brute Force to Optimal Hash Map</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U0k-QUWPWkoCccfCfj1tkA.png" /></figure><h3>Problem Description</h3><p>Given an integer array nums and an integer target, return the <strong>indices</strong> of the two numbers whose sum equals target. Assume exactly one solution exists, and you can&#39;t use the same element twice:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*naVeAEOa8mTcOcDvp9SZpA.png" /></figure><pre>public int[] TwoSum(int[] nums, int target)</pre><p><strong>Examples:</strong></p><pre>nums = [2,7,11,15], target = 9   =&gt; [0,1]  <br>nums = [3,2,4],    target = 6   =&gt; [1,2]  <br>nums = [3,3],      target = 6   =&gt; [0,1]</pre><h3>1️⃣ Brute‑Force (Nested Loops)</h3><pre>public int[] TwoSumBrute(int[] nums, int target) {<br>    for (int i = 0; i &lt; nums.Length; i++) {<br>        for (int j = i + 1; j &lt; nums.Length; j++) {<br>            if (nums[i] + nums[j] == target)<br>                return new int[]{ i, j };<br>        }<br>    }<br>    return Array.Empty&lt;int&gt;();<br>}</pre><ul><li>⏱️ <strong>Time Complexity:</strong> O(n²) — checks every pair</li><li>💾 <strong>Space Complexity:</strong> O(1) — no extra data structures</li><li>✅ <strong>Use Case:</strong> Good for tiny inputs and clarity <a href="https://medium.com/%40AlexanderObregon/solving-the-two-sum-problem-on-leetcode-c-answers-walkthrough-8097f4ae3352?utm_source=chatgpt.com">reddit.com+12medium.com+12medium.com+12</a><a href="https://medium.com/%40JPtheProgrammer/solving-the-two-sum-problem-in-c-a-step-by-step-guide-5dfd048e3e98?utm_source=chatgpt.com">medium.com</a><a href="https://www.reddit.com/r/developersIndia/comments/1huvzao/i_have_taken_100_sde_interviews_here_is_my_take/?utm_source=chatgpt.com">reddit.com+2reddit.com+2reddit.com+2</a><a href="https://www.reddit.com/r/csharp/comments/1amnc84/leetcode_two_sum_brute_force_faster_than/?utm_source=chatgpt.com">reddit.com+1reddit.com+1</a></li></ul><h3>2️⃣ Two‑Pass Hash Map</h3><pre>public int[] TwoSumTwoPass(int[] nums, int target) {<br>    var map = new Dictionary&lt;int,int&gt;(nums.Length);<br>    for (int i = 0; i &lt; nums.Length; i++)<br>        map[nums[i]] = i;</pre><pre>    for (int i = 0; i &lt; nums.Length; i++) {<br>        int comp = target - nums[i];<br>        if (map.TryGetValue(comp, out int j) &amp;&amp; j != i)<br>            return new int[]{ i, j };<br>    }<br>    return Array.Empty&lt;int&gt;();<br>}</pre><ul><li>⏱️ <strong>Time Complexity:</strong> O(n) — two linear passes</li><li>💾 <strong>Space Complexity:</strong> O(n) — stores entire array in dictionary <a href="https://medium.com/%40AlexanderObregon/solving-the-two-sum-problem-on-leetcode-c-answers-walkthrough-8097f4ae3352?utm_source=chatgpt.com">medium.com</a></li></ul><h3>3️⃣ One‑Pass Hash Map (Optimal)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kG4Rfi0q37LGepOBh5bd9g.png" /></figure><pre>public int[] TwoSumOnePass(int[] nums, int target) {<br>    var map = new Dictionary&lt;int,int&gt;(nums.Length);<br>    for (int i = 0; i &lt; nums.Length; i++) {<br>        int comp = target - nums[i];<br>        if (map.TryGetValue(comp, out int idx))<br>            return new int[]{ idx, i };<br>        map[nums[i]] = i;<br>    }<br>    return Array.Empty&lt;int&gt;();<br>}</pre><ul><li>⏱️ <strong>Time Complexity:</strong> O(n) — single-pass with dictionary lookup</li><li>💾 <strong>Space Complexity:</strong> O(n) — storage for seen elements</li></ul><h3>📈 Real‑World Benchmark Insights</h3><p>An interesting benchmark from Reddit showed surprising results: for small or pre-arranged datasets, the brute force approach outperformed both two-pass and one-pass hash map methods — even though its theoretical complexity is worse <a href="https://www.reddit.com/r/csharp/comments/1amnc84/leetcode_two_sum_brute_force_faster_than/?utm_source=chatgpt.com">reddit.com+6reddit.com+6reddit.com+6</a>.</p><blockquote><em>“…the brute force solution outperforms the Dictionary solution… even with large data sets (502 elements), but only when the matching pair occurs early.” </em><a href="https://www.reddit.com/r/csharp/comments/1amnc84/leetcode_two_sum_brute_force_faster_than/?utm_source=chatgpt.com"><em>reddit.com</em></a></blockquote><p>However, when reordered or fully randomized, the hash map variants consistently win out, showcasing their superior scaling for larger inputs .</p><h3>🧩 Trade-Off Summary</h3><p>MethodTimeSpaceIdeal ScenarioBrute ForceO(n²)O(1)Tiny arrays, predictable positions, extreme clarityTwo‑Pass Hash MapO(n)O(n)Easy-to-read, two-step logicOne‑Pass Hash MapO(n)O(n)Interview-optimal, efficient single-pass solution</p><h3>🧠 Extensions &amp; Challenges</h3><ul><li><strong>Return all unique pairs</strong>: handle duplicates (use HashSet or sorting)</li><li><strong>Stream-friendly variant</strong>: process input in chunks without storing the whole array</li><li><strong>Space-constrained: Two-pointer</strong>: sort to avoid extra dictionary, then map original indices</li><li><strong>Benchmark it!</strong> Use BenchmarkDotNet to evaluate differently ordered inputs</li></ul><h3>✅ Final Thoughts</h3><ul><li>Don’t dismiss the brute-force method — it’s cleaner and may outperform smarter solutions on small datasets.</li><li>But for real-world scale and interviews, the one-pass hash map is tried, tested, and fast.</li><li>Understanding data patterns and worst-case scenarios is crucial for picking the right tool.</li></ul><p><strong>Tags:</strong> C#, LeetCode, Algorithms, DSA, Coding Interview</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47de2e7fa0fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>