Given an even number N, the task is to find two Fibonacci numbers whose sum can be represented as N. There may be several combinations possible. Print only first such pair. If there is no solution then print -1.
Examples:
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
Approach: The idea is to use hashing to precompute and store the Fibonacci numbers, and then check if a pair is a Fibonacci value in O(1) time.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void createHash(set< int >& hash,
int maxElement)
{
int prev = 0, curr = 1;
hash.insert(prev);
hash.insert(curr);
while (curr < maxElement) {
int temp = curr + prev;
hash.insert(temp);
prev = curr;
curr = temp;
}
}
void findFibonacciPair( int n)
{
set< int > hash;
createHash(hash, n);
for ( int i = 0; i < n; i++) {
if (hash.find(i) != hash.end()
&& hash.find(n - i) != hash.end()) {
cout << i << ", "
<< (n - i) << endl;
return ;
}
}
cout << "-1\n" ;
}
int main()
{
int N = 90;
findFibonacciPair(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void createHash(HashSet<Integer> hash,
int maxElement)
{
int prev = 0 , curr = 1 ;
hash.add(prev);
hash.add(curr);
while (curr < maxElement) {
int temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
static void findFibonacciPair( int n)
{
HashSet<Integer> hash = new HashSet<Integer>();
createHash(hash, n);
for ( int i = 0 ; i < n; i++) {
if (hash.contains(i)
&& hash.contains(n - i)) {
System.out.print(i+ ", "
+ (n - i) + "\n" );
return ;
}
}
System.out.print( "-1\n" );
}
public static void main(String[] args)
{
int N = 90 ;
findFibonacciPair(N);
}
}
|
Python3
def createHash(hash1,maxElement):
prev , curr = 0 , 1
hash1.add(prev)
hash1.add(curr)
while (curr < maxElement):
temp = curr + prev
hash1.add(temp)
prev = curr
curr = temp
def findFibonacciPair( n):
hash1 = set ()
createHash(hash1, n)
for i in range (n):
if (i in hash1 and (n - i) in hash1):
print (i , ", " , (n - i))
return
print ( "-1" )
if __name__ = = "__main__" :
N = 90
findFibonacciPair(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void createHash(HashSet< int > hash,
int maxElement)
{
int prev = 0, curr = 1;
hash.Add(prev);
hash.Add(curr);
while (curr < maxElement) {
int temp = curr + prev;
hash.Add(temp);
prev = curr;
curr = temp;
}
}
static void findFibonacciPair( int n)
{
HashSet< int > hash = new HashSet< int >();
createHash(hash, n);
for ( int i = 0; i < n; i++) {
if (hash.Contains(i)
&& hash.Contains(n - i)) {
Console.Write(i+ ", "
+ (n - i) + "\n" );
return ;
}
}
Console.Write( "-1\n" );
}
public static void Main(String[] args)
{
int N = 90;
findFibonacciPair(N);
}
}
|
Javascript
<script>
function createHash(hash, maxElement)
{
let prev = 0, curr = 1;
hash.add(prev);
hash.add(curr);
while (curr < maxElement) {
let temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
function findFibonacciPair(n)
{
let hash = new Set();
createHash(hash, n);
for (let i = 0; i < n; i++) {
if (hash.has(i)
&& hash.has(n - i)) {
document.write(i+ ", "
+ (n - i) + "<br/>" );
return ;
}
}
document.write( "-1" + "<br/>" );
}
let N = 90;
findFibonacciPair(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)