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.
Algorithm:
- Define a function named checkIsFibonacci that takes an array of integers and its size as input.
- Check if the size of the array is 1 or 2. If yes, return true as an array of 1 or 2 elements can always form a Fibonacci series.
- Sort the array in ascending order using the sort() function from the algorithm header.
- Traverse the sorted array from index 2 to n-1.
- Check if the current element is equal to the sum of the previous two elements of the array. If not, return false.
- If all the elements pass the above condition, return true.
- In the main function:
a. Define an array of integers and its size.
b. Call the checkIsFibonacci() function with the array and its size as arguments.
c. If the function returns true, print “Yes” to the console. Otherwise, print “No”.
8. End of the program.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkIsFibonacci( int arr[], int n)
{
if (n == 1 || n == 2)
return true ;
sort(arr, arr + n);
for ( int i = 2; i < n; i++)
if ((arr[i - 1] + arr[i - 2])
!= arr[i])
return false ;
return true ;
}
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
import java. util. Arrays;
class GFG{
public static boolean checkIsFibonacci( int arr[],
int n)
{
if (n == 1 || n == 2 )
return true ;
Arrays.sort(arr);
for ( int i = 2 ; i < n; i++)
{
if ((arr[i - 1 ] + arr[i - 2 ]) != arr[i])
return false ;
}
return true ;
}
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" );
}
}
|
Python3
def checkIsFibonacci(arr, n) :
if (n = = 1 or n = = 2 ) :
return True ;
arr.sort()
for i in range ( 2 , n) :
if ((arr[i - 1 ] +
arr[i - 2 ])! = arr[i]) :
return False ;
return True ;
if __name__ = = "__main__" :
arr = [ 8 , 3 , 5 , 13 ];
n = len (arr);
if (checkIsFibonacci(arr, n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
public static bool checkIsFibonacci( int []arr,
int n)
{
if (n == 1 || n == 2)
return true ;
Array.Sort(arr);
for ( int i = 2; i < n; i++)
{
if ((arr[i - 1] + arr[i - 2]) != arr[i])
return false ;
}
return true ;
}
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" );
}
}
|
Javascript
<script>
function checkIsFibonacci(arr , n)
{
if (n == 1 || n == 2)
return true ;
arr.sort((a, b) => a - b);
for (i = 2; i < n; i++) {
if ((arr[i - 1] + arr[i - 2]) != arr[i])
return false ;
}
return true ;
}
var arr = [ 8, 3, 5, 13 ];
var n = arr.length;
if (checkIsFibonacci(arr, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N Log N)
Auxiliary Space: O(1)
Approach 2: Using Stacks;
Here’s how the stack checks if an array can form a Fibonacci series:
We start by pushing the first two elements of the array onto the stack.
Then, for each subsequent element in the array, we check if it is equal to the sum of the two elements on top of the stack.
If it is, we push the element onto the stack.
If it isn’t, we return false, indicating that the array cannot form a Fibonacci series.
If we reach the end of the array without returning false, we return true, indicating that the array can form a Fibonacci series.
C++
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
bool checkIsFibonacci( int arr[], int n) {
if (n == 1 || n == 2) {
return true ;
}
sort(arr, arr+n);
stack< int > s;
for ( int i = 0; i < n; i++) {
if (i < 2) {
s.push(arr[i]);
} else {
if (s.top() + s.size() - 2 == arr[i]) {
s.push(arr[i]);
} else {
return false ;
}
}
}
return true ;
}
int main() {
int arr[] = {8, 3, 5, 13};
int n = sizeof (arr)/ sizeof (arr[0]);
if (checkIsFibonacci(arr, n)) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
return 0;
}
|
Java
import java.util.*;
public class CheckFibonacci {
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" );
}
}
public static boolean checkIsFibonacci( int [] arr, int n) {
if (n == 1 || n == 2 ) {
return true ;
}
Arrays.sort(arr);
Stack<Integer> stack = new Stack<>();
for ( int i = 0 ; i < n; i++) {
if (i < 2 ) {
stack.push(arr[i]);
} else {
if (stack.peek() + stack.get(stack.size() - 2 ) == arr[i]) {
stack.push(arr[i]);
} else {
return false ;
}
}
}
return true ;
}
}
|
Python3
def checkIsFibonacci(arr, n) :
if (n = = 1 or n = = 2 ) :
return True ;
arr.sort()
stack = []
for i in range (n):
if i < 2 :
stack.append(arr[i])
else :
if stack[ - 1 ] + stack[ - 2 ] = = arr[i]:
stack.append(arr[i])
else :
return False
return True ;
if __name__ = = "__main__" :
arr = [ 8 , 3 , 5 , 13 ]
n = len (arr)
if (checkIsFibonacci(arr, n)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static bool CheckIsFibonacci( int [] arr, int n) {
if (n == 1 || n == 2) {
return true ;
}
Array.Sort(arr);
Stack< int > s = new Stack< int >();
for ( int i = 0; i < n; i++) {
if (i < 2) {
s.Push(arr[i]);
} else {
if (s.Peek() + s.ElementAt(s.Count - 2) == arr[i]) {
s.Push(arr[i]);
} else {
return false ;
}
}
}
return true ;
}
static void Main( string [] args) {
int [] arr = {8, 3, 5, 13};
int n = arr.Length;
if (CheckIsFibonacci(arr, n)) {
Console.WriteLine( "No" );
} else {
Console.WriteLine( "Yes" );
}
}
}
|
Javascript
function checkIsFibonacci(arr, n) {
if (n == 1 || n == 2) {
return true ;
}
arr.sort((a, b) => a - b);
let stack = [];
for (let i = 0; i < n; i++) {
if (i < 2) {
stack.push(arr[i]);
} else {
if (stack[stack.length - 1] + stack[stack.length - 2] == arr[i]) {
stack.push(arr[i]);
} else {
return false ;
}
}
}
return true ;
}
let arr = [8, 3, 5, 13];
let n = arr.length;
if (checkIsFibonacci(arr, n)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(N Log N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Apr, 2023
Like Article
Save Article