text: Lexicographically largest string possible by reversing substrings having even number of 1 s | Function to find the lexicographically maximum string by reversing substrings having even numbers of 1 s ; Store size of string ; Traverse the string ; Count the number of 1 s ; Stores the starting index ; Stores the end index ; Increment count , when 1 is encountered ; Traverse the remaining string ; Reverse the string from starting and end index ; Printing the string ; Driver Code
code: function lexicographicallyMax ( s ) { var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { var count = 0 ; var beg = i ; var end = i ; if ( s [ i ] == ' ' ) count ++ ; for ( var j = i + 1 ; j < n ; j ++ ) { if ( s [ j ] == ' ' ) count ++ ; if ( count % 2 == 0 && count != 0 ) { end = j ; break ; } } for ( var i = beg ; i < parseInt ( ( end + 1 ) / 2 ) ; i ++ ) { let temp = s [ i ] ; s [ i ] = s [ end - i + 1 ] ; s [ end - i + 1 ] = temp ; } } document . write ( s . join ( " " ) + " " ) ; } var S = " " . split ( ' ' ) ; lexicographicallyMax ( S ) ;