Longest Consecutive Subsequence | Java program to find longest contiguous 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
import java . io . * ; import java . util . * ; class GFG { static int findLongestConseqSubseq ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = 0 , count = 0 ; ArrayList < Integer > v = new ArrayList < Integer > ( ) ; v . add ( 10 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) v . add ( arr [ i ] ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( i > 0 && v . get ( i ) == v . get ( i - 1 ) + 1 ) count ++ ; else count = 1 ; ans = Math . max ( ans , count ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ Longest ▁ " + " contiguous ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }