Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 Explanation: 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
Example 2:
Input: arr = [1,2] Output: 3 Explanation: There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.
Example 3:
Input: arr = [10,11,12] Output: 66
Constraints:
1 <= arr.length <= 1001 <= arr[i] <= 1000Follow up:
Could you solve this problem in O(n) time complexity?
Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,4,2,5,3]
[1,2]
[10,11,12]
sum-of-squares-of-special-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1588: Sum of All Odd Length Subarrays
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int n = arr.length;
int[] f = new int[n];
int[] g = new int[n];
int ans = f[0] = arr[0];
for (int i = 1; i < n; ++i) {
f[i] = g[i - 1] + arr[i] * (i / 2 + 1);
g[i] = f[i - 1] + arr[i] * ((i + 1) / 2);
ans += f[i];
}
return ans;
}
}
// Accepted solution for LeetCode #1588: Sum of All Odd Length Subarrays
func sumOddLengthSubarrays(arr []int) (ans int) {
n := len(arr)
f := make([]int, n)
g := make([]int, n)
f[0] = arr[0]
ans = f[0]
for i := 1; i < n; i++ {
f[i] = g[i-1] + arr[i]*(i/2+1)
g[i] = f[i-1] + arr[i]*((i+1)/2)
ans += f[i]
}
return
}
# Accepted solution for LeetCode #1588: Sum of All Odd Length Subarrays
class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
n = len(arr)
f = [0] * n
g = [0] * n
ans = f[0] = arr[0]
for i in range(1, n):
f[i] = g[i - 1] + arr[i] * (i // 2 + 1)
g[i] = f[i - 1] + arr[i] * ((i + 1) // 2)
ans += f[i]
return ans
// Accepted solution for LeetCode #1588: Sum of All Odd Length Subarrays
impl Solution {
pub fn sum_odd_length_subarrays(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut f = vec![0; n];
let mut g = vec![0; n];
let mut ans = arr[0];
f[0] = arr[0];
for i in 1..n {
f[i] = g[i - 1] + arr[i] * ((i as i32) / 2 + 1);
g[i] = f[i - 1] + arr[i] * (((i + 1) as i32) / 2);
ans += f[i];
}
ans
}
}
// Accepted solution for LeetCode #1588: Sum of All Odd Length Subarrays
function sumOddLengthSubarrays(arr: number[]): number {
const n = arr.length;
const f: number[] = Array(n).fill(arr[0]);
const g: number[] = Array(n).fill(0);
let ans = f[0];
for (let i = 1; i < n; ++i) {
f[i] = g[i - 1] + arr[i] * ((i >> 1) + 1);
g[i] = f[i - 1] + arr[i] * ((i + 1) >> 1);
ans += f[i];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.