📌  相关文章
📜  最长斐波那契子数组的长度

📅  最后修改于: 2022-05-13 01:56:05.903000             🧑  作者: Mango

最长斐波那契子数组的长度

给定一个整数元素数组arr[] ,任务是找到arr[]的最大子数组的长度,使得子数组的所有元素都是斐波那契数。

例子:

方法:这个问题可以通过遍历数组arr[]来解决。请按照以下步骤解决此问题。

  • 初始化变量max_lengthcurrent_length0以存储子数组的最大长度和子数组的当前长度,使得子数组中的每个元素都是斐波那契数。
  • 使用变量i[0, N-1]范围内迭代:
    • 如果当前数字是斐波那契数,则将current_length增加1 ,否则将current_length设置为0。
    • 现在,将max_length指定为current_lengthmax_length 的最大值。
  • 完成上述步骤后,打印max_length作为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// A utility function that returns
// true if x is perfect square
bool isPerfectSquare(int x)
{
    int s = sqrt(x);
    return (s * s == x);
}
 
// Returns true if n is a
// Fibonacci Number, else false
bool isFibonacci(int n)
{
    // Here n is Fibinac ci if one of 5*n*n + 4
    // or 5*n*n - 4 or both is a perfect square
    return isPerfectSquare(5 * n * n + 4)
           || isPerfectSquare(5 * n * n - 4);
}
 
// Function to find the length of the
// largest sub-array of an array every
// element of whose is a Fibonacci number
int contiguousFibonacciNumber(int arr[], int n)
{
 
    int current_length = 0;
    int max_length = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
        // Check if arr[i] is a Fibonacci number
        if(isFibonacci(arr[i])) {
            current_length++;
        }
        else{
           current_length = 0;
        }
 
        // Stores the maximum length of the
        // Fibonacci number subarray
        max_length = max(max_length, current_length);
    }
   
    // Finally, return the maximum length
    return max_length;
}
 
// Driver code
int main()
{
 
    // Given Input
    int arr[] = { 11, 8, 21, 5, 3, 28, 4};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << contiguousFibonacciNumber(arr, n);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.io.*;
 
class GFG
{
 
  // A utility function that returns
  // true if x is perfect square
  public static boolean isPerfectSquare(int x)
  {
    int s =(int) Math.sqrt(x);
    return (s * s == x);
  }
 
  // Returns true if n is a
  // Fibonacci Number, else false
  public static boolean isFibonacci(int n)
  {
 
    // Here n is Fibonacci if one of 5*n*n + 4
    // or 5*n*n - 4 or both is a perfect square
    return isPerfectSquare(5 * n * n + 4)
      || isPerfectSquare(5 * n * n - 4);
  }
 
  // Function to find the length of the
  // largest sub-array of an array every
  // element of whose is a Fibonacci number
  public static int contiguousFibonacciNumber(int arr[], int n)
  {
 
    int current_length = 0;
    int max_length = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
      // Check if arr[i] is a Fibonacci number
      if (isFibonacci(arr[i])) {
        current_length++;
      }
      else {
        current_length = 0;
      }
 
      // Stores the maximum length of the
      // Fibonacci number subarray
      max_length = Math.max(max_length, current_length);
    }
 
    // Finally, return the maximum length
    return max_length;
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    // Given Input
    int arr[] = { 11, 8, 21, 5, 3, 28, 4 };
    int n = arr.length;
 
    // Function Call
    System.out.println( contiguousFibonacciNumber(arr, n));
  }
}
 
 // This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
import math
 
# A utility function that returns
# true if x is perfect square
def isPerfectSquare(x):
     
    s = int(math.sqrt(x))
     
    if s * s == x:
        return True
    else:
        return False
 
# Returns true if n is a
# Fibonacci Number, else false
def isFibonacci(n):
     
    # Here n is fibonacci if one of 5*n*n+4
    # or 5*n*n-4 or both is a perfect square
    return (isPerfectSquare(5 * n * n + 4) or
            isPerfectSquare(5 * n * n - 4))
   
# Function to find the length of the
# largest sub-array of an array every
# element of whose is a Fibonacci number
def contiguousFibonacciNumber(arr, n):
     
    current_length = 0
    max_length = 0
     
    # Traverse the array arr
    for i in range(0, n):
         
        # Check if arr[i] is a Fibonacci number
        if isFibonacci(arr[i]):
            current_length += 1
        else:
            current_length = 0
             
        # stores the maximum length of the
        # Fibonacci number subarray
        max_length = max(max_length, current_length)
         
    # Finally, return the maximum length
    return max_length
 
# Driver code
if __name__ == '__main__':
 
    # Given Input
    arr = [ 11, 8, 21, 5, 3, 28, 4 ]
    n = len(arr)
     
    # Function Call
    print(contiguousFibonacciNumber(arr, n))
 
# This code is contributed by MuskanKalra1


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// A utility function that returns
// true if x is perfect square
static bool isPerfectSquare(int x)
{
    int s = (int)Math.Sqrt(x);
    return(s * s == x);
}
 
// Returns true if n is a
// Fibonacci Number, else false
static bool isFibonacci(int n)
{
     
    // Here n is Fibonacci if one of 5*n*n + 4
    // or 5*n*n - 4 or both is a perfect square
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
 
// Function to find the length of the
// largest sub-array of an array every
// element of whose is a Fibonacci number
static int contiguousFibonacciNumber(int []arr, int n)
{
    int current_length = 0;
    int max_length = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Check if arr[i] is a Fibonacci number
        if (isFibonacci(arr[i]))
        {
            current_length++;
        }
        else
        {
            current_length = 0;
        }
 
        // Stores the maximum length of the
        // Fibonacci number subarray
        max_length = Math.Max(max_length,
                              current_length);
    }
 
    // Finally, return the maximum length
    return max_length;
}
 
// Driver code
public static void Main()
{
     
    // Given Input
    int []arr = { 11, 8, 21, 5, 3, 28, 4 };
    int n = arr.Length;
 
    // Function Call
    Console.Write(contiguousFibonacciNumber(arr, n));
}
}
         
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出
4

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