Length of Longest Fibonacci Subsequence

Given a strictly increasing array A of positive integers forming a sequence,

A sequence X1, X2, X3, …, XN if fibonacci like if

  • N > =3
  • Xi + Xi+1 = Xi+2 for all i+2<=N

</b>

Find the length of the longest Fibonacci-like subsequence of A.
If one does not exist, return 0.

Note: 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.



Input Format

The only argument given is the integer array A.

Output Format

Return the length of the longest Fibonacci-like subsequence of A.  
If one does not exist, return 0.

Constraints

3 <= length of the array <= 1000
1 <= A[i] <= 10^9 

For Example

Input 1:
    A = [1, 2, 3, 4, 5, 6, 7, 8]
Output 1:
    5
    Explanation 1:
        The longest subsequence that is fibonacci-like: [1, 2, 3, 5, 8].

Input 2:
    A = [1, 3, 7, 11, 12, 14, 18]
Output 2:
    Explanation 2:
        The longest subsequence that is fibonacci-like:
        [1, 11, 12], [3, 11, 14] or [7, 11, 18].
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Length of Longest Fibonacci Subsequence on Interview Code Editor
Sign Up
to access hints and editorial solutions for Length of Longest Fibonacci Subsequence

Discussion


Loading...
Click here to start solving coding interview questions