All problems
1102MediumArrayHash TableDynamic Programming

The Longest Adding Ladder of Readings

Tracked in this browser only
Write code

Trains the technique from

LeetCode 873Length of Longest Fibonacci Subsequence

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

The list gauges is strictly increasing. A ladder is a selection of three or more of its readings, kept in the order they appear in the list, where every reading after the first two equals the sum of the two readings before it.

Return the length of the longest ladder, or 0 when the list holds none.

Examples

Example 1

Input
gauges = [1, 2, 3, 5, 8, 13, 21]
Output
7

Every reading from the third onwards is the sum of the two before it, so the whole list is one ladder.

Example 2

Input
gauges = [1, 2, 4]
Output
0

No reading is the sum of two earlier ones, so there is no ladder at all.

Example 3

Input
gauges = [3, 6, 9, 12, 15, 18, 21, 24]
Output
5

Take 3, 6, 9, 15 and 24: each rung is the sum of the two before it. A sixth rung would have to be 39, which the list does not hold.

Constraints

  • 3 <= gauges.length <= 1000
  • 1 <= gauges[i] <= 10^9
  • gauges is strictly increasing

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def len_longest_fib_subseq(gauges: list[int]) -> int:
Java
public int lenLongestFibSubseq(int[] gauges)
September 7
Apply