Problem 309972 · medium · Phase 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 = [1, 4, 2, 5]
Output: 30
Explanation: length 1 windows total 12; length 3 windows [1,4,2] = 7 and [4,2,5] = 11; 12 + 7 + 11 = 30.

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…