Longest Increasing consecutive subsequence | C # program to find length of the longest increasing subsequence whose adjacent element differ by 1 ; function that returns the length of the longest increasing subsequence whose adjacent element differ by 1 ; Create hashmap to save latest consequent number as " key " and its length as " value " ; Put first element as " key " and its length as " value " ; iterate for all element ; Check if last consequent of arr [ i ] exist or not ; put the updated consequent number and increment its value ( length ) ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int longIncrConseqSubseq ( int [ ] arr , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; map . Add ( arr [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( map . ContainsKey ( arr [ i ] - 1 ) ) { map . Add ( arr [ i ] , map [ arr [ i ] - 1 ] + 1 ) ; map . Remove ( arr [ i ] - 1 ) ; } else { if ( ! map . ContainsKey ( arr [ i ] ) ) map . Add ( arr [ i ] , 1 ) ; } } int max = int . MinValue ; foreach ( KeyValuePair < int , int > entry in map ) { if ( entry . Value > max ) { max = entry . Value ; } } return max ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( longIncrConseqSubseq ( arr , n ) ) ; } }