Check whether Array represents a Fibonacci Series or not - GeeksforGeeks

03 May, 2021
Given an array arr[] consisting of N integers, the task is to check whether a Fibonacci series can be formed using all the array elements or not. If possible, print “Yes”. Otherwise, print “No”.Examples:  
Input: arr[] = { 8, 3, 5, 13 } Output: Yes Explanation: Rearrange given array as {3, 5, 8, 13} and these numbers form Fibonacci series.Input: arr[] = { 2, 3, 5, 11 } Output: No Explanation: The given array elements do not form a Fibonacci series.  
 
Approach: In order to solve the problem mentioned above, the main idea is to sort the given array. After sorting, check if every element is equal to the sum of the previous 2 elements. If so, then the array elements form a Fibonacci series.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to check if the// elements of a given array// can form a Fibonacci Series #include <bits/stdc++.h>using namespace std; // Returns true if a permutation// of arr[0..n-1] can form a// Fibonacci Seriesbool 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 Codeint 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 program to check if the elements of// a given array can form a Fibonacci Seriesimport java. util. Arrays; class GFG{     // Returns true if a permutation// of arr[0..n-1] can form a// Fibonacci Seriespublic 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 codepublic 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 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 Seriesdef 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 Codeif __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# program to check if the elements of// a given array can form a fibonacci seriesusing System; class GFG{     // Returns true if a permutation// of arr[0..n-1] can form a// fibonacci seriespublic 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 codepublic 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
<script> // Javascript 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    function checkIsFibonacci(arr , n)    {        if (n == 1 || n == 2)            return true;         // Sort array        arr.sort((a, b) => a - b);         // After sorting, check if every        // element is equal to the sum        // of previous 2 elements        for (i = 2; i < n; i++) {            if ((arr[i - 1] + arr[i - 2]) != arr[i])                return false;        }        return true;    }     // Driver code             var arr = [ 8, 3, 5, 13 ];        var n = arr.length;         if (checkIsFibonacci(arr, n))            document.write("Yes");        else            document.write("No"); // This code contributed by umadevi9616 </script>
Yes
 
Time Complexity: O(N Log N) 
divyeshrabadiya07
ankthon
umadevi9616
Fibonacci
series
Arrays
Mathematical
Arrays
Mathematical
series
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in Java
Introduction to Arrays
Maximum and minimum of an array using minimum number of comparisons
Python | Using 2D arrays/lists the right way
Linked List vs Array
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Modulo Operator (%) in C/C++ with Examples
Program to find GCD or HCF of two numbers