Pair of fibonacci numbers with a given sum and minimum absolute difference
Last Updated :
18 Apr, 2023
Given an integer N(less than 10^6), the task is to find a pair of Fibonacci numbers whose sum is equal to the given N, and the absolute difference between the chosen pair is minimum.
Print -1 if there is no solution.
Examples:
Input: N = 199
Output: 55 144
Explanation
199 can be represented as sum 55 and 144 which has the minimum difference.
Input: N = 1830
Output: 233 1597
Explanation
1830 can be represented as sum 233 and 1597 which has the minimum difference.
Approach: The idea is to use hashing to precompute and store the Fibonacci nodes up to the maximum value to make checking easy and efficient (in O(1) time).
After precomputing the hash:
- Start a loop from (N / 2) to 1 (to minimize the absolute difference) and check whether the loop counter ‘i’ and ‘N – i’ are both Fibonacci.
- If they are Fibonacci, then we will print them and break out of the loop.
- If the number N cannot be represented as the sum of two Fibonacci numbers, then we will print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000005
set< int > fib;
void fibonacci()
{
int prev = 0, curr = 1, len = 2;
fib.insert(prev);
fib.insert(curr);
while (len <= MAX) {
int temp = curr + prev;
fib.insert(temp);
prev = curr;
curr = temp;
len++;
}
}
void findFibonacci( int N)
{
for ( int i = N / 2; i > 1; i--) {
if (fib.find(i) != fib.end()
&& fib.find(N - i) != fib.end()) {
cout << i << " " << (N - i) << endl;
return ;
}
}
cout << "-1" << endl;
}
int main()
{
fibonacci();
int sum = 199;
findFibonacci(sum);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAX= 1000005 ;
static Set<Integer> fib= new HashSet<Integer>();
static void fibonacci()
{
int prev = 0 , curr = 1 , len = 2 ;
fib.add(prev);
fib.add(curr);
while (len <= MAX) {
int temp = curr + prev;
fib.add(temp);
prev = curr;
curr = temp;
len++;
}
}
static void findFibonacci( int N)
{
for ( int i = N / 2 ; i > 1 ; i--) {
if (fib.contains(i) && fib.contains(N - i)) {
System.out.println(i+ " " +(N - i));
return ;
}
}
System.out.println( "-1" );
}
public static void main(String args[])
{
fibonacci();
int sum = 199 ;
findFibonacci(sum);
}
}
|
Python3
MAX = 10005
fib = set ()
def fibonacci():
global fib
global MAX
prev = 0
curr = 1
l = 2
fib.add(prev)
fib.add(curr)
while (l < = MAX ):
temp = curr + prev
fib.add(temp)
prev = curr
curr = temp
l + = 1
def findFibonacci(N):
global fib
i = N / / 2
while (i > 1 ):
if (i in fib and
(N - i) in fib):
print (i, (N - i))
return
i - = 1
print ( "-1" )
if __name__ = = '__main__' :
fibonacci()
sum = 199
findFibonacci( sum )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MAX=100005;
static HashSet< int > fib= new HashSet< int >();
static void fibonacci()
{
int prev = 0, curr = 1, len = 2;
fib.Add(prev);
fib.Add(curr);
while (len <= MAX) {
int temp = curr + prev;
fib.Add(temp);
prev = curr;
curr = temp;
len++;
}
}
static void findFibonacci( int N)
{
for ( int i = N / 2; i > 1; i--) {
if (fib.Contains(i) && fib.Contains(N - i)) {
Console.WriteLine(i+ " " +(N - i));
return ;
}
}
Console.WriteLine( "-1" );
}
public static void Main(String []args)
{
fibonacci();
int sum = 199;
findFibonacci(sum);
}
}
|
Javascript
<script>
let MAX=1000005;
let fib= new Set();
function fibonacci()
{
let prev = 0, curr = 1, len = 2;
fib.add(prev);
fib.add(curr);
while (len <= MAX) {
let temp = curr + prev;
fib.add(temp);
prev = curr;
curr = temp;
len++;
}
}
function findFibonacci(N)
{
for (let i = Math.floor(N / 2); i > 1; i--) {
if (fib.has(i) && fib.has(N - i)) {
document.write(i+ " " +(N - i));
return ;
}
}
document.write( "-1" );
}
fibonacci();
let sum = 199;
findFibonacci(sum);
</script>
|
Time Complexity : O(N)
Space Complexity : O(MAX)
Share your thoughts in the comments
Please Login to comment...