给定一个数字N ,任务是找出0 到 N范围内总和为N的斐波那契对的数量。
例子:
Input: N = 90
Output: 1
Explanation:
Only Fibonacci pair in range [0, 90] whose sum is equal to 90 is {1, 89}
Input: N = 3
Output: 2
Explanation:
Fibonacci Pair in range [0, 3] with whose sum is equal to 3 are {0, 3}, {1, 2}
方法:
- 这个想法是使用散列来预先计算和存储小于等于N的斐波那契数列在一个散列中
- 将计数器变量初始化为 0
- 然后对于散列中的每个元素K ,检查N – K是否也存在于散列中。
- 如果 K 和 N – K 都在散列中,则增加计数器变量
下面是上述方法的实现:
C++
// C++ program to find count of
// Fibonacci pairs 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 count of Fibonacci
// pair with the given sum
int findFibonacciPairCount(int N)
{
// creating a set containing
// all fibonacci numbers
set hash;
createHash(hash, N);
// Initialize count with 0
int count = 0;
// traverse the hash to find
// pairs with sum as N
set::iterator itr;
for (itr = hash.begin();
*itr <= (N / 2);
itr++) {
// If both *itr and
//(N - *itr) are Fibonacci
// increment the count
if (hash.find(N - *itr)
!= hash.end()) {
// Increase the count
count++;
}
}
// Return the count
return count;
}
// Driven code
int main()
{
int N = 90;
cout << findFibonacciPairCount(N)
<< endl;
N = 3;
cout << findFibonacciPairCount(N)
<< endl;
return 0;
}
Java
// Java program to find count of
// Fibonacci pairs 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 count of Fibonacci
// pair with the given sum
static int findFibonacciPairCount(int N)
{
// creating a set containing
// all fibonacci numbers
HashSet hash = new HashSet();
createHash(hash, N);
// Initialize count with 0
int count = 0;
int i = 0;
// traverse the hash to find
// pairs with sum as N
for (int itr : hash) {
i++;
// If both *itr and
//(N - *itr) are Fibonacci
// increment the count
if (hash.contains(N - itr)) {
// Increase the count
count++;
}
if(i == hash.size()/2)
break;
}
// Return the count
return count;
}
// Driven code
public static void main(String[] args)
{
int N = 90;
System.out.print(findFibonacciPairCount(N)
+"\n");
N = 3;
System.out.print(findFibonacciPairCount(N)
+"\n");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find count of
# Fibonacci pairs whose sum can be
# represented as N
# Function to create hash table
# to check Fibonacci numbers
def createHash(Hash, maxElement):
# Storing the first two numbers
# in the hash
prev, curr = 0, 1
Hash.add(prev)
Hash.add(curr)
# Finding Fibonacci numbers up to N
# and storing them in the hash
while (curr < maxElement):
temp = curr + prev
Hash.add(temp)
prev = curr
curr = temp
# Function to find count of Fibonacci
# pair with the given sum
def findFibonacciPairCount(N):
# Creating a set containing
# all fibonacci numbers
Hash = set()
createHash(Hash, N)
# Initialize count with 0
count = 0
i = 0
# Traverse the hash to find
# pairs with sum as N
for itr in Hash:
i += 1
# If both *itr and
#(N - *itr) are Fibonacci
# increment the count
if ((N - itr) in Hash):
# Increase the count
count += 1
if (i == (len(Hash) // 2)):
break
# Return the count
return count
# Driver Code
N = 90
print(findFibonacciPairCount(N))
N = 3
print(findFibonacciPairCount(N))
# This code is contributed by divyeshrabadiya07
C#
// C# program to find count of
// Fibonacci pairs 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 count of Fibonacci
// pair with the given sum
static int findFibonacciPairCount(int N)
{
// creating a set containing
// all fibonacci numbers
HashSet hash = new HashSet();
createHash(hash, N);
// Initialize count with 0
int count = 0;
int i = 0;
// traverse the hash to find
// pairs with sum as N
foreach (int itr in hash) {
i++;
// If both *itr and
//(N - *itr) are Fibonacci
// increment the count
if (hash.Contains(N - itr)) {
// Increase the count
count++;
}
if(i == hash.Count/2)
break;
}
// Return the count
return count;
}
// Driven code
public static void Main(String[] args)
{
int N = 90;
Console.Write(findFibonacciPairCount(N)
+"\n");
N = 3;
Console.Write(findFibonacciPairCount(N)
+"\n");
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
1
2
性能分析:
- 时间复杂度:在上述方法中,构建小于N的斐波那契数的哈希图将是O(N)操作。然后,对于 hashmap 中的每个元素,我们搜索另一个元素,使搜索时间复杂度为O(N * log N) 。所以总的时间复杂度是O(N * log N)
- 辅助空间复杂度:在上述方法中,我们使用额外的空间来存储哈希图值。所以辅助空间复杂度是O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。