2023-12-09 18:42:21 +08:00
< p > Given an array of positive integers < code > arr< / code > , return < em > the sum of all possible < strong > odd-length subarrays< / strong > of < / em > < code > arr< / code > .< / p >
2022-03-27 20:45:09 +08:00
2023-12-09 18:42:21 +08:00
< p > A < strong > subarray< / strong > is a contiguous subsequence of the array.< / p >
2022-03-27 20:45:09 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:45:09 +08:00
< pre >
< strong > Input:< / strong > arr = [1,4,2,5,3]
< strong > Output:< / strong > 58
< strong > Explanation: < / strong > The odd-length subarrays of arr and their sums are:
[1] = 1
[4] = 4
[2] = 2
[5] = 5
[3] = 3
[1,4,2] = 7
[4,2,5] = 11
[2,5,3] = 10
[1,4,2,5,3] = 15
If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:45:09 +08:00
< pre >
< strong > Input:< / strong > arr = [1,2]
< strong > Output:< / strong > 3
< b > Explanation: < / b > There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:45:09 +08:00
< pre >
< strong > Input:< / strong > arr = [10,11,12]
< strong > Output:< / strong > 66
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = arr.length < = 100< / code > < / li >
< li > < code > 1 < = arr[i] < = 1000< / code > < / li >
< / ul >
2023-12-09 18:42:21 +08:00
< p > < / p >
< p > < strong > Follow up:< / strong > < / p >
< p > Could you solve this problem in O(n) time complexity?< / p >