Longest subsequence of a number having same left and right rotation | Function to find the longest subsequence having equal left and right rotation ; Length of the string ; Iterate for all possible combinations of a two - digit numbers ; Check for alternate occurrence of current combination ; Increment the current value ; Increment the current value ; If alternating sequence is obtained of odd length ; Reduce to even length ; Update answer to store the maximum ; Return the answer ; Driver Code
function findAltSubSeq ( s ) { var n = s . length , ans = - 1000000000 ; for ( var i = 0 ; i < 10 ; i ++ ) { for ( var j = 0 ; j < 10 ; j ++ ) { var cur = 0 , f = 0 ; for ( var k = 0 ; k < n ; k ++ ) { if ( f == 0 && s [ k ] - ' ' == i ) { f = 1 ; cur ++ ; } else if ( f == 1 && s [ k ] - ' ' == j ) { f = 0 ; cur ++ ; } } if ( i != j && cur % 2 == 1 ) cur -- ; ans = Math . max ( cur , ans ) ; } } return ans ; } var s = " " ; document . write ( findAltSubSeq ( s ) ) ;