23. Sum of Subarrays
โ GFG solution to the Sum of Subarrays problem: calculate total sum of all possible subarrays using mathematical contribution technique. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given an array arr[]
, find the sum of all the subarrays of the given array.
A subarray is a contiguous sequence of elements within an array. The goal is to calculate the total sum of all possible subarrays efficiently.
Note: It is guaranteed that the total sum will fit within a 32-bit integer range.
๐ Examples
Example 1
Input: arr[] = [1, 2, 3]
Output: 20
Explanation: All subarray sums are: [1] = 1, [2] = 2, [3] = 3, [1, 2] = 3, [2, 3] = 5, [1, 2, 3] = 6.
Thus total sum is 1 + 2 + 3 + 3 + 5 + 6 = 20.
Example 2
Input: arr[] = [1, 3]
Output: 8
Explanation: All subarray sums are: [1] = 1, [3] = 3, [1, 3] = 4.
Thus total sum is 1 + 3 + 4 = 8.
๐ Constraints
$1 \le \text{arr.size()} \le 10^5$
$0 \le \text{arr}[i] \le 10^4$
โ
My Approach
The optimal approach uses the Mathematical Contribution Technique to efficiently calculate each element's total contribution across all subarrays:
Mathematical Contribution Formula
Key Insight:
Each element
arr[i]
appears in multiple subarrays.The number of subarrays containing
arr[i]
can be calculated mathematically.Element at index
i
appears in(i + 1) * (n - i)
subarrays.
Formula Derivation:
Left choices: Element at index
i
can be the starting point for subarrays in(i + 1)
ways (indices 0 to i).Right choices: Element at index
i
can be the ending point for subarrays in(n - i)
ways (indices i to n-1).Total contribution:
arr[i] * (i + 1) * (n - i)
Algorithm:
Iterate through each element once.
Calculate its contribution using the formula.
Sum all contributions to get the final result.
Mathematical Proof:
For element at position
i
, it appears in subarrays starting from positions[0, 1, 2, ..., i]
(i+1 choices).Each of these subarrays can end at positions
[i, i+1, i+2, ..., n-1]
(n-i choices).Total occurrences =
(i + 1) * (n - i)
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We perform a single pass through the array, calculating each element's contribution in constant time.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables (sum, loop counter).
๐งโ๐ป Code (C++)
class Solution {
public:
int subarraySum(vector<int>& arr) {
int n = arr.size(), sum = 0;
for (int i = 0; i < n; ++i)
sum += arr[i] * (i + 1) * (n - i);
return sum;
}
};
๐งโ๐ป Code (Java)
class Solution {
public int subarraySum(int[] arr) {
int n = arr.length, sum = 0;
for (int i = 0; i < n; ++i)
sum += arr[i] * (i + 1) * (n - i);
return sum;
}
}
๐ Code (Python)
class Solution:
def subarraySum(self, arr):
n = len(arr)
return sum(arr[i] * (i + 1) * (n - i) for i in range(n))
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated