<?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>Forem: Satyam Pruthi</title>
    <description>The latest articles on Forem by Satyam Pruthi (@satyam150506).</description>
    <link>https://forem.com/satyam150506</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%2F3381361%2F2f0b0662-a22a-4960-82ae-8c223923f6dc.png</url>
      <title>Forem: Satyam Pruthi</title>
      <link>https://forem.com/satyam150506</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/satyam150506"/>
    <language>en</language>
    <item>
      <title>A New Perspective on Subarray Sum: Beyond Brute Force and Prefix Arrays</title>
      <dc:creator>Satyam Pruthi</dc:creator>
      <pubDate>Wed, 23 Jul 2025 09:30:08 +0000</pubDate>
      <link>https://forem.com/satyam150506/a-new-perspective-on-subarray-sum-beyond-brute-force-and-prefix-arrays-2ifc</link>
      <guid>https://forem.com/satyam150506/a-new-perspective-on-subarray-sum-beyond-brute-force-and-prefix-arrays-2ifc</guid>
      <description>&lt;p&gt;As a developer passionate about problem solving, I often find joy in discovering elegant alternatives to well-known algorithms. Recently, I derived a fresh approach to calculating the sum of all subarrays of an array—one that goes beyond brute force and even traditional prefix sum techniques.&lt;br&gt;
This blog walks you through the intuition, implementation, and mathematical reasoning behind my two-pass prefix-based solution.&lt;/p&gt;

&lt;p&gt;💡 Problem Statement&lt;br&gt;
Given an array arr[] of length n, compute the sum of all possible subarrays.&lt;br&gt;
Example:&lt;br&gt;
Input: arr = [1, 2, 3]&lt;br&gt;
Output: 20&lt;br&gt;
Explanation:&lt;br&gt;
Subarrays: [1], [2], [3], [1,2], [2,3], [1,2,3]&lt;br&gt;
Sum = 1 + 2 + 3 + (1+2) + (2+3) + (1+2+3) = 20&lt;/p&gt;

&lt;p&gt;🚫 Traditional Approaches&lt;br&gt;
Most solutions fall into one of these categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Brute Force (O(n²)): Iterate over all subarrays and sum them individually.&lt;/li&gt;
&lt;li&gt;Prefix Sum Array: Precompute prefix sums and use them to calculate subarray sums.&lt;/li&gt;
&lt;li&gt;Contribution Technique: Use mathematical formulas to compute how many times each element appears in subarrays.
While effective, these methods either lack elegance or require additional space or combinatorial reasoning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ My Two-Pass Prefix-Based Solution&lt;br&gt;
Here’s the code:&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
public int subarraySum(int[] arr) {&lt;br&gt;
    int prefix = 0, prefixsum = 0, ans = 0;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// First pass: compute total of all prefix sums
for (int i = 0; i &amp;lt; arr.length; i++) {
    prefix += arr[i];
    prefixsum += prefix;
}

int totalpresum = prefixsum;
prefixsum = 0;
prefix = 0;

// Second pass: subtract scaled prefix to isolate contributions
for (int i = 0; i &amp;lt; arr.length; i++) {
    ans += (totalpresum - prefixsum - (prefix * (arr.length - i)));
    prefix += arr[i];
    prefixsum += prefix;
}

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

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;🧠 Intuition Behind the Formula&lt;br&gt;
Let’s break it down:&lt;br&gt;
🔹 First Pass&lt;br&gt;
We calculate the sum of all prefix sums. This gives us a cumulative view of how elements build up across subarrays.&lt;br&gt;
🔹 Second Pass&lt;br&gt;
We subtract overlapping contributions using:&lt;br&gt;
ans += totalpresum - prefixsum - (prefix * (n - i))&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;totalpresum: Total of all prefix sums.&lt;/li&gt;
&lt;li&gt;prefixsum: Sum of prefixes up to index i.&lt;/li&gt;
&lt;li&gt;prefix * (n - i): Adjusts for how many subarrays the current prefix affects.
This isolates the net contribution of each element across all subarrays.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📊 Time and Space Complexity&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time Complexity: O(n)&lt;/li&gt;
&lt;li&gt;Space Complexity: O(1)
No extra arrays, no nested loops—just pure arithmetic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🧪 Example Walkthrough&lt;br&gt;
Let’s walk through arr = [1, 2, 3].&lt;br&gt;
First Pass:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Subarrays and their sums:&lt;/li&gt;
&lt;li&gt;[1] → 1&lt;/li&gt;
&lt;li&gt;[2] → 2&lt;/li&gt;
&lt;li&gt;[3] → 3&lt;/li&gt;
&lt;li&gt;[1, 2] → 3&lt;/li&gt;
&lt;li&gt;[2, 3] → 5&lt;/li&gt;
&lt;li&gt;[1, 2, 3] → 6
🔢 Total prefix Sum = 1 + 2 + 3 + 3 + 5 + 6 = 20
Second Pass:
We adjust each prefix’s contribution using the formula, and the final answer becomes 20.
&lt;strong&gt;ans += totalpresum - prefixsum - (prefix * (n - i))&lt;/strong&gt;
🌟 Why This Matters
This approach isn’t just efficient—it’s original. I couldn’t find this exact method documented on any major platform. It’s a great example of how deep thinking and experimentation can lead to new insights in algorithm design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✍️ Final Thoughts&lt;br&gt;
If you found this technique interesting, I’d love to hear your thoughts or see how you might extend it to related problems (like sum of all submatrices in 2D arrays).&lt;br&gt;
You can connect with me on LinkedIn(Satyam Pruthi) or leave a comment below. Let’s keep learning and building together!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>competativeprogramming</category>
      <category>java</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
