Ctrl K

Fixed-Step Chain

Medium
You are given an integer array values and an integer step. Choose some elements, keeping their original order, so that each chosen element is exactly step more than the previous chosen one (with a negative step, that means smaller). Return the largest number of elements such a chain can have. A single element counts as a chain.
Example 1
values=[4, 1, 7, 2, 10, 13], step=3 → 4

The chain 4, 7, 10, 13: each value is exactly 3 more than the previous one.

Example 2
values=[9, 6, 5, 3, 0], step=-3 → 4

The step is negative: 9, 6, 3, 0.

Example 3
values=[2, 2, 2], step=1 → 1

No pair differs by 1, so the chain is a single number.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁴ ≤ values[i] ≤ 10⁴
  • -10⁴ ≤ step ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit Into Dictionary Words MediumPoints Without Neighbors MediumWords Hidden in a Text MediumTrees Built From Products MediumLongest Sum Chain Medium
loading Python…
Input
values=[4, 1, 7, 2, 10, 13], step=3
Expected
4

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