Related Articles

# Check whether Array represents a Fibonacci Series or not

• Last Updated : 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++

 `// 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);` `    ``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`

## Javascript

 ``
Output:

`Yes`

Time Complexity: O(N Log N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up