Every element arr[i] appears in two types of subsets:
In sybarrays beginning with arr[i]. There are
(n-i) such subsets. For example [2] appears
in [2] and [2, 3].
In (n-i)*i subarrays where this element is not
first element. For example [2] appears in
[1, 2] and [1, 2, 3].
Sum = (n-i) + (n-i)*i = (n-i)(i+1)
publicstaticlongSubArraySum(int arr[],int n ){longresult=0; // computing sum of subarray using formulafor(inti=0; i<n; i++) result +=(arr[i]*(i+1)*(n-i));return result ;}