[Naive Approach] Using Nested Loop - O(n2) Time and O(1) Space
The main idea of this approach is to iterate over all possible subarrays of the given array using two nested loops. The outer loop selects the starting index of the subarray, while the inner loop extends the subarray to include all possible ending points. During this process, a running sum (temp) is maintained for each subarray and added to the total result.
C++
#include<iostream>#include<vector>usingnamespacestd;intsubarraySum(vector<int>&arr){intn=arr.size();intresult=0,temp=0;// pick starting pointfor(inti=0;i<n;i++){// pick ending pointtemp=0;for(intj=i;j<n;j++){// sum subarray between current// starting and ending pointstemp+=arr[j];result+=temp;}}returnresult;}intmain(){vector<int>arr={1,4,5,3,2};intn=arr.size();cout<<subarraySum(arr);return0;}
Java
classGfG{staticintsubarraySum(int[]arr){intn=arr.length;intresult=0,temp=0;// pick starting pointfor(inti=0;i<n;i++){// pick ending pointtemp=0;for(intj=i;j<n;j++){// sum subarray between current starting // and ending pointstemp+=arr[j];result+=temp;}}returnresult;}publicstaticvoidmain(String[]args){int[]arr={1,4,5,3,2};System.out.println(subarraySum(arr));}}
Python
defsubarraySum(arr):n=len(arr)result=0# pick starting pointforiinrange(n):temp=0# pick ending pointforjinrange(i,n):# sum subarray between current starting # and ending pointstemp+=arr[j]result+=tempreturnresultif__name__=="__main__":arr=[1,4,5,3,2]print(subarraySum(arr))
C#
usingSystem;classGfG{staticintSubarraySum(int[]arr){intn=arr.Length;intresult=0;// Pick starting pointfor(inti=0;i<n;i++){inttemp=0;// pick ending pointfor(intj=i;j<n;j++){// sum subarray between current starting // and ending pointstemp+=arr[j];result+=temp;}}returnresult;}staticvoidMain(){int[]arr={1,4,5,3,2};Console.WriteLine(SubarraySum(arr));}}
JavaScript
functionsubarraySum(arr){letn=arr.length;letresult=0;// pick starting pointfor(leti=0;i<n;i++){lettemp=0;// pick ending pointfor(letj=i;j<n;j++){// sum subarray between current starting// and ending pointstemp+=arr[j];result+=temp;}}returnresult;}// Driver Code letarr=[1,4,5,3,2];console.log(subarraySum(arr));
Output
116
[Expected Approach] Element Contribution Method - O(n) Time and O(1) Space
If we take a close look then we observe a pattern.
Third element arr[2] appears 3 time when subarray start with arr[0]: [1, 4, 5], [1, 4, 5, 3], [1, 4, 5, 3, 2] Third element arr[2] appears 3 time when subarray start with arr[1]: [4, 5], [4, 5, 3], [4, 5, 3, 2] Third element arr[2] appears 3 time when subarray start with arr[2]: [5], [5, 3], [5, 3, 2]
So, We can clearly see that, For any element arr[i] in an array of size n, it appears in exactly (i + 1) * (n - i) subarrays.
Run a for loop for i from [0, n-1]: Add arr[i] * (i+1) * (n-i) into the answer at each iteration
Return answer
C++
#include<iostream>#include<vector>usingnamespacestd;intsubarraySum(vector<int>&arr){intn=arr.size();intresult=0;// computing sum of subarray using formulafor(inti=0;i<n;i++)result+=(arr[i]*(i+1)*(n-i));// return all subarray sumreturnresult;}intmain(){vector<int>arr={1,4,5,3,2};intn=arr.size();cout<<subarraySum(arr);return0;}
Java
classGfG{staticintsubarraySum(int[]arr){intn=arr.length;intresult=0;// Computing sum of subarrays using the formulafor(inti=0;i<n;i++){result+=(arr[i]*(i+1)*(n-i));}// Return the sum of all subarraysreturnresult;}publicstaticvoidmain(String[]args){int[]arr={1,4,5,3,2};System.out.println(subarraySum(arr));}}
Python
defsubarraySum(arr):n=len(arr)result=0# Computing sum of subarrays using the formulaforiinrange(n):result+=arr[i]*(i+1)*(n-i)# Return the sum of all subarraysreturnresultif__name__=="__main__":arr=[1,4,5,3,2]print(subarraySum(arr))
C#
usingSystem;classGfG{staticintsubarraySum(int[]arr){intn=arr.Length;intresult=0;// Computing sum of subarrays using the formulafor(inti=0;i<n;i++){result+=arr[i]*(i+1)*(n-i);}// Return the sum of all subarraysreturnresult;}staticvoidMain(){int[]arr={1,4,5,3,2};Console.WriteLine(subarraySum(arr));}}
JavaScript
functionsubarraySum(arr){constn=arr.length;letresult=0;// Computing sum of subarrays using the formulafor(leti=0;i<n;i++){result+=arr[i]*(i+1)*(n-i);}// Return the sum of all subarraysreturnresult;}// Driver Codeconstarr=[1,4,5,3,2];console.log(subarraySum(arr));