Ctrl K

Longest Equal-Gap Chain

Medium
You are given an integer array values. Cross out any number of elements (possibly none), keeping the rest in their original order, so that every remaining element differs from the one before it by the same amount. The gap may be positive, negative or zero. Return the greatest possible length of such a chain.
Example 1
values=[2, 9, 5, 1, 8, 11] → 4

Pick 2, 5, 8, 11: each neighbour differs by exactly 3. No chain is longer.

Example 2
values=[10, 7, 10, 4, 10] → 3

Both 10, 10, 10 (gap 0) and 10, 7, 4 (gap -3) work, each of length 3.

Example 3
values=[5, 1] → 2

Any two numbers form a chain with an equal gap.

Constraints
  • 2 ≤ len(values) ≤ 1000
  • 0 ≤ values[i] ≤ 500
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords Hidden in a Text MediumTwo Separate Segments With a Given Sum MediumShuttle Driver Earnings MediumSquaring Ladder MediumSell Plots to Bidders Medium
loading Python…
Input
values=[2, 9, 5, 1, 8, 11]
Expected
4

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