A Binary String Game | Function to return the result of the game ; length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1' ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Prlet the result ; Given string S ; Function Call
function gameMax ( S ) { let N = S . length ; let list = [ ] ; let one = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { one ++ ; } else { if ( one != 0 ) { list . push ( one ) ; } one = 0 ; } } if ( one != 0 ) { list . push ( one ) ; } list . sort ( ) ; list . reverse ( ) ; let score_1 = 0 , score_2 = 0 ; for ( let i = 0 ; i < list . length ; i ++ ) { if ( list [ i ] % 2 == 1 ) { score_1 += list [ i ] ; } else { score_2 += list [ i ] ; } } if ( score_1 == score_2 ) return " " ; return ( score_1 > score_2 ) ? " " : " " ; } let S = " " ; document . write ( gameMax ( S ) ) ;