Ctrl K

Longest Sum Chain

Medium
Call a sequence x₁, x₂, …, xₘ a sum chain if it has at least 3 elements and every element from the third on equals the sum of the two before it: xᵢ + xᵢ₊₁ == xᵢ₊₂. You get a strictly increasing array of positive integers values. Return the length of the longest subsequence of values that is a sum chain, or 0 if there is none. A subsequence is what remains after deleting any elements while keeping the rest in order.
Example 1
values=[2, 3, 4, 5, 7, 9, 12, 20] → 4

[2, 5, 7, 12] works: 2 + 5 = 7 and 5 + 7 = 12, length 4. No longer chain exists.

Example 2
values=[1, 4, 10, 30] → 0

No number equals the sum of two earlier picks, so the answer is 0.

Example 3
values=[1, 2, 3, 5, 8] → 5
Constraints
  • 3 ≤ len(values) ≤ 1000
  • 1 ≤ values[i] < values[i + 1] ≤ 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 Equal-Gap Chain Medium
loading Python…
Input
values=[2, 3, 4, 5, 7, 9, 12, 20]
Expected
4

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