![]() |
VOOZH | about |
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:
1, 89
Time Complexity: O(N)
Auxiliary Space: O(N)