给定一个包含N 个元素的数组arr[] ,任务是检查数组的斐波那契元素之和是否为斐波那契数。
例子:
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
方法:这个想法是使用散列来预先计算并存储最大数量的斐波那契节点,以使检查变得容易和高效(在 O(1) 时间内)。
预计算后,迭代数组的所有元素。如果数字是斐波那契数,则将其添加到总和中。最后,检查总和是否为斐波那契数。
下面是上述方法的实现:
C++
// C++ program to check whether the
// sum of fibonacci elements of the
// array is a Fibonacci number or not
#include
#define ll long long int
#define MAX 100005
using namespace std;
// Hash to store the Fibonacci
// numbers up to Max
set 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 fibonacci = new HashSet();
// 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 fibonacci = new HashSet();
// 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
Javascript
输出:
Yes