/*
Problem Description
You are given an integer array arr of length N.
You have to find the sum of all subarray sums of A.
More formally, a subarray is defined as a contiguous part of an array which we can obtain by deleting zero or more elements from either end of the array.
A subarray sum denotes the sum of all the elements of that subarray.

Problem Constraints
1 <= N <= 10^5
1 <= Ai <= 10^4
*/

import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    
    public static long sumofAllSubarraysSum(int arr []){
        int n = arr.length;
        long overallsum = 0 ;     

        for(int i=0; i <n ; i++){
            
            int start = i+1 ;
            int end = n-i;
            
            long freq = (long)start * end ;
            overallsum += freq * arr[i];
        }
        
        return overallsum ;        
    }

    
    public static void main(String[] args) {
        int [] arr = { 4 ,3 ,7 ,6 };
        System.out.println("RESULT : " + sumofAllSubarraysSum(arr));
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: