给定一个偶数N ,任务是找到两个斐波那契数,其总和可以表示为N 。可能有几种可能的组合。只打印第一个这样的对。如果没有解决方案,则打印-1 。
例子:
Input: N = 90
Output: 1, 89
Explanation:
The first pair with whose sum is equal to 90 is {1, 89}
Input: N = 74
Output: -1
方法:想法是使用散列来预先计算并存储斐波那契数,然后在 O(1) 时间内检查一对是否为斐波那契值。
下面是上述方法的实现:
CPP
// C++ program to find two
// Fibonacci numbers whose
// sum can be represented as N
#include
using namespace std;
// Function to create hash table
// to check Fibonacci numbers
void createHash(set& hash,
int maxElement)
{
// Storing the first two numbers
// in the hash
int prev = 0, curr = 1;
hash.insert(prev);
hash.insert(curr);
// Finding Fibonacci numbers up to N
// and storing them in the hash
while (curr < maxElement) {
int temp = curr + prev;
hash.insert(temp);
prev = curr;
curr = temp;
}
}
// Function to find the Fibonacci pair
// with the given sum
void findFibonacciPair(int n)
{
// creating a set containing
// all fibonacci numbers
set hash;
createHash(hash, n);
// Traversing all numbers
// to find first pair
for (int i = 0; i < n; i++) {
// If both i and (N - i) are Fibonacci
if (hash.find(i) != hash.end()
&& hash.find(n - i) != hash.end()) {
// Printing the pair because
// i + (N - i) = N
cout << i << ", "
<< (n - i) << endl;
return;
}
}
// If no fibonacci pair is found
// whose sum is equal to n
cout << "-1\n";
}
// Driven code
int main()
{
int N = 90;
findFibonacciPair(N);
return 0;
}
Java
// Java program to find two
// Fibonacci numbers whose
// sum can be represented as N
import java.util.*;
class GFG{
// Function to create hash table
// to check Fibonacci numbers
static void createHash(HashSet hash,
int maxElement)
{
// Storing the first two numbers
// in the hash
int prev = 0, curr = 1;
hash.add(prev);
hash.add(curr);
// Finding Fibonacci numbers up to N
// and storing them in the hash
while (curr < maxElement) {
int temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
// Function to find the Fibonacci pair
// with the given sum
static void findFibonacciPair(int n)
{
// creating a set containing
// all fibonacci numbers
HashSet hash = new HashSet();
createHash(hash, n);
// Traversing all numbers
// to find first pair
for (int i = 0; i < n; i++) {
// If both i and (N - i) are Fibonacci
if (hash.contains(i)
&& hash.contains(n - i)) {
// Printing the pair because
// i + (N - i) = N
System.out.print(i+ ", "
+ (n - i) +"\n");
return;
}
}
// If no fibonacci pair is found
// whose sum is equal to n
System.out.print("-1\n");
}
// Driven code
public static void main(String[] args)
{
int N = 90;
findFibonacciPair(N);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find two
# Fibonacci numbers whose
# sum can be represented as N
# Function to create hash table
# to check Fibonacci numbers
def createHash(hash1,maxElement):
# Storing the first two numbers
# in the hash
prev , curr = 0 , 1
hash1.add(prev)
hash1.add(curr)
# Finding Fibonacci numbers up to N
# and storing them in the hash
while (curr < maxElement):
temp = curr + prev
hash1.add(temp)
prev = curr
curr = temp
# Function to find the Fibonacci pair
# with the given sum
def findFibonacciPair( n):
# creating a set containing
# all fibonacci numbers
hash1 = set()
createHash(hash1, n)
# Traversing all numbers
# to find first pair
for i in range(n):
# If both i and (N - i) are Fibonacci
if (i in hash1 and (n - i) in hash1):
# Printing the pair because
# i + (N - i) = N
print(i , ", ", (n - i))
return
# If no fibonacci pair is found
# whose sum is equal to n
print("-1")
# Driven code
if __name__ == "__main__":
N = 90
findFibonacciPair(N)
# This code is contributed by chitranayal
C#
// C# program to find two
// Fibonacci numbers whose
// sum can be represented as N
using System;
using System.Collections.Generic;
class GFG{
// Function to create hash table
// to check Fibonacci numbers
static void createHash(HashSet hash,
int maxElement)
{
// Storing the first two numbers
// in the hash
int prev = 0, curr = 1;
hash.Add(prev);
hash.Add(curr);
// Finding Fibonacci numbers up to N
// and storing them in the hash
while (curr < maxElement) {
int temp = curr + prev;
hash.Add(temp);
prev = curr;
curr = temp;
}
}
// Function to find the Fibonacci pair
// with the given sum
static void findFibonacciPair(int n)
{
// creating a set containing
// all fibonacci numbers
HashSet hash = new HashSet();
createHash(hash, n);
// Traversing all numbers
// to find first pair
for (int i = 0; i < n; i++) {
// If both i and (N - i) are Fibonacci
if (hash.Contains(i)
&& hash.Contains(n - i)) {
// Printing the pair because
// i + (N - i) = N
Console.Write(i+ ", "
+ (n - i) +"\n");
return;
}
}
// If no fibonacci pair is found
// whose sum is equal to n
Console.Write("-1\n");
}
// Driven code
public static void Main(String[] args)
{
int N = 90;
findFibonacciPair(N);
}
}
// This code is contributed by Princi Singh
Javascript
输出:
1, 89
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。