Skip to main content
LeetCode 873, Medium. Topics: Array, Hash Table, Dynamic Programming. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

A sequence <code>x1, x2, …, xn</code> is <em>Fibonacci-like</em> if:</p> <ul> <li><code>n >= 3</code></li> <li><code>xi + xi+1 == xi+2</code> for all <code>i + 2 <= n</code></li> </ul> <p>Given a <strong>strictly increasing</strong> array <code>arr</code> of positive integers forming a sequence, return <em>the length of the longest Fibonacci-like subsequence of</em> <code>arr</code>. If one does not exist, return <code>0</code>.</p> <p>A <em>subsequence</em> is derived from another sequence <code>arr</code> by deleting any number of elements (including none) from <code>arr</code>, without changing the order of the remaining elements. For example, <code>[3, 5, 8]</code> is a subsequence of <code>[3, 4, 5, 6, 7, 8]</code>.

Examples

Constraints

  • 3 <= arr.length <= 1000
  • 1 <= arr[i] < arr[i + 1] <= 10^9

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026