You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ i ≤ r).
Let's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l1, r1], is longer than segment [l2, r2], if len([l1, r1]) > len([l2, r2]).
Your task is to find a good segment of the maximum length in array a. Note that a segment of length 1 or 2 is always good.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains integers: a1, a2, ..., an (0 ≤ ai ≤ 109).
Output
Print the length of the longest good segment in array a.
Examples
Input
10
1 2 3 5 8 13 21 34 55 89
Output
10
Input
5
1 1 1 1 1
Output
2
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to read problem carefully. Given array a1..an. A segment [l, r] is "good" if for all i from l+2 to r, ai = a_{i-1} + a_{i-2}. So the segment is such that from the third element onwards, each element is the sum of the two previous ones. That's like Fibonacci-like but with given initial two values? Actually it's just that consecutive triple condition holds.
Length 1 or 2 segments are always good.
We need maximum length good segment.
Constraints: n up to 100k, ai up to 1e9.
We can iterate and find the longest contiguous subsequence where the condition holds.
We can scan and maintain current good segment length. The condition must hold for all indices i in segment (starting from third). So we can check each i from 3 to n whether a[i] == a[i-1] + a[i-2]. If true, we can extend the current segment; else we start a new segment at i-1? Actually careful: For a segment [l, r], the condition must hold for all i from l+2 to r. So if we are building contiguous segment, we can think of the maximal contiguous subarray where the condition holds for every triple. If the condition fails at some index i, then any segment that includes i must not be good (unless segment length <=2? Actually if the condition fails at i, that means for the triple (i-2,i-1,i), a[i] != a[i-1]+a[i-2]. Then any segment that includes these three indices (i-2,i-1,i) would be not good. However, a segment that ends at i but starts after i-2? For segment [l, r] with r >= i, we require condition for all indices from l+2 to r. If l > i-2 then i might not be required? Let's analyze: If we have failure at index i (i.e., condition fails for triple (i-2,i-1,i)), then any segment that includes i and also includes i-2 and i-1 would be invalid. But could there be a segment that includes i but not both i-2 and i-1? Since segment is contiguous, if it includes i, and its length >=3, then it must include some earlier indices. But to require the condition at position i, we need both i-2 and i-1 to be in the segment. So if the segment starts at l such that l <= i-2, then i-2 and i-1 are in the segment because l <= i-2 < i-1 < i, provided r >= i. If l > i-2, then either i-2 is not in the segment. For the condition to be checked at i, we need l+2 <= i, so l <= i-2. So if l > i-2, then i is not required to satisfy condition because l+2 > i. So a segment that starts at i-1? For example, segment [i-1, i] has length 2, which is always good. So the failure at i only breaks segments that include i and start at <= i-2. So we can treat the array as having "breakpoints" where the condition fails. The maximal contiguous segment where condition holds for all triples can be found by scanning and restarting when condition fails. But careful: Even if condition fails at i, we can still start a new segment at i-1 (or i) because length 1 or 2 are automatically good. So the algorithm: Let ans = min(2, n) maybe? Actually answer at least 2 if n>=2? But also could be 1 if n=1. We'll compute max length.
We can keep a variable curLen that tracks length of current good segment ending at current index.
Initialize: curLen = 1 (for first element). For i=2: we can always extend to length 2? Condition for segment length 2 is always good. So we can set curLen=2 at i=2 regardless.
For i>=3: Check if a[i] == a[i-1] + a[i-2]. If true, then the segment that we were extending (which ends at i-1) can be extended to include i, and the condition holds for i because the triple (i-2,i-1,i) is valid. So we increment curLen by 1. If false, then the previous segment cannot be extended; we must start a new segment at i-1? Actually we can have a new segment that starts at i-1 (length 2 includes i-1 and i? Wait we need to consider. The segment [i-1, i] length 2 is good, but that segment doesn't include i-2. However, if we start a new segment at i-1, then at i, we need to check if condition holds? For segment starting at i-1, we have l = i-1. For i (the second element after start), we need to check condition at i? For segment [l,r], condition applies for i such that l+2 <= i <= r. For l = i-1, l+2 = i+1, so i is not required. So at i, the segment is length 2 (if we include i). So it's good. So after failure, we can start a new segment that begins at i-1, with length 2 (including i-1 and i). But also we might consider starting at i (length 1). So the new curLen could be set to 2? But careful: The current segment we are tracking might have ended at i-1. When we encounter failure at i, the segment that ended at i-1 is still good, but cannot be extended to i. So we record its length. Then for the new segment, we need to consider that the next index i can be part of a new segment that starts at i-1? However, that new segment would include both i-1 and i. But i-1 was part of the previous segment. Can a segment be considered overlapping? They are contiguous non-overlapping if we are scanning linearly; typically we set curLen = 2 after a failure, meaning we consider the segment [i-1, i] as the current segment. But is that correct? Let's test: Suppose we had failure at i; then the segment [i-1, i] is good. But could there be a longer segment that starts earlier than i-1 and includes i but not i-2? No, because to include i, the segment must include all indices from l to i. If we want to avoid the failing triple (i-2,i-1,i), we need l > i-2. So the earliest l is i-1. So the maximum segment that includes i and is good must start at i-1 or i. Starting at i-1 gives length 2; starting at i gives length 1. So indeed after failure, the longest good segment that ends at i will be of length at most 2. However, we must be careful: Could a segment start at i-2 but still be good despite the failing condition? No, because if l <= i-2, then the condition at i must hold, but it fails. So that segment cannot be good. So the new segment's start must be >= i-1. So we set curLen = 2 after failure? But that assumes we include both i-1 and i. However, what about the possibility that the previous segment ended at i-1, and we want to start new segment at i? That would be length 1, which is smaller. So the optimal to maximize length from current position is to set curLen = 2. But is it always safe to set curLen = 2? Consider scenario where we had failure at i, but what about the condition for i+1? If we have curLen=2, meaning we consider segment [i-1, i]. For i+1, we need to check if a[i+1] == a[i] + a[i-1]. That's okay. So we can continue extending from that base. So the scanning algorithm: Initialize curLen = 1 for index 1. For i=2: curLen = 2. For i>=3: if a[i] == a[i-1] + a[i-2], then curLen++; else: record ans = max(ans, curLen); then set curLen = 2? But need to think: After failure, the current good segment that ends at i-1 is done. The new segment that ends at i is [i-1, i] length 2. But also maybe the previous segment could be considered as starting at i? Actually we might also want to consider that we could have a segment that starts at i and goes forward. But at this point we are scanning forward, and we want to compute the maximal segment that ends at i. So setting curLen=2 is correct for the segment ending at i (since it includes i-1 and i). However, is there a case where after failure, we could have curLen=1? Starting at i only. But that would be worse, so we might as well keep track of the longer one. However, it's safe to set curLen=2 because it represents a valid good segment ending at i. But we need to be careful: If n=2, we handle separately.
Edge cases: n=1 -> answer 1. n=2 -> answer 2.
Testing with examples: Example1: All satisfy condition. i=3: true, curLen=3; i=4: true curLen=4; ... i=10: curLen=10. ans=10. Good.
Example2: 1 1 1 1 1. Check i=3: 1 == 1+1? 1==2 false. So record curLen=2, set curLen=2? Wait before failure curLen was 2 (since at i=2, curLen=2). Then at i=3 we detect false, so ans=2, set curLen = ? According to our plan, set curLen = 2 (for segment [2,3]? Actually indices: i=3. After failure, new segment ending at i (3) would be [2,3] length 2. But is [2,3] good? Length 2 always good. Yes. So curLen = 2. Then i=4: Check a[4]==a[3]+a[2]? 1 == 1+1=2 false. So record ans = max(2,2)=2; set curLen = 2 (segment [3,4] length 2). i=5: Check a[5]==a[4]+a[3]? 1==1+1=2 false. So ans remains 2. So answer 2. Good.
But consider scenario: a = [2,1,3,4,7,...] Actually check: i=3: 3==1+2? 3==3 true, curLen=3. i=4: 4==3+1? 4==4 true, curLen=4. i=5: 7==4+3? 7==7 true, curLen=5. So works.
Consider scenario: a = [1,2,4,6,10,...] check: i=3: 4 == 2+1? 4==3 false. So record curLen=2 (previous), set curLen=2. i=4: 6 == 4+2? 6==6 true, so curLen becomes 3 (segment [2,4]? Wait need to track: After i=3 failure, curLen=2 representing segment [2,3] (indices 2,3?). Actually at i=3 we had curLen=2 after reset, meaning segment ending at i=3 is [2,3]? But careful: The segment that ends at i=3 and has length 2 could be either [2,3] or [3,3] extended? Typically we treat curLen as length of current good segment that ends at current index i. So after reset at i=3, curLen=2 means the segment ending at 3 is [2,3] (since length 2 includes indices 2 and 3). Then at i=4, condition check uses a[4] == a[3] + a[2]? Since we check i=4 using indices i-2=2 and i-1=3, which are exactly the two previous indices. In the segment [2,4], the condition at i=4 requires a[4] = a[3]+a[2]. That holds (6=4+2). So we can extend the segment from length 2 to 3. So now curLen=3, segment ends at 4, starting at 2? Actually length 3 means indices 2,3,4. That is valid: Check i=4: 6=4+2 ok. So good. So answer could be 3? But is there a longer? Possibly later.
Now consider a scenario: a = [1,2,3,5,8,13, 20,...] At i=7, 20 !=13+8? 20==21? false. So at i=7 failure. Then curLen before failure was length? For i=6, curLen=6 (assuming all previous satisfied). At i=7 failure, record ans=6, set curLen=2 (segment [6,7]? Actually indices: i=7, so after reset curLen=2 representing segment ending at 7: [6,7] length 2). Then continue.
Seems straightforward.
But wait: Is it always optimal to set curLen=2 after failure? What if the array has zeros? e.g., a = [0,0,0,0,0]. Condition: i=3: 0 == 0+0 true; curLen=3; i=4: 0==0+0 true; curLen=4; i=5: 0==0+0 true; curLen=5. So answer 5. No issue.
What about a case where after failure, the condition holds for the next triple but the segment might be extended further. Example: a = [1,1,2,3,5,8,13,...] At i=3: 2==1+1 true; curLen=3; i=4: 3==2+1 true; curLen=4; etc. Works.
One more tricky: The condition requires that for all i from l+2 to r, a_i = a_{i-1} + a_{i-2}. So it's a linear recurrence with coefficients 1,1. So if we have a segment of length L, the first two elements determine the rest (if it holds). So we can just scan.
Thus algorithm: ans = min(2,n)? Actually set ans=1 if n=1 else 2. But better to compute.
Initialize cur = 1 (since first element). ans = 1.
Loop i from 2 to n:
If i == 2: we can always extend? Actually condition for length 2 is always good. So we can set cur = 2. But we need to treat as extension: since cur was 1, we can just cur++? Yes because any segment of length 1 can be extended to length 2 without checking condition? Actually segment of length 2 is always good, so we can simply set cur = 2. So we can do: if i == 2 then cur = 2 else check condition.
Better: For i=2, we can just do cur = 2. For i>=3: if a[i] == a[i-1] + a[i-2] then cur++ else ans = max(ans, cur); cur = 2; (since at least length 2 ending at i). After loop, ans = max(ans, cur). Output ans.
But careful: When i=3 and condition fails, we set cur = 2 after recording previous cur which was 2 (from i=2). That's fine.
But also we must consider that after a failure, we might have a longer segment that starts at i (only one element) and then later extends. But our cur=2 is longer than 1, so it's fine.
Edge: n=1: loop from 2 to n doesn't run, ans initially 1, output 1.
Test: n=2: i=2: cur=2, ans initially 1, after loop ans=max(1,2)=2.
Works.
But is there any scenario where after failure we should set cur=1 instead of 2? Consider a case where i-1 and i themselves violate the condition? Actually condition only matters for triples. For two elements, no condition. So segment [i-1,i] always good. So cur=2 is always valid. So we can always set cur=2 after failure.
But what about the case where we have failure at i, but then at i+1 we need to check condition a[i+1] == a[i] + a[i-1]. This uses i-1 and i. Since we set cur=2 meaning segment [i-1,i] is current, then at i+1 we check using i-1 and i. That's correct. So we can continue.
Now, is there a possibility that after failure, the segment [i-1,i] could be invalid because of some condition at i? No, because condition for segment length 2 is always good.
Thus algorithm is correct.
But wait, maybe there is nuance: The definition says segment [l, r] is good if for all i (l+2 <= i <= r), a_i = a_{i-1} + a_{i-2}. So if segment length is 2, the set of i is empty, so trivially true. So yes.
So solution is O(n). Straightforward.
But is there any trick with integer overflow? a_i up to 1e9, sum up to 2e9, fits in 32-bit signed? Actually 2e9 > 2^31-1? 2^31-1=2147483647 ~2.147e9, 2e9 is less, but maybe sum could be up to 2e9? Actually a_i can be up to 1e9, so a_{i-1}+a_{i-2} can be up to 2e9, which fits in signed 32-bit int (max 2147483647). But in Python, no issue.
So we can implement.
But hold on, let's double-check with some edge cases: n=3, a = [1,2,4]. Check i=3: 4 == 2+1? 4==3 false. So cur at i=2 was 2, record ans=2, set cur=2 (segment [2,3]). After loop, ans = max(2, cur=2) = 2. So answer 2. That seems correct because longest good segment is either [1,2] or [2,3] length 2. [1,3] fails condition.
Another: n=3, a=[1,2,3]. Condition holds, cur becomes 3. ans=3.
Now, what about n=4, a=[1,2,3,6]. Check i=3: 3=2+1 true -> cur=3. i=4: 6=3+2? 6==5 false -> ans=3, cur=2 (segment [3,4]). After loop, ans=3. But is there a longer good segment? Check [2,4]? Condition at i=4: a4 should equal a3+a2 =3+2=5, fails. [1,4] fails. [3,4] length2. So answer 3 (segment [1,3] length3). Good.
Now consider a scenario where after failure, the new segment might be able to combine with the previous segment if we skip some? No.
Thus solution is simple.
But we need to be careful about reading input and output.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
a = list(map(int, data[1:1+n]))
if n <= 2:
print(n)
return
ans = 2 # at least 2 for n>=2, but we'll compute
cur = 2
for i in range(2, n): # i is index in 0-based? We'll use 0-based indexing: i from 2 to n-1
if a[i] == a[i-1] + a[i-2]:
cur += 1
else:
if cur > ans:
ans = cur
cur = 2
# after loop
if cur > ans:
ans = cur
print(ans)
But need to ensure for n=1, we print 1. So we can handle early if n==1: print(1); return; if n==2: print(2); return. Then loop for i=2..n-1 (0-index). That's fine.
Test with example 1: n=10, a from 1 to 89. Loop all true, cur becomes 10, ans initially 2, after loop ans=10, print 10. Works.
Example 2: n=5, a all 1. i=2: a[2]=1, a[1]+a[0]=1+1=2 -> false -> ans=max(2,cur=2)=2, cur=2. i=3: a[3]=1, a[2]+a[1]=1+1=2 false -> ans=2, cur=2. i=4: false -> ans=2, cur=2. End: ans=2. Print 2.
Edge: n=2: early return 2.
Edge: n=3 with all good: i=2 true -> cur=3, ans initially 2, after loop ans = max(2,3)=3. Print 3.
Edge: n=3 with fail: i=2 false -> ans=2, cur=2, after loop ans=max(2,2)=2. Print 2.
Thus solution accepted.
But wait, the problem statement says: "Note that a segment of length 1 or 2 is always good." So our handling matches.
Thus answer.
But is there any scenario where the maximum length could be 1? If n=1, answer 1. If n>=2, could answer be 1? Possibly if all consecutive triples fail and also maybe we have n=2, answer is 2. For n>=3, could the maximum be 1? Consider n=3, a = [1,2,4] we got answer 2 because there is segment of length 2. Actually any array with n>=2 has at least one segment of length 2, so answer at least 2. So for n>=2 answer >=2. The problem expects answer at least 2 for n>=2. So we can just initialize ans=2 for n>=2.
Implementation details: Use 0-indexed.
Complexities: O(n) time, O(1) extra.
But wait, we must consider that the condition holds only if the segment is of length at least 3. For length 1 and 2, it's automatically good. So maximum could be larger than n? No.
Thus solution done.
But is there a potential for longer segments if the array has zeros? e.g., a = [0,0,0,0] yields cur=4. Works.
Now, consider if a_i can be very large, sum may exceed 32-bit but Python handles.
Thus we have solution.
But I want to double-check the logic for resetting cur to 2 after failure. Suppose failure occurs at index i (i>=3). Then we start a new segment that ends at i with length 2. However, could there be a scenario where we should start a new segment of length 1 instead? For maximizing length, we want the longest segment that ends at current position. So length 2 is better than 1. But is it always valid to set cur=2? Yes because segment [i-1,i] is good. However, careful: In our scanning, we are maintaining cur as length of current good segment ending at the current index i. When we reset to 2, we are effectively saying that the current good segment ending at i is [i-1,i]. But what about the possibility that the previous segment ended at i-1? That segment is separate. That's fine.
But consider a scenario where we have failure at i, but also the condition for i+1 might depend on a[i] and a[i-1]. That's fine.
Now, one nuance: At the moment of failure at i, we record the length of the previous good segment (which ended at i-1). Then we set cur = 2. But note that the new segment starting at i-1 might actually be extended from the previous segment? No, because the previous segment cannot be extended due to failure, so it's separate.
Thus algorithm is correct.
Proof: We can treat array as having "breakpoints" where condition fails. The maximum good segment is either the whole array if no failures, or the longest segment between failures plus possibly the two elements before a failure? Actually the segment can cross failure? No, because failure at i means any segment containing i and starting at <= i-2 is invalid. So the maximum good segment must be either entirely before the failure, or starting at i-1 or i. So scanning works.
Edge case: At the very beginning, we treat i=2 as always good; but what if n=1? we handled.
Thus solution is trivial.
But the problem might be trickier: Could there be a segment that is good but not contiguous? No, segment is contiguous.
Hence done.
We can code now.
But I recall that Codeforces problem "Fibonacci-ish" maybe? Actually it's a Codeforces problem: "Longest Fibonacci-like segment" something like that. The solution is indeed scanning.
One more thought: Could there be a scenario where after failure we might need to set cur=1 instead of 2? For example, if i-1 and i themselves don't satisfy some condition? No, condition doesn't apply.
Thus final.
Implementation:
def solve():
import sys
input_data = sys.stdin.read().strip().split()
if not input_data:
return
n = int(input_data[0])
a = list(map(int, input_data[1:]))
if n == 1:
print(1)
return
if n == 2:
print(2)
return
max_len = 2
cur_len = 2
for i in range(2, n):
if a[i] == a[i-1] + a[i-2]:
cur_len += 1
else:
if cur_len > max_len:
max_len = cur_len
cur_len = 2
if cur_len > max_len:
max_len = cur_len
print(max_len)
But note: For n=2, we early return, so fine.
But we can also combine: Actually we don't need early return if we handle loop correctly. But it's fine.
n=5, a = [1,2,4,6,10] -> i=2: 4==2+1? true cur=3; i=3: 6==4+2? false -> max=3 cur=2; i=4: 10==6+4? false -> max=3 cur=2; final max=3. Check segments: [1,2,3] (indices 0-2) length3 good? a[2]=4 = a[1]+a[0]=2+1=3? No! Wait earlier we said true, but 4==3 false. So actually i=2 false. Let's recompute: a[0]=1, a[1]=2, a[2]=4: 4==2+1? false. So at i=2, condition false. So cur was initially 2 (from i=1? Actually after processing i=2 we set cur=2 earlier? Let's simulate: Initially cur=2 (after i=1, but we start loop from i=2). Before loop, cur=2. i=2 (index 2): condition false -> max=2 (cur=2) cur=2. Then i=3: check a[3]=6, a[2]+a[1]=4+2=6 true -> cur=3. i=4: a[4]=10, a[3]+a[2]=6+4=10 true -> cur=4. So max becomes 4. But segment [2,3,4,5] indices 1-4? Actually a[1]=2, a[2]=4, a[3]=6, a[4]=10. Check: i=3: 6==4+2 ok; i=4: 10==6+4 ok; so length 4? That would be indices 1 to 4 inclusive, which is length 4. But is that segment good? It requires that for all i from l+2 to r. If l=1 (index1), then need i=3 and i=4 conditions hold, which they do. So segment [2,4]? Actually l=1, r=4: length 4, includes a[1]=2, a[2]=4, a[3]=6, a[4]=10. Check: a[3]=6 = a[2]+a[1]=4+2=6, a[4]=10 = a[3]+a[2]=6+4=10. So yes, good. So answer 4. So the algorithm yields max=4. But is there any longer? No. So correct.
Thus algorithm works.
We need to ensure that we didn't miss the possibility of a good segment starting at index 0? That's included.
Now let's test with a more tricky case: n=4, a = [1,3,2,5]. Check: i=2: 2==3+1? false -> cur reset to 2; i=3: 5==2+3? 5==5 true -> cur=3; final max=3. Which segment? [3,2,5] indices 1-3? Check: a[3]=5, a[2]=2, a[1]=3: 5=2+3? yes; so length3 good. Also [1,3,2]? i=2 false, so not good. So answer 3.
Now consider case where after failure, we might be able to have a segment of length >2 starting at i? Not possible because need at least two preceding elements.
Thus solution is correct.
Now, can we implement more concisely? Some solutions might treat all indices together: cur = 2; for i in range(2,n): if a[i]==a[i-1]+a[i-2]: cur+=1 else: cur=2; ans = max(ans,cur). But careful: That would reset cur to 2 after failure, but we also need to record ans before reset. The ans can be updated each iteration: if condition fails, we record the cur before reset, but also we can record ans after each extension. But simpler to update ans when we reset and also at the end.
But note: If we do for i in range(2,n): if condition: cur+=1; else: ans=max(ans,cur); cur=2; Then after loop ans=max(ans,cur). That works.
Edge: if n=2, we don't enter loop, ans initial maybe 2, we output ans.
But we must initialize cur appropriately. For n>=2, cur=2 (since first two elements). So ans=2 initially.
For n=1, we handle separately.
Thus code:
if n == 1: print(1); return
ans = 2
cur = 2
for i in range(2, n):
if a[i] == a[i-1] + a[i-2]:
cur += 1
else:
if cur > ans:
ans = cur
cur = 2
ans = max(ans, cur)
print(ans)
This yields same.
Now check for n=2: loop not executed, ans initially 2, after loop ans = max(2, cur=2)=2, prints 2. Good.
For n=3 with all good: i=2 condition true -> cur=3; after loop ans = max(2,3)=3, prints 3.
For n=3 with fail: i=2 false -> ans=max(2,2)=2, cur=2; after loop ans=max(2,2)=2, prints 2.
Thus simpler.
But need to be careful: For n=2, we do not have a[i-2] defined for i=2? But we don't loop, so fine.
Thus we can even unify: if n <= 2: print(n) else: as above.
Ok.
Now, about input size up to 100k, reading via sys.stdin is fine.
I think that's the solution.
But wait, is there any possibility for longer good segment that is not captured by contiguous scanning? The definition is contiguous segment, so scanning works.
Thus answer.
Now, I'll produce final answer with explanation.
But note: The problem might be from Codeforces Round? Possibly they expect answer to be at most n, but we can do O(n).
Thus final.
Now I'll output final solution.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1 + n]))
code
# Trivial cases
if n == 1:
print(1)
return
if n == 2:
print(2)
return
# n >= 3
ans = 2 # at least a segment of length 2 exists
cur = 2 # length of the current good segment ending at position 1 (0‑based)
for i in range(2, n): # i is index in 0‑based array, i >= 2
if a[i] == a[i - 1] + a[i - 2]:
cur += 1
else:
if cur > ans:
ans = cur
cur = 2 # start a new good segment [i-1, i]
# final update
if cur > ans:
ans = cur
print(ans)