Find two Fibonacci numbers whose sum can be represented as N | Java program to find two Fibonacci numbers whose sum can be represented as N ; Function to create hash table to check Fibonacci numbers ; Storing the first two numbers in the hash ; Finding Fibonacci numbers up to N and storing them in the hash ; Function to find the Fibonacci pair with the given sum ; creating a set containing all fibonacci numbers ; Traversing all numbers to find first pair ; If both i and ( N - i ) are Fibonacci ; Printing the pair because i + ( N - i ) = N ; If no fibonacci pair is found whose sum is equal to n ; Driven code
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 ) + "NEW_LINE"); return ; } } System . out . print ( "-1NEW_LINE"); } public static void main ( String [ ] args ) { int N = 90 ; findFibonacciPair ( N ) ; } }