Sum of Subarrays | Practice | GeeksforGeeks

Given an array A[] with N elements , you need to find the sum all sub arrays of array A. Since the sum could be very large print the sum modulo (109+7).

Example 1:
Input:
N = 3
A[] = {1, 2, 3}
Output: 20
Explanation:
All subarrays are [1], [2], [3],
[1,2], [2,3], [1,2,3].
Thus total sum is 20.
 
Example 2:
Input:
N = 2
A[] = {1, 3}
Output: 8


Your Task:  
You don't need to read input or print anything. Your task is to complete the function subarraySum() which takes the array A[] and its size N as inputs and returns the sum.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints :
1 ≤ N ≤ 105
1 ≤ A[] ≤ 109
0
imranwahid3 days ago
Easy C++ solution
0
swagathshetty3 days ago
for anyone stuck in javascript test cases . convert all the numbers to bigInt() and then do the calculations.
 
at the end when returning the result convert it back to a number using Number()
0
ershubhamkewat2 weeks ago
simple java code O(n)
public static long subarraySum( long a[], long n) {
       long p=1000000007, sum=0;
        for(int i=0;i<n;i++)
           sum= (sum + a[i]*(n-i)*(i+1))%p;
        return sum%p;
    }
0
surajkumar725011 month ago
class Solution:   def subarraySum(self, a, n):       sum=0       p=1000000007       for i in range(0,n):            sum = (sum + ((((i+1)*a[i])%p)*(n-i))%p)%p       return sum
0
chetanbharatyadav2 months ago
class Solution{   public:   long long subarraySum(long long a[], long long n)   {       // Your code goes here        long long total=0;                for(int i=0;i<n;i++){            total+=(a[i]*(n-i)*(i+1))%1000000007;        }        return total%1000000007;   }};
0
manishsaini21423 months ago
  long long subarraySum(long long a[], long long n)   {long long m=pow(10,9)+7;       long long ans=0;       for(int i=0;i<n;i++){     long long con=(((i+1)%m *(n-i)%m)%m * a[i]%m) %m;     ans = (ans%m + con%m)%m;       }return ans;   }};
in c++
0
2001shivank4 months ago
understand the formula ,for O(n)  ,there is a formula ,which can be understood when you solve some examples on paper, you will feel how symmetrical/patterned  is this mathematics world.
-2
hemant18singh4 months ago
In example 1 why subarray {1,3 } was not consider
0
gopaljirai19995 months ago
Java O(n) time and o(1) space most easy to understand
 
class Solution {   public static long subarraySum( long a[], long n) {       long m = 1000000007;       long sum = 0;       long ans = 0;      for(int i=0;i<n;i++){          sum=((a[i]%m)*((i+1)%m)*((n-i)%m))%m;          ans = (ans%m + sum%m)%m;      }      return ans;   }}
0
ankitmohapatra5 months ago
Can anybody say what is wrong with this jugaad ? 
 
class Solution{   
public:
    long long subarraySum(long long arr[], long long n)
    {
    vector<int> res;
    int curr = 0;
    for(int i = 0; i < n; i++){
        curr = 0;
        for(int j = i; j < n; j++){
            curr += arr[j];
            res.push_back(curr);
        }
    }
    int totalSum = 0;
    for(int i = 0; i < res.size(); i++){
        totalSum += res[i];
    }
    return totalSum;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.