Length of Longest Fibonacci Subsequence - Problem

Imagine you're a mathematician studying Fibonacci-like sequences within arrays of numbers. A sequence is considered Fibonacci-like if it has at least 3 elements and each element (starting from the third) equals the sum of the two preceding elements.

Your mission: Given a strictly increasing array of positive integers, find the length of the longest Fibonacci-like subsequence. If no such subsequence exists, return 0.

Key Rules:

  • A Fibonacci-like sequence needs at least 3 elements
  • For any valid sequence [a, b, c, d, ...]: a + b = c, b + c = d, etc.
  • You can skip elements in the array (subsequence property)
  • Order must be preserved

Example: In array [1, 2, 3, 4, 5, 8], the subsequence [1, 2, 3, 5, 8] is Fibonacci-like because 1+2=3, 2+3=5, 3+5=8. Its length is 5.

Input & Output

example_1.py — Basic Fibonacci Sequence
$ Input: [1,2,3,4,5,8]
Output: 5
💡 Note: The longest Fibonacci-like subsequence is [1,2,3,5,8] with length 5. We have 1+2=3, 2+3=5, 3+5=8.
example_2.py — Multiple Sequences
$ Input: [1,3,7,11,12,14,18]
Output: 3
💡 Note: The longest Fibonacci-like subsequence has length 3: [1,11,12] because 1+11=12. Another valid sequence is [3,11,14] with 3+11=14.
example_3.py — No Valid Sequence
$ Input: [1,2,4,8,16]
Output: 0
💡 Note: No Fibonacci-like subsequence exists. Each element is double the previous (geometric sequence), but not sum-based (Fibonacci-like).

Constraints

  • 3 ≤ arr.length ≤ 1000
  • 1 ≤ arr[i] < arr[i + 1] ≤ 109
  • Array is strictly increasing
  • All elements are positive integers

Visualization

Tap to expand
🎵 Musical Fibonacci HarmonyNotes: [1♪, 2♪, 3♪, 5♪, 8♪] - Each frequency is sum of previous two1♪Base2♪Base3♪1+25♪2+38♪3+5Start+1+2+3🎼 Harmony Progression AnalysisStep 1: Start with base pair (1♪, 2♪)Step 2: Check if harmony 1+2=3♪ exists in catalog ✓Step 3: Extend progression to (2♪, 3♪), check 2+3=5♪ ✓Step 4: Extend to (3♪, 5♪), check 3+5=8♪ ✓Step 5: Try to extend (5♪, 8♪), check 5+8=13♪ ✗🎯 Result: Longest harmonic progression has 5 notes!DP Table tracks: dp[(note1, note2)] = length of progression ending with this pair
Understanding the Visualization
1
Catalog Notes
Create an index of all available note frequencies for quick lookup
2
Test Harmonies
For each pair of notes, check if their harmonic sum exists in our catalog
3
Extend Progressions
When a harmonic sum is found, extend the progression and track its length
4
Find Longest
Remember the longest harmonic progression discovered
Key Takeaway
🎯 Key Insight: Just like musical harmonies, each Fibonacci-like sequence is uniquely identified by its last two 'notes' - this allows us to efficiently track and extend sequences using dynamic programming!
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
42.4K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen