Longest sub | Function to return the length of the longest required sub - sequence ; Find the maximum element from the array ; push 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
function LucasSequence ( arr , n ) { var max = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var s = [ ] ; var a = 2 , b = 1 , c ; s . push ( a ) ; s . push ( b ) ; while ( b < max ) { var c = a + b ; a = b ; b = c ; s . push ( b ) ; } s . sort ( ( a , b ) => a - b ) var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s . includes ( arr [ i ] ) ) { s . pop ( arr [ i ] ) ; count ++ ; } } return count ; } var arr = [ 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 ] ; var n = arr . length ; document . write ( LucasSequence ( arr , n ) ) ;