Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays.
A subarray is a contiguous sequence of elements from the original array. For example, in array [1, 4, 2, 5, 3], the subarrays of length 1 are [1], [4], [2], [5], [3], the subarrays of length 3 are [1,4,2], [4,2,5], [2,5,3], and the subarray of length 5 is [1,4,2,5,3].
Your task: Find all subarrays with odd lengths (1, 3, 5, 7, ...), calculate the sum of each subarray, then return the total sum of all these sums.
Example: For arr = [1,4,2,5,3]:
• Length 1: [1], [4], [2], [5], [3] → sum = 15
• Length 3: [1,4,2], [4,2,5], [2,5,3] → sum = 7+11+10 = 28
• Length 5: [1,4,2,5,3] → sum = 15
• Total: 15 + 28 + 15 = 58
Input & Output
Constraints
- 1 ≤ arr.length ≤ 100
- 1 ≤ arr[i] ≤ 1000
- All integers in the array are positive