Find the longest contiguous segment whose values follow the Fibonacci rule after the first two elements.
You are given an array of integers. Your task is to find the length of the longest contiguous segment in which every element from the third one onward is equal to the sum of the two previous elements.
In other words, for a segment , it is valid if for every with , the condition holds.
Return the maximum possible length of such a segment. If no segment longer than 1 exists, the answer is 1. If a pair of consecutive elements forms a valid segment of length 2, that is also allowed.
Print a single integer — the maximum length of a contiguous Fibonacci-like segment.
Example 1
Input
5 1 2 3 5 8
Output
5
Explanation
The entire array is a valid Fibonacci-like segment.
Example 2
Input
6 4 1 5 6 11 1
Output
4
Explanation
The segment [1, 5, 6, 11] has length 4 and satisfies the rule: 6 = 1 + 5, 11 = 5 + 6.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.