Sum of All Odd Length Subarrays

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.

30:00

Sum of All Odd Length Subarrays
easy
Free written solution
Topics
Companies

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.

Example 1:
Input: {"arr":[1,4,2,5,3]}
Output: 58
Constraints:
  • 1≤arr.length≤1001 \leq \text{arr.length} \leq 100

  • 1≤arr[i]≤10001 \leq \text{arr}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[1,4,2,5,3]}

Instead of listing every odd-length subarray, ask how many of them contain each element, then add element times that count.

14253

each cell will be one (start, end) choice; green cells have odd length

total = 0

Variables
VariableValue
total0
DepthFunction Call
Stack empty
0/16