Length of longest subsequence consisting of distinct adjacent elements | C # program for the above approach ; Function that finds the length of longest subsequence having different adjacent elements ; Stores the length of the longest subsequence ; Traverse the array ; If previous and current element are not same ; Increment the count ; Print the maximum length ; Driver Code ; Size of Array ; Function Call
using System ; class GFG { static void longestSubsequence ( int [ ] arr , int N ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count ++ ; } } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 7 , 8 , 1 , 2 , 2 , 5 , 5 , 1 } ; int N = arr . Length ; longestSubsequence ( arr , N ) ; } }