Queries to find longest subsequence having no similar adjacent elements with updates | Function to find the length of the longest subsequence such that no two adjacent elements are equal ; Replace element at index x with y ; Since x is 1 - indexed , decrement x by 1 ; Keep track of number of elements in subsequence ; If previous element is not same as current element ; Print the desired count ; Driver code ; Function Call
function longestSubsequence ( N , Q , arr , Queries ) { for ( let i = 0 ; i < Q ; i ++ ) { let x = Queries [ i ] [ 0 ] ; let y = Queries [ i ] [ 1 ] ; arr [ x - 1 ] = y ; let count = 1 ; for ( let j = 1 ; j < N ; j ++ ) { if ( arr [ j ] != arr [ j - 1 ] ) { count += 1 ; } } document . write ( count + " " ) ; } } let arr = [ 1 , 1 , 2 , 5 , 2 ] ; let N = arr . length ; let Q = 2 ; let Queries = [ [ 1 , 3 ] , [ 4 , 2 ] ] ; longestSubsequence ( N , Q , arr , Queries ) ;