Longest sub | C # implementation of the approach ; Function to return the length of the longest required sub - sequence ; Find the maximum element from the array ; Insert all lucas numbers below max to the set a and b are first two elements of the Lucas sequence ; If current element is a Lucas number , increment count ; Return the count ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int LucasSequence ( int [ ] arr , int n ) { int max = arr . Max ( ) ; int counter = 0 ; HashSet < int > s = new HashSet < int > ( ) ; int a = 2 , b = 1 ; s . Add ( a ) ; s . Add ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; s . Add ( b ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( arr [ i ] ) ) counter ++ ; } return counter ; } static public void Main ( ) { int [ ] arr = { 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 } ; int n = arr . Length ; Console . WriteLine ( LucasSequence ( arr , n ) ) ; } }