Problem 586344 · medium · Phase 05 Advanced Algorithms & Graphs

Even Strides

dynamic programming · 1-D dp · arithmetic runs · counting subarrays

A running watch records the distance marker at each stride in marks. A stretch of at least three consecutive entries is even if the difference between neighbouring entries is the same throughout it. Return how many even stretches there are (a longer even stretch also contains shorter ones, each counted separately).

Examples

Input:  marks = [1, 3, 5, 7, 8]
Output: 3
Explanation: [1,3,5], [3,5,7] and [1,3,5,7].

Input:  marks = [2, 2, 2, 2]
Output: 3

Constraints

  • 0 <= len(marks) <= 10**5
  • -10**6 <= marks[i] <= 10**6
  • Target complexity: O(n) time.

Goals

  • Count arithmetic subarrays by extending the count that ends at the previous index
  • Recognise that each new element adds as many slices as the run length allows
Starting Python…