📌  相关文章
📜  查找给定数组中最长的类似于Fibonacci的子数组

📅  最后修改于: 2021-05-05 01:54:19             🧑  作者: Mango

给定N个元素的数组,任务是找到最长的类似于Fibonacci的子数组。

类斐波那契子数组定义为以下数组:

A[i]=A[i-1]+A[i-2] where i>2

and, A[1] and A[2] can be anything.

例子:

Input : N = 5, arr[] = {2, 4, 6, 10, 2}
Output : 4
The sub-array 2, 4, 6, 10 is Fibonacci like.

Input : N = 3, arr[] = {0, 0, 0}
Output : 3
The entire array is Fibonacci-like.

方法:

想法是观察到任何长度小于或等于2的数组都是斐波那契式的。现在,对于长度大于2的数组:

  1. 保持变量len初始化为2和变量mx来存储到目前为止的最大长度。
  2. 从第三个索引开始遍历数组。
  3. 如果像这样的斐波那契数组可以扩展此索引,即如果a [i] = a [i-1] + a [i-2]
    • 然后将变量len的值增加1。
    • 否则,将变量len重新初始化为2。
    • 将mx和len的最大值存储在变量mx中以进行当前迭代。

下面是上述方法的实现:

C++
// C++ program to find length of longest
// Fibonacci-like subarray
  
#include 
using namespace std;
  
// Function to find the length of the 
// longest Fibonacci-like subarray
int longestFibonacciSubarray(int n, int a[])
{
    // Any 2 terms are Fibonacci-like
    if (n <= 2)
        return n;
      
    int len = 2;
      
    int mx = INT_MIN;
      
    for (int i = 2; i < n; i++) {
          
        // If previous subarray can be extended
        if (a[i] == a[i - 1] + a[i - 2])
            len++;
              
        // Any 2 terms are Fibonacci-like
        else
            len = 2;
              
        // Find the maximum length
        mx = max(mx, len);
    }
      
    return mx;
}
  
// Driver Code
int main()
{
    int n = 5;
    int a[] = {2, 4, 6, 10, 2};
      
    cout << longestFibonacciSubarray(n, a);
      
    return 0;
}


Java
// Java program to find length of longest 
// Fibonacci-like subarray 
class GFG 
{
      
    // Function to find the length of the 
    // longest Fibonacci-like subarray 
    static int longestFibonacciSubarray(int n, int a[]) 
    { 
        // Any 2 terms are Fibonacci-like 
        if (n <= 2) 
            return n; 
          
        int len = 2; 
          
        int mx = Integer.MIN_VALUE; 
          
        for (int i = 2; i < n; i++)
        { 
              
            // If previous subarray can be extended 
            if (a[i] == a[i - 1] + a[i - 2]) 
                len++; 
                  
            // Any 2 terms are Fibonacci-like 
            else
                len = 2; 
                  
            // Find the maximum length 
            mx = Math.max(mx, len); 
        } 
        return mx; 
    } 
      
    // Driver Code 
    public static void main (String[] args) 
    { 
        int n = 5; 
        int a[] = {2, 4, 6, 10, 2}; 
          
        System.out.println(longestFibonacciSubarray(n, a)); 
    } 
}
  
// This code is contributed by Ryuga


Python3
# Python3 program to find Length of 
# longest Fibonacci-like subarray
  
# Function to find the Length of the 
# longest Fibonacci-like subarray
def longestFibonacciSubarray(n, a):
  
    # Any 2 terms are Fibonacci-like
    if (n <= 2):
        return n
      
    Len = 2
      
    mx = -10**9
      
    for i in range(2, n):
          
        # If previous subarray can be extended
        if (a[i] == a[i - 1] + a[i - 2]):
            Len += 1
              
        # Any 2 terms are Fibonacci-like
        else:
            Len = 2
              
        # Find the maximum Length
        mx = max(mx, Len)
      
    return mx
  
# Driver Code
n = 5
a = [2, 4, 6, 10, 2]
  
print(longestFibonacciSubarray(n, a))
  
# This code is contributed by Mohit Kumar


C#
// C# program to find length of longest 
// Fibonacci-like subarray 
using System;
  
class GFG 
{
      
    // Function to find the length of the 
    // longest Fibonacci-like subarray 
    static int longestFibonacciSubarray(int n, int[] a) 
    { 
        // Any 2 terms are Fibonacci-like 
        if (n <= 2) 
            return n; 
          
        int len = 2; 
          
        int mx = int.MinValue; 
          
        for (int i = 2; i < n; i++)
        { 
              
            // If previous subarray can be extended 
            if (a[i] == a[i - 1] + a[i - 2]) 
                len++; 
                  
            // Any 2 terms are Fibonacci-like 
            else
                len = 2; 
                  
            // Find the maximum length 
            mx = Math.Max(mx, len); 
        } 
        return mx; 
    } 
      
    // Driver Code 
    public static void Main () 
    { 
        int n = 5; 
        int[] a = {2, 4, 6, 10, 2}; 
          
        Console.WriteLine(longestFibonacciSubarray(n, a)); 
    } 
}
  
// This code is contributed by Code_Mech.


PHP


输出:
4

时间复杂度: O(N)
辅助空间:O(1)