Largest subset whose all elements are Fibonacci numbers | C # program to find largest Fibonacci subset ; Prints largest subset of an array whose all elements are fibonacci numbers ; Find maximum element in arr [ ] ; Generate all Fibonacci numbers till max and store them ; Now iterate through all numbers and quickly check for Fibonacci ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { public static void findFibSubset ( int [ ] x ) { List < int > fib = new List < int > ( ) ; List < int > result = new List < int > ( ) ; int max = x . Max ( ) ; int a = 0 ; int b = 1 ; while ( b < max ) { int c = a + b ; a = b ; b = c ; fib . Add ( c ) ; } for ( int i = 0 ; i < x . Length ; i ++ ) { if ( fib . Contains ( x [ i ] ) ) { result . Add ( x [ i ] ) ; } } foreach ( int i in result ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 } ; findFibSubset ( a ) ; } }