#C9402. Longest Fibonacci-Like Subsequence
Longest Fibonacci-Like Subsequence
Longest Fibonacci-Like Subsequence
You are given an array of positive integers. Your task is to determine the length of the longest Fibonacci-like subsequence within the array. A Fibonacci-like subsequence is defined as a sequence with at least three elements where every element from the third onward satisfies the following relation:
\(F_i = F_{i-1} + F_{i-2}\) for all \(i \ge 3\).
If no such subsequence exists, output 0.
Note that the subsequence does not need to be contiguous in the original array.
inputFormat
The input is read from standard input and consists of:
- A single integer \(n\) on the first line representing the number of elements in the array.
- A single line containing \(n\) space-separated integers.
outputFormat
Output a single integer to standard output representing the length of the longest Fibonacci-like subsequence. If no such subsequence exists, output 0.
## sample7
1 3 7 11 12 14 18
3