Skip to content

873. Length of Longest Fibonacci Subsequence #1368

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the length of the longest Fibonacci-like subsequence in a strictly increasing array. A Fibonacci-like sequence is defined such that each element after the first two is the sum of the two preceding ones.

Approach

  1. Dynamic Programming (DP) with Hash Map:

    • Hash Map: Use a hash map to store the indices of elements for quick lookup. This allows us to check if the difference between two elements (which would be the preceding element in a Fibonacci sequence) exists in the array.
    • DP Table: Use a 2D DP array where dp[j][k] represents the length of the longest Fibonacci-like subsequence ending with elements at indices j and k. Initialize all elements to 2 since any two elements can…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Feb 27, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Feb 27, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants