##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/873.html

# 873. Length of Longest Fibonacci Subsequence

Medium

## Description

A sequence X_1, X_2, ..., X_n is fibonacci-like if:

• n >= 3
• X_i + X_{i+1} = X_{i+2} for all i + 2 <= n

Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.

(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)

Example 1:

Input: [1,2,3,4,5,6,7,8]

Output: 5

Explanation:

The longest subsequence that is fibonacci-like: [1,2,3,5,8].

Example 2:

Input: [1,3,7,11,12,14,18]

Output: 3

Explanation:

The longest subsequence that is fibonacci-like: [1,11,12], [3,11,14] or [7,11,18].

Note:

• 3 <= A.length <= 1000
• 1 <= A < A < ... < A[A.length - 1] <= 10^9
• (The time limit has been reduced by 50% for submissions in Java, C, and C++.)

## Solution

Use a set to store all the numbers in array A. For each indices pair (i, j) where i < j, try to generate a fibonacci-like subsequence of A, where all the numbers from the third number to the end are determined by the sum of the two previous numbers. A fibonacci-like subsequence ends until a number is not in the set. If the subsequence has length greater than or equal to 3, update the longest length. Finally, return the longest length.

• class Solution {
public int lenLongestFibSubseq(int[] A) {
int length = A.length;
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < length; i++)
int longestLength = 0;
int end1 = length - 2, end2 = length - 1;
for (int i = 0; i < end1; i++) {
for (int j = i + 1; j < end2; j++) {
int num1 = A[i];
int num2 = A[j];
int curLength = 2;
while (set.contains(num1 + num2)) {
int num3 = num1 + num2;
curLength++;
num1 = num2;
num2 = num3;
}
if (curLength >= 3)
longestLength = Math.max(longestLength, curLength);
}
}
return longestLength;
}
}

• class Solution:
def lenLongestFibSubseq(self, arr: List[int]) -> int:
mp = {v: i for i, v in enumerate(arr)}
n = len(arr)
dp = [ * n for _ in range(n)]
for i in range(n):
for j in range(i):
dp[j][i] = 2
ans = 0
for i in range(n):
for j in range(i):
d = arr[i] - arr[j]
if d in mp and (k := mp[d]) < j:
dp[j][i] = max(dp[j][i], dp[k][j] + 1)
ans = max(ans, dp[j][i])
return ans

############

class Solution(object):
def lenLongestFibSubseq(self, A):
"""
:type A: List[int]
:rtype: int
"""
s = set(A)
n = len(A)
res = 0
for i in range(n - 1):
for j in range(i + 1, n):
a, b = A[i], A[j]
count = 2
while a + b in s:
a, b = b, a + b
count += 1
res = max(res, count)
return res if res > 2 else 0

• class Solution {
public:
int lenLongestFibSubseq(vector<int>& arr) {
unordered_map<int, int> mp;
int n = arr.size();
for (int i = 0; i < n; ++i) mp[arr[i]] = i;
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < i; ++j)
dp[j][i] = 2;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
int d = arr[i] - arr[j];
if (mp.count(d)) {
int k = mp[d];
if (k < j) {
dp[j][i] = max(dp[j][i], dp[k][j] + 1);
ans = max(ans, dp[j][i]);
}
}
}
}
return ans;
}
};

• func lenLongestFibSubseq(arr []int) int {
n := len(arr)
mp := make(map[int]int, n)
for i, v := range arr {
mp[v] = i + 1
}
dp := make([][]int, n)
for i := 0; i < n; i++ {
dp[i] = make([]int, n)
for j := 0; j < i; j++ {
dp[j][i] = 2
}
}
ans := 0
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
d := arr[i] - arr[j]
k := mp[d] - 1
if k >= 0 && k < j {
dp[j][i] = max(dp[j][i], dp[k][j]+1)
ans = max(ans, dp[j][i])
}
}
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

• /**
* @param {number[]} arr
* @return {number}
*/
var lenLongestFibSubseq = function (arr) {
const mp = new Map();
const n = arr.length;
const dp = new Array(n).fill(0).map(() => new Array(n).fill(0));
for (let i = 0; i < n; ++i) {
mp.set(arr[i], i);
for (let j = 0; j < i; ++j) {
dp[j][i] = 2;
}
}
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < i; ++j) {
const d = arr[i] - arr[j];
if (mp.has(d)) {
const k = mp.get(d);
if (k < j) {
dp[j][i] = Math.max(dp[j][i], dp[k][j] + 1);
ans = Math.max(ans, dp[j][i]);
}
}
}
}
return ans;
};