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.
Input: N = 199
Output: 55 144
199 can be represented as sum 55 and 144 which has the minimum difference.
Input: N = 1830
Output: 233 1597
1830 can be represented as sum 233 and 1597 which has the minimum difference.
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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Queries for maximum and minimum difference between Fibonacci numbers in given ranges
- Longest subsequence such that absolute difference between every pair is atmost 1
- Check if a pair with given absolute difference exists in a Matrix
- Minimum absolute difference of adjacent elements in a circular array
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Sort an array according to absolute difference with given value
- Triplets in array with absolute difference less than k
- Remove all the fibonacci numbers from the given array
- Sum of numbers in the Kth level of a Fibonacci triangle
- Find two Fibonacci numbers whose sum can be represented as N
- Sum of absolute difference of all pairs raised to power K
- Maximum absolute difference of value and index sums
- Largest subset whose all elements are Fibonacci numbers
- Largest and smallest Fibonacci numbers in an Array
- Last digit of sum of numbers in the given range in the Fibonacci series
- Count pairs in an array whose absolute difference is divisible by K
- Maximum absolute difference between distinct elements in an Array
- Length of longest subsequence of Fibonacci Numbers in an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.