sum array element to form a given sum code example
Example 1: Write a program to find the sum of all sub-arrays of a given integer array.
long int SubArraySum( int arr[] , int n )
{
long int result = 0;
// computing sum of subarray using formula
for (int i=0; i<n; i++)
result += (arr[i] * (i+1) * (n-i));
// return all subarray sum
return result ;
}
Example 2: sum of an array
const sum = (...args) => args.reduce((a, b) => a + b, 0);
console.log(sum(1,2,3)); // returns 6