Ctrl K

Totals of Odd-Length Slices

Easy
You are given an array of non-negative integers nums. A slice is a non-empty run of consecutive elements of the array. Take every slice of odd length (1, 3, 5, …), compute the sum of each one, and return the grand total of all those sums.
Example 1
nums=[2, 6, 1, 3] → 31

The odd-length slices are the four single elements (12 in total), [2,6,1] = 9 and [6,1,3] = 10. Altogether 12 + 9 + 10 = 31.

Example 2
nums=[7] → 7

The only slice is the element itself.

Example 3
nums=[1, 1, 1] → 6

The three single elements give 3, the whole array gives another 3, so 6 in total.

Constraints
  • 1 ≤ nums.length ≤ 10⁵
  • 0 ≤ nums[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEven-Gap Cut Points EasyA Stretch Divisible by k MediumThe Growing Grab Limit MediumStretches With k Odd Numbers MediumSplit Into Equal-XOR Halves Medium
loading Python…
Input
nums=[2, 6, 1, 3]
Expected
31

Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.