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++
// C++ program to check whether the // sum of fibonacci elements of the // array is a Fibonacci number or not #include <bits/stdc++.h> #define ll long long int #define MAX 100005 using namespace std; // Hash to store the Fibonacci // numbers up to Max set<int> fibonacci; // Function to create the hash table // to check Fibonacci numbers void createHash() { // Inserting the first two Fibonacci // numbers into the hash int prev = 0, curr = 1; fibonacci.insert(prev); fibonacci.insert(curr); // Add the remaining Fibonacci numbers // based on the previous two numbers while (curr <= MAX) { int temp = curr + prev; fibonacci.insert(temp); prev = curr; curr = temp; } } // Function to check if the sum of // Fibonacci numbers is Fibonacci or not bool checkArray(int arr[], int n) { // Find the sum of // all Fibonacci numbers ll sum = 0; // Iterating through the array for (int i = 0; i < n; i++) if (fibonacci.find(arr[i]) != fibonacci.end()) sum += arr[i]; // If the sum is Fibonacci // then return true if (fibonacci.find(sum) != fibonacci.end()) return true; return false; } // Driver code int main() { // array of elements int arr[] = { 1, 2, 4, 8, 2 }; int n = sizeof(arr) / sizeof(arr[0]); // Creating a set containing // all fibonacci numbers createHash(); if (checkArray(arr, n)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java program to check whether the // sum of fibonacci elements of the // array is a Fibonacci number or not import java.util.*; class GFG{ static final int MAX = 100005; // Hash to store the Fibonacci // numbers up to Max static HashSet<Integer> fibonacci = new HashSet<Integer>(); // Function to create the hash table // to check Fibonacci numbers static void createHash() { // Inserting the first two Fibonacci // numbers into the hash int prev = 0, curr = 1; fibonacci.add(prev); fibonacci.add(curr); // Add the remaining Fibonacci numbers // based on the previous two numbers while (curr <= MAX) { int temp = curr + prev; fibonacci.add(temp); prev = curr; curr = temp; } } // Function to check if the sum of // Fibonacci numbers is Fibonacci or not static boolean checkArray(int arr[], int n) { // Find the sum of // all Fibonacci numbers int sum = 0; // Iterating through the array for (int i = 0; i < n; i++) if (fibonacci.contains(arr[i])) sum += arr[i]; // If the sum is Fibonacci // then return true if (fibonacci.contains(sum)) return true; return false; } // Driver code public static void main(String[] args) { // array of elements int arr[] = { 1, 2, 4, 8, 2 }; int n = arr.length; // Creating a set containing // all fibonacci numbers createHash(); if (checkArray(arr, n)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Rajput-Ji |
Python3
# Python 3 program to check whether the # sum of fibonacci elements of the # array is a Fibonacci number or not MAX = 100005 # Hash to store the Fibonacci # numbers up to Max fibonacci = set() # Function to create the hash table # to check Fibonacci numbers def createHash(): global fibonacci # Inserting the first two Fibonacci # numbers into the hash prev , curr = 0, 1 fibonacci.add(prev) fibonacci.add(curr) # Add the remaining Fibonacci numbers # based on the previous two numbers while (curr <= MAX): temp = curr + prev if temp <= MAX: fibonacci.add(temp) prev = curr curr = temp # Function to check if the sum of # Fibonacci numbers is Fibonacci or not def checkArray(arr, n): # Find the sum of # all Fibonacci numbers sum = 0 # Iterating through the array for i in range( n ): if (arr[i] in fibonacci): sum += arr[i] # If the sum is Fibonacci # then return true if (sum in fibonacci): return True return False # Driver code if __name__ == "__main__": # array of elements arr = [ 1, 2, 4, 8, 2 ] n = len(arr) # Creating a set containing # all fibonacci numbers createHash() if (checkArray(arr, n)): print("Yes") else: print("No") # This code is contributed by chitranayal |
C#
// C# program to check whether the // sum of fibonacci elements of the // array is a Fibonacci number or not using System; using System.Collections.Generic; class GFG{ static readonly int MAX = 100005; // Hash to store the Fibonacci // numbers up to Max static HashSet<int> fibonacci = new HashSet<int>(); // Function to create the hash table // to check Fibonacci numbers static void createHash() { // Inserting the first two Fibonacci // numbers into the hash int prev = 0, curr = 1; fibonacci.Add(prev); fibonacci.Add(curr); // Add the remaining Fibonacci numbers // based on the previous two numbers while (curr <= MAX) { int temp = curr + prev; fibonacci.Add(temp); prev = curr; curr = temp; } } // Function to check if the sum of // Fibonacci numbers is Fibonacci or not static bool checkArray(int []arr, int n) { // Find the sum of // all Fibonacci numbers int sum = 0; // Iterating through the array for (int i = 0; i < n; i++) if (fibonacci.Contains(arr[i])) sum += arr[i]; // If the sum is Fibonacci // then return true if (fibonacci.Contains(sum)) return true; return false; } // Driver code public static void Main(String[] args) { // array of elements int []arr = { 1, 2, 4, 8, 2 }; int n = arr.Length; // Creating a set containing // all fibonacci numbers createHash(); if (checkArray(arr, n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by sapnasingh4991 |
Yes
Recommended Posts:
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check whether Array represents a Fibonacci Series or not
- Check if a number is Full Fibonacci or not
- Check whether the sum of prime elements of the array is prime or not
- Check if sum of exactly K elements of the Array can be odd or not
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- GCD of elements occurring Fibonacci number of times in an Array
- How to check if a given number is Fibonacci number?
- Check if Array elements can be maximized upto M by adding all elements from another array
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Check if all elements of the array are palindrome or not
- Check if product of Array elements in given range are M-th root or not
- Check if all array elements are pairwise co-prime or not
- Check if all array elements are present in a given stack or not
- Count of elements not divisible by any other elements of Array
- Number of ways to represent a number as sum of k fibonacci numbers
- Check if each element of an Array is the Sum of any two elements of another Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


