Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array of positive integers nums.
A Fibonacci array is a contiguous sequence whose third and subsequent terms each equal the sum of the two preceding terms.
Return the length of the longest Fibonacci subarray in nums.
Note: Subarrays of length 1 or 2 are always Fibonacci.
Example 1:
Input: nums = [1,1,1,1,2,3,5,1]
Output: 5
Explanation:
The longest Fibonacci subarray is nums[2..6] = [1, 1, 2, 3, 5].
[1, 1, 2, 3, 5] is Fibonacci because 1 + 1 = 2, 1 + 2 = 3, and 2 + 3 = 5.
Example 2:
Input: nums = [5,2,7,9,16]
Output: 5
Explanation:
The longest Fibonacci subarray is nums[0..4] = [5, 2, 7, 9, 16].
[5, 2, 7, 9, 16] is Fibonacci because 5 + 2 = 7, 2 + 7 = 9, and 7 + 9 = 16.
Example 3:
Input: nums = [1000000000,1000000000,1000000000]
Output: 2
Explanation:
The longest Fibonacci subarray is nums[1..2] = [1000000000, 1000000000].
[1000000000, 1000000000] is Fibonacci because its length is 2.
Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: You are given an array of positive integers nums. A Fibonacci array is a contiguous sequence whose third and subsequent terms each equal the sum of the two preceding terms. Return the length of the longest Fibonacci subarray in nums. Note: Subarrays of length 1 or 2 are always Fibonacci.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,1,1,1,2,3,5,1]
[5,2,7,9,16]
[1000000000,1000000000,1000000000]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3708: Longest Fibonacci Subarray
class Solution {
public int longestSubarray(int[] nums) {
int f = 2;
int ans = f;
for (int i = 2; i < nums.length; ++i) {
if (nums[i] == nums[i - 1] + nums[i - 2]) {
++f;
ans = Math.max(ans, f);
} else {
f = 2;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3708: Longest Fibonacci Subarray
func longestSubarray(nums []int) int {
f := 2
ans := f
for i := 2; i < len(nums); i++ {
if nums[i] == nums[i-1]+nums[i-2] {
f++
ans = max(ans, f)
} else {
f = 2
}
}
return ans
}
# Accepted solution for LeetCode #3708: Longest Fibonacci Subarray
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
ans = f = 2
for i in range(2, n):
if nums[i] == nums[i - 1] + nums[i - 2]:
f = f + 1
ans = max(ans, f)
else:
f = 2
return ans
// Accepted solution for LeetCode #3708: Longest Fibonacci Subarray
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3708: Longest Fibonacci Subarray
// class Solution {
// public int longestSubarray(int[] nums) {
// int f = 2;
// int ans = f;
// for (int i = 2; i < nums.length; ++i) {
// if (nums[i] == nums[i - 1] + nums[i - 2]) {
// ++f;
// ans = Math.max(ans, f);
// } else {
// f = 2;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3708: Longest Fibonacci Subarray
function longestSubarray(nums: number[]): number {
let f = 2;
let ans = f;
for (let i = 2; i < nums.length; ++i) {
if (nums[i] === nums[i - 1] + nums[i - 2]) {
ans = Math.max(ans, ++f);
} else {
f = 2;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.