Skip to content

Practice Sum of All Odd Length Subarrays

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1588
LeetCode Problem

Sum of All Odd Length Subarrays

Problem Statement

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: Example 2: Example 3: Constraints: Follow up: Could you solve this problem in O(n) time complexity?

Source: Sum of All Odd Length Subarrays on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [1,4,2,5,3]
Output
58

# Example 2

Input
arr = [1,2]
Output
3

# Example 3

Input
arr = [10,11,12]
Output
66

Constraints

1 <= arr.length <= 100
1 <= arr[i] <= 1000

Related LeetCode Problems

Practice more problems similar to Sum of All Odd Length Subarrays.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.