📜  检查数组是否代表斐波那契数列

📅  最后修改于: 2021-05-13 22:16:46             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是检查是否可以使用所有数组元素形成斐波那契级数。如果可能,请打印“是”。否则,打印“否”。

例子:

方法:
为了解决上述问题,主要思想是给定的数组进行排序。排序后,检查每个元素是否等于前两个元素的总和。如果是这样,则数组元素形成斐波那契数列。

下面是上述方法的实现:

C++
// C++ program to check if the
// elements of a given array
// can form a Fibonacci Series
  
#include 
using namespace std;
  
// Returns true if a permutation
// of arr[0..n-1] can form a
// Fibonacci Series
bool checkIsFibonacci(int arr[], int n)
{
    if (n == 1 || n == 2)
        return true;
  
    // Sort array
    sort(arr, arr + n);
  
    // After sorting, check if every
    // element is equal to the
    // sum of previous 2 elements
  
    for (int i = 2; i < n; i++)
        if ((arr[i - 1] + arr[i - 2])
            != arr[i])
            return false;
  
    return true;
}
  
// Driver Code
int main()
{
    int arr[] = { 8, 3, 5, 13 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    if (checkIsFibonacci(arr, n))
        cout << "Yes" << endl;
    else
        cout << "No";
  
    return 0;
}


Java
// Java program to check if the elements of 
// a given array can form a Fibonacci Series 
import java. util. Arrays;
  
class GFG{
      
// Returns true if a permutation 
// of arr[0..n-1] can form a 
// Fibonacci Series 
public static boolean checkIsFibonacci(int arr[], 
                                       int n) 
{ 
    if (n == 1 || n == 2) 
        return true; 
      
    // Sort array 
    Arrays.sort(arr);
      
    // After sorting, check if every 
    // element is equal to the sum 
    // of previous 2 elements 
    for(int i = 2; i < n; i++)
    {
       if ((arr[i - 1] + arr[i - 2]) != arr[i]) 
           return false; 
    } 
    return true; 
} 
      
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 8, 3, 5, 13 }; 
    int n = arr.length; 
      
    if (checkIsFibonacci(arr, n)) 
        System.out.println("Yes");
    else
        System.out.println("No"); 
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to check if the
# elements of a given array
# can form a Fibonacci Series
  
# Returns true if a permutation
# of arr[0..n-1] can form a
# Fibonacci Series
def checkIsFibonacci(arr, n) :
  
    if (n == 1 or n == 2) :
        return True;
  
    # Sort array
    arr.sort()
  
    # After sorting, check if every
    # element is equal to the
    # sum of previous 2 elements
  
    for i in range(2, n) :
        if ((arr[i - 1] + 
             arr[i - 2])!= arr[i]) :
            return False;
  
    return True;
  
# Driver Code
if __name__ == "__main__" :
  
    arr = [ 8, 3, 5, 13 ];
    n = len(arr);
  
    if (checkIsFibonacci(arr, n)) :
        print("Yes");
    else :
        print("No");
  
# This code is contributed by AnkitRai01


C#
// C# program to check if the elements of 
// a given array can form a fibonacci series 
using System;
  
class GFG{ 
      
// Returns true if a permutation 
// of arr[0..n-1] can form a 
// fibonacci series 
public static bool checkIsFibonacci(int []arr, 
                                    int n) 
{ 
    if (n == 1 || n == 2) 
        return true; 
          
    // Sort array 
    Array.Sort(arr); 
          
    // After sorting, check if every 
    // element is equal to the sum 
    // of previous 2 elements 
    for(int i = 2; i < n; i++) 
    { 
       if ((arr[i - 1] + arr[i - 2]) != arr[i]) 
           return false; 
    } 
    return true; 
} 
          
// Driver code 
public static void Main(string[] args) 
{ 
    int []arr = { 8, 3, 5, 13 }; 
    int n = arr.Length; 
          
    if (checkIsFibonacci(arr, n)) 
        Console.WriteLine("Yes"); 
    else
        Console.WriteLine("No"); 
} 
} 
  
// This code is contributed by AnkitRai01


输出:
Yes

时间复杂度: O(N Log N)