Longest Consecutive Subsequence | Returns length of the longest contiguous subsequence ; Sort the array ; Insert repeated elements only once in the vector ; Find the maximum length by traversing the array ; Check if the current element is equal to previous element + 1 ; Update the maximum ; Driver code
def findLongestConseqSubseq ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE count = 0 NEW_LINE arr . sort ( ) NEW_LINE v = [ ] NEW_LINE v . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT if ( i > 0 and v [ i ] == v [ i - 1 ] + 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 1 NEW_LINE DEDENT ans = max ( ans , count ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ the ▁ Longest ▁ contiguous ▁ subsequence ▁ is " , findLongestConseqSubseq ( arr , n ) ) NEW_LINE