Maximum consecutive numbers present in an array | Python3 program to find largest consecutive numbers present in arr . ; We insert all the array elements into unordered set . ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; increment the value of array element and repeat search in the set ; Update optimal length if this length is more . To get the length as it is incremented one by one ; Driver code
def findLongestConseqSubseq ( arr , n ) : NEW_LINE INDENT S = set ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT S . add ( arr [ i ] ) ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if S . __contains__ ( arr [ i ] ) : NEW_LINE INDENT j = arr [ i ] ; NEW_LINE while ( S . __contains__ ( j ) ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT ans = max ( ans , j - arr [ i ] ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 94 , 93 , 1000 , 5 , 92 , 78 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findLongestConseqSubseq ( arr , n ) ) ; NEW_LINE DEDENT