<?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: Youssef Shaker</title>
    <description>The latest articles on DEV Community by Youssef Shaker (@joeshak).</description>
    <link>https://dev.to/joeshak</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%2F2448208%2F01201644-0e91-4246-a8d5-5f519dc59c9f.jpeg</url>
      <title>DEV Community: Youssef Shaker</title>
      <link>https://dev.to/joeshak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joeshak"/>
    <language>en</language>
    <item>
      <title>Crushing Subarray Sum Equals K 🧮💥</title>
      <dc:creator>Youssef Shaker</dc:creator>
      <pubDate>Mon, 18 Nov 2024 09:12:57 +0000</pubDate>
      <link>https://dev.to/joeshak/crushing-subarray-sum-equals-k-knb</link>
      <guid>https://dev.to/joeshak/crushing-subarray-sum-equals-k-knb</guid>
      <description>&lt;h2&gt;
  
  
  The Problem: What’s the Buzz About?
&lt;/h2&gt;

&lt;p&gt;You’re tasked with finding how many continuous subarrays in an array sum up to a given number k. Think of it like you’re on a hunt for subarrays that add up to a specific score. Here’s the official problem: &lt;a href="https://leetcode.com/problems/subarray-sum-equals-k/description/" rel="noopener noreferrer"&gt;Subarray Sum Equals K on LeetCode&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Quick Take: What’s the Deal?
&lt;/h2&gt;

&lt;p&gt;Given an array of numbers and a target sum k, we need to count how many subarrays sum up exactly to k. Simple, right? Well, not so much when you start thinking about those nested loops… yikes! Let’s avoid O(n^2) solutions and hit that sweet O(n) mark with a neat trick.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Simple Breakdown 🛠️
&lt;/h3&gt;

&lt;p&gt;Here’s the plan:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We use a dictionary (hash map) to store prefix sums — these are sums of all elements from the start up to a given index.&lt;/li&gt;
&lt;li&gt;For each element in the array, we calculate the running sum (current_sum).&lt;/li&gt;
&lt;li&gt;If current_sum - k exists in our dictionary, it means we found a subarray that sums up to k (cue confetti 🎉).&lt;/li&gt;
&lt;li&gt;We add current_sum to the dictionary and keep track of how many times it appears.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why This Works
&lt;/h3&gt;

&lt;p&gt;The current_sum - k trick tells us that there’s a subarray that ends at the current index with a total sum of k. This avoids recalculating every subarray sum from scratch — saving time and making our approach much more efficient.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Magic ✨
&lt;/h3&gt;

&lt;p&gt;Here’s the Python code for the solution:&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def subarraySum(self, nums: List[int], k: int) -&amp;gt; int:
    count = 0
    prefix_sum = 0
    prefix_count = {0:1}

    for num in nums:
        prefix_sum += num
        rem_sum = prefix_sum - k
        rem_sum_count = prefix_count.get(rem_sum, None)

        # if remaining sum exists in the map, 
        # add the prefix sum count
        if rem_sum_count:
            count += rem_sum_count

        # add or increment the prefix sum count
        if prefix_count.get(prefix_sum, None):
            prefix_count[prefix_sum] += 1
        else:
            prefix_count[prefix_sum] = 1

    return count
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Flows 📈
&lt;/h2&gt;

&lt;p&gt;We loop through the array, adding each number to current_sum. Check if current_sum - k exists in prefix_sum_counts. If yes, we found a subarray that sums up to k, so we update count. Record the current_sum in the dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I Loved Solving This
&lt;/h2&gt;

&lt;p&gt;It’s one of those problems that makes you think, “There’s gotta be a trick to this!” Once you understand how prefix sums can work like a charm, it’s a satisfying ‘aha!’ moment. It’s like finding that hidden pathway in a game that saves you hours of grinding. 🕹️&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up 🌯
&lt;/h2&gt;

&lt;p&gt;This problem is a solid test of your data structure skills. You learn how to make a seemingly complex problem much more manageable using a simple map and some math magic. Plus, it’s a sweet example of how thinking outside the nested-loop box pays off!&lt;/p&gt;

&lt;p&gt;Try it out, and you’ll soon be saying, “Subarray sum equals k? Easy peasy!” 😎&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
