Longest sub | 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
def LucasSequence ( arr , n ) : NEW_LINE INDENT max = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > max ) : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT DEDENT s = set ( ) NEW_LINE a = 2 NEW_LINE b = 1 NEW_LINE s . add ( a ) NEW_LINE s . add ( b ) NEW_LINE while ( b < max ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE s . add ( b ) NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] in s ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( LucasSequence ( arr , n ) ) NEW_LINE DEDENT