Sum of All Odd Length Subarrays

Easy

Description

Given an array of positive integers arr, return the sum of all possible odd-length subarrays of arr. A subarray is a contiguous subsequence of the array.

Examples

Input:arr = [1,4,2,5,3]
Output:58
Explanation:

All odd-length subarrays sum.

Input:arr = [7]
Output:7
Explanation:

With only one element, there's only one subarray of odd length (length 1): [7]. The sum is 7.

Input:arr = [2,1,3,4]
Output:24
Explanation:

Odd-length subarrays: length 1: [2]=2, [1]=1, [3]=3, [4]=4; length 3: [2,1,3]=6, [1,3,4]=8. Total: 2+1+3+4+6+8=24.

Constraints

  • 1 ≤ arr.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!