Sum of All Odd Length Subarrays (LeetCode 1588): Solution and Explanation
Add up every odd-length subarray without listing them, by working out how many odd-length subarrays each element belongs to.
Intuition
The answer is a sum of sums, so each element is added once for every odd-length subarray that contains it. If we know that count for arr[i], its share of the answer is arr[i] times the count.
A subarray that contains index i is fixed by its start and end. The start can be any of 0..i (i + 1 choices) and the end any of i..n-1 (n - i choices), giving (i + 1) * (n - i) subarrays.
Lay those choices out as a grid with starts as rows and ends as columns. Moving one step in either direction changes the length by one, so odd and even lengths form a checkerboard. The corner cell start = end = i has length 1, which is odd, so the odd cells number (total + 1) / 2, rounded down.
Approach: Contribution counting
- Set total = 0 and n = len(arr).
- For each index i, let left = i + 1 and right = n - i.
- The number of odd-length subarrays containing i is odd = (left * right + 1) // 2.
- Add arr[i] * odd to total, then return total.
Step-by-step example
arr = [1,4,2,5,3]
- i = 0: left 1, right 5, 5 subarrays, 3 odd. 1 * 3 = 3, total = 3.
- i = 1: left 2, right 4, 8 subarrays, 4 odd. 4 * 4 = 16, total = 19.
- i = 2: left 3, right 3, 9 subarrays, 5 odd. 2 * 5 = 10, total = 29.
- i = 3: left 4, right 2, 8 subarrays, 4 odd. 5 * 4 = 20, total = 49.
- i = 4: left 5, right 1, 5 subarrays, 3 odd. 3 * 3 = 9, total = 58.
Code: Python, Java, C++
1class Solution:
2 def sumOddLengthSubarrays(self, arr: List[int]) -> int:
3 n = len(arr)
4 total = 0
5 for i, x in enumerate(arr):
6 left, right = i + 1, n - i
7 odd = (left * right + 1) // 2
8 total += x * odd
9 return total1class Solution {
2 public int sumOddLengthSubarrays(int[] arr) {
3 int n = arr.length;
4 int total = 0;
5 for (int i = 0; i < n; i++) {
6 int left = i + 1, right = n - i;
7 int odd = (left * right + 1) / 2;
8 total += arr[i] * odd;
9 }
10 return total;
11 }
12}1class Solution {
2public:
3 int sumOddLengthSubarrays(vector<int>& arr) {
4 int n = arr.size();
5 int total = 0;
6 for (int i = 0; i < n; i++) {
7 int left = i + 1, right = n - i;
8 int odd = (left * right + 1) / 2;
9 total += arr[i] * odd;
10 }
11 return total;
12 }
13};Time and space complexity
O(n)
One pass with constant work per element.
O(1)
Only a running total and a few integers.
Other approaches
Prefix sums over every odd length (time O(n^2), space O(n))
Build prefix sums, then for every start and every odd length read the subarray sum in O(1). Simple and correct, but quadratic.
Brute force (time O(n^3), space O(1))
Enumerate every odd-length subarray and sum it directly. Cubic, though still fine for n <= 100.
Common mistakes
- Using left * right / 2 without the + 1. When the product is odd, the extra odd cell (the single-element subarray) is lost.
- Off-by-one on the choices: there are i + 1 starts (including i itself) and n - i ends (including i itself).
- Forgetting floor division in JavaScript, where / gives a fraction.
Frequently asked questions
Why are exactly half (rounded up) of the subarrays odd-length?
In the start/end grid, moving to an adjacent start or end changes the length by one, so parities alternate like a checkerboard. The corner start = end = i is odd, so odd cells are the larger half when the grid has an odd number of cells.
Can the total overflow a 32-bit int?
No. With n = 100 and every value 1000 the answer is 85,850,000, well inside the int range.
Does this extend to even-length subarrays?
Yes. The even count is left * right minus the odd count, that is (left * right) // 2.