Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
Given an array arr[] containing N elements, the task is to check if the sum of Fibonacci elements of the array is a fibonacci number or not.
Examples:
Input: arr[] = {2, 3, 7, 11}
Output: Yes
Explanation:
As there are two Fibonacci numbers in the array i.e. 2 and 3.
So, the sum of Fibonacci numbers is 2 + 3 = 5 and 5 is also a Fibonacci number.
Input: arr[] = {1, 2, 3}
Output: No
Approach: The idea is to use hashing to precompute and store the Fibonacci nodes up to the maximum number to make checking easy and efficient (in O(1) time).
After precomputation, iterate over all elements of the array. If the number is a Fibonacci number then add it to the sum. And finally, check whether the sum is a Fibonacci number or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
#define MAX 100005
using namespace std;
set< int > fibonacci;
void createHash()
{
int prev = 0, curr = 1;
fibonacci.insert(prev);
fibonacci.insert(curr);
while (curr <= MAX) {
int temp = curr + prev;
fibonacci.insert(temp);
prev = curr;
curr = temp;
}
}
bool checkArray( int arr[], int n)
{
ll sum = 0;
for ( int i = 0; i < n; i++)
if (fibonacci.find(arr[i])
!= fibonacci.end())
sum += arr[i];
if (fibonacci.find(sum)
!= fibonacci.end())
return true ;
return false ;
}
int main()
{
int arr[] = { 1, 2, 4, 8, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
createHash();
if (checkArray(arr, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int MAX = 100005 ;
static HashSet<Integer> fibonacci = new HashSet<Integer>();
static void createHash()
{
int prev = 0 , curr = 1 ;
fibonacci.add(prev);
fibonacci.add(curr);
while (curr <= MAX) {
int temp = curr + prev;
fibonacci.add(temp);
prev = curr;
curr = temp;
}
}
static boolean checkArray( int arr[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
if (fibonacci.contains(arr[i]))
sum += arr[i];
if (fibonacci.contains(sum))
return true ;
return false ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 8 , 2 };
int n = arr.length;
createHash();
if (checkArray(arr, n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
MAX = 100005
fibonacci = set ()
def createHash():
global fibonacci
prev , curr = 0 , 1
fibonacci.add(prev)
fibonacci.add(curr)
while (curr < = MAX ):
temp = curr + prev
if temp < = MAX :
fibonacci.add(temp)
prev = curr
curr = temp
def checkArray(arr, n):
sum = 0
for i in range ( n ):
if (arr[i] in fibonacci):
sum + = arr[i]
if ( sum in fibonacci):
return True
return False
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 8 , 2 ]
n = len (arr)
createHash()
if (checkArray(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static readonly int MAX = 100005;
static HashSet< int > fibonacci = new HashSet< int >();
static void createHash()
{
int prev = 0, curr = 1;
fibonacci.Add(prev);
fibonacci.Add(curr);
while (curr <= MAX) {
int temp = curr + prev;
fibonacci.Add(temp);
prev = curr;
curr = temp;
}
}
static bool checkArray( int []arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
if (fibonacci.Contains(arr[i]))
sum += arr[i];
if (fibonacci.Contains(sum))
return true ;
return false ;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 4, 8, 2 };
int n = arr.Length;
createHash();
if (checkArray(arr, n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
let MAX = 100005;
let fibonacci = new Set();
function createHash()
{
let prev = 0, curr = 1;
fibonacci.add(prev);
fibonacci.add(curr);
while (curr <= MAX) {
let temp = curr + prev;
fibonacci.add(temp);
prev = curr;
curr = temp;
}
}
function checkArray(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
if (fibonacci.has(arr[i]))
sum += arr[i];
if (fibonacci.has(sum))
return true ;
return false ;
}
let arr = [ 1, 2, 4, 8, 2 ];
let n = arr.length;
createHash();
if (checkArray(arr, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(nlogn), where n represents the size of the given array.
Auxiliary Space: O(n), where n represents the size of the given array, since no extra space has been taken.
Last Updated :
16 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...