Ctrl K

Evenly Stepped Stretches

Medium
A sensor logged its readings into the integer array readings. Call a stretch of the array (a run of consecutive elements) even if it has at least three elements and the difference between every two neighbours is the same. For example, [1, 4, 7, 10], [6, 6, 6] and [9, 5, 1] are even stretches, while [1, 2, 4] is not. Return how many even stretches readings contains. Stretches with a different start or end count separately.
Example 1
readings=[2, 4, 6, 8] → 3

[2, 4, 6], [4, 6, 8] and the whole array [2, 4, 6, 8] all qualify, each with a step of 2.

Example 2
readings=[5, 5, 5, 1, -3] → 2

[5, 5, 5] with step 0 and [5, 1, -3] with step -4. [5, 5, 1] does not count, since its steps 0 and -4 differ.

Example 3
readings=[7, 3] → 0

There is no stretch of length 3 or more.

Constraints
  • 1 ≤ len(readings) ≤ 10⁵
  • -1000 ≤ readings[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Run of Ones With One Fix MediumLongest Shared Run MediumSawtooth Readings MediumTwo Separate Windows MediumTwo Separate Segments With a Given Sum Medium
loading Python…
Input
readings=[2, 4, 6, 8]
Expected
3

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