<?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 Palash Bagora on Medium]]></title>
        <description><![CDATA[Stories by Palash Bagora on Medium]]></description>
        <link>https://medium.com/@palashpaliwal25?source=rss-22723f651527------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vPq6zoOC_0TT0kfUx6hSyQ.jpeg</url>
            <title>Stories by Palash Bagora on Medium</title>
            <link>https://medium.com/@palashpaliwal25?source=rss-22723f651527------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 20:00:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@palashpaliwal25/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[Finding an Effective Solution to the Subarray Sum Equals K Problem]]></title>
            <link>https://medium.com/@palashpaliwal25/finding-an-effective-solution-to-the-subarray-sum-equals-k-problem-3a7fce76654c?source=rss-22723f651527------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a7fce76654c</guid>
            <dc:creator><![CDATA[Palash Bagora]]></dc:creator>
            <pubDate>Mon, 30 Sep 2024 16:23:52 GMT</pubDate>
            <atom:updated>2024-09-30T16:23:52.028Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Problem Statement- </strong>In this problem, we are given an array of integer nums and an integer k. We need to find the total number of continuous subarrays whose sum equals k.</p><pre>Input: nums = [1, 3, 5, 3], k = 8<br>Output: 2<br>Explanation: [3, 5] (starting at index 1) and [5, 3] (starting at index 2) are two subarrays that sum to 8.</pre><p>This problem challenges our understanding of array traversal and the efficient use of data structures. Let’s see an approach that optimizes the solution.</p><h3><strong>Approach 1: Brute Force (O(n²) Time Complexity)</strong></h3><p>A naive approach would involve generating all possible subarrays, and then calculating their sums, and checking if they are equal to k. This results in an O(n²) time complexity, which is not optimal for large arrays. Here’s how the brute-force approach might look:</p><pre>int subarraySum(vector&lt;int&gt;&amp; nums, int k) {<br>    int count = 0;<br>    int n=nums.size();  <br>    for (int i = 0; i&lt; n; i++) {<br>        int sum = 0;<br>        for (int j = i; j &lt; n; j++) {<br>            sum += nums[j];<br>            if (sum == k) {<br>                count++;<br>            }<br>        }<br>    }<br>    return count;<br>}</pre><p>While this approach works for small arrays, but because of its O(n²) time complexity it becomes impractical for the array having a larger size (say up to 10⁵).</p><h3><strong>Approach 2: Optimized Approach Using Hash Maps (O(n) Time Complexity)</strong></h3><p>To optimize the solution, we can use a hash map to store the prefix sum up to each index. The main logic is that if the prefix sum up to index i is the sum and there exists an earlier index j such that the prefix sum up to j is sum — k, then the subarray from j+1 to i sums to k.</p><p>The key steps are:</p><p>Initially, we will move through the array while keeping track of a prefix sum.</p><p>Next, we will examine each index to determine if the value of prefix sum minus k can be found in the hash map.</p><p>The presence of it indicates the existence of a subarray that totals k.</p><p>and thereafter, we will modify the hash map with the latest prefix sum.</p><p>Here’s the optimized code:</p><pre>int subarraySum(vector&lt;int&gt;&amp; nums, int k) {<br>    unordered_map&lt;int, int&gt; mp;<br>    mp[0] = 1;  // Base case: one subarray (empty) sums to 0.<br>    int prefix_sum= 0;<br>    int count = 0;<br><br>    for (int num : nums) {<br>        prefix_sum += num;<br><br>        // Check if there is a subarray that ends at this index and sums to k.<br>        if (mp.find(prefix_sum - k) != mp.end()) {<br>            count += mp[prefix_sum - k];<br>        }<br><br>        // Increment the count of the current prefix sum.<br>        mp[prefix_sum]++;<br>    }<br><br>    return count;<br>}</pre><h4>Explanation:</h4><ul><li><strong>prefix_sum</strong>: It stores the sum of the elements from the start to the current index.</li><li><strong>mp</strong>: This hash map keeps track of how many times a certain prefix sum has occurred.</li><li><strong>count</strong>: The count monitors the number of subarrays that add up to k.</li></ul><h4>Time Complexity: O(n)</h4><ul><li>We iterate through the array once, and every search or modification in the hash map requires constant time, resulting in a time complexity of O(n).</li></ul><h4>Space Complexity: O(n)</h4><ul><li>In the worst case, we store all the prefix sums in the hash map, which requires O(n) space.</li></ul><h3>Conclusion</h3><p>The Subarray Sum Equals K problem is a great exercise for improving our skills in hash maps and efficient array manipulation. The brute-force approach is easy to understand but inefficient, especially for large inputs. The optimized hash map approach reduces the time complexity to O(n) and makes the solution practical for larger arrays.</p><p><strong>Try out the problem on Leetcode</strong>: <a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a7fce76654c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>