Problem 309972 · medium · Level 03 Linear Management & Searching

Total Over Odd-Length Windows

contribution counting · prefix sums · combinatorics

Given a list nums, consider every contiguous window whose length is odd (1, 3, 5, ...). Return the sum of the sums of all such windows.

Examples

Input:  nums = [2, 6, 1, 3]
Output: 31
Explanation: length 1 windows total 12; length 3 windows [2,6,1] = 9 and [6,1,3] = 10; 12 + 9 + 10 = 31.

Input:  nums = [3]
Output: 3

Constraints

  • 1 <= len(nums) <= 10**5
  • -1000 <= nums[i] <= 1000
  • Target complexity: O(n) time. Enumerating the windows is O(n^2) and too slow for the largest tests.

Goals

  • Replace enumeration of windows by counting how often each element is used
  • Derive the number of odd-length windows through a position
Starting Python…