Find the last player to be able to flip a character in a Binary String | Function to check if player A wins the game or not ; Stores size of the groups of 0 s ; Stores size of the group of 0 s ; Traverse the array ; Increment c by 1 if a [ i ] is 0 ; Otherwise , push the size in array and reset c to 0 ; If there is no substring of odd length consisting only of 0 s ; If there is only 1 substring of odd length consisting only of 0 s ; Otherwise ; Stores the size of the largest and second largest substrings of 0 s ; Traverse the array v [ ] ; If current element is greater than first , then update both first and second ; If arr [ i ] is in between first and second , then update second ; If the condition is satisfied ; Driver code
function findWinner ( a , n ) { let v = [ ] ; let c = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == ' ' ) { c ++ ; } else { if ( c != 0 ) v . push ( c ) ; c = 0 ; } } if ( c != 0 ) v . push ( c ) ; if ( v . length == 0 ) { document . write ( " " ) ; return ; } if ( v . length == 1 ) { if ( ( v [ 0 ] & 1 ) != 0 ) document . write ( " " ) ; else document . write ( " " ) ; return ; } let first = Number . MIN_VALUE ; let second = Number . MIN_VALUE ; for ( let i = 0 ; i < v . length ; i ++ ) { if ( a [ i ] > first ) { second = first ; first = a [ i ] ; } else if ( a [ i ] > second && a [ i ] != first ) second = a [ i ] ; } if ( ( first & 1 ) != 0 && parseInt ( ( first + 1 ) / 2 , 10 ) > second ) document . write ( " " ) ; else document . write ( " " ) ; } let S = " " ; let N = S . length ; findWinner ( S , N ) ;