2
Suppose N is the length of given array.3
Number of subarrays including element arr[i] is4
i * (N-i) + (N-i) because there are N-i subarrays with arr[i] as first element5
and i * (N-i) subarrays with arr[i] as a not-first element. arr[i] appears in 6
(N-i) subarrays for each preceding element and therefore we have i*(N-i).8
Suppose i * (N-i) + (N-i) is `total`. Ceil(total / 2) is the number of odd-length subarrays and Floor(total / 2) is the number of even-length subarrays. 9
When total is odd, there is one more odd-length subarray because of a single-element subarray.11
For each number, we multiply its value with the total number of subarrays it appears and14
var sumOddLengthSubarrays = function (arr) {17
for (let i = 0; i < arr.length; i++) {18
let total = i * (N - i) + (N - i);19
sum += Math.ceil(total / 2) * arr[i];