text: Find the last player to be able to flip a character in a Binary String | Java program for the above approach ; 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
code: import java . util . * ; public class GFG { static void findWinner ( String a , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a . charAt ( i ) == '0' ) { c ++ ; } else { if ( c != 0 ) v . add ( c ) ; c = 0 ; } } if ( c != 0 ) v . add ( c ) ; if ( v . size ( ) == 0 ) { System . out . print ( " Player ▁ B " ) ; return ; } if ( v . size ( ) == 1 ) { if ( ( v . get ( 0 ) & 1 ) != 0 ) System . out . print ( " Player ▁ A " ) ; else System . out . print ( " Player ▁ B " ) ; return ; } int first = Integer . MIN_VALUE ; int second = Integer . MIN_VALUE ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( a . charAt ( i ) > first ) { second = first ; first = a . charAt ( i ) ; } else if ( a . charAt ( i ) > second && a . charAt ( i ) != first ) second = a . charAt ( i ) ; } if ( ( first & 1 ) != 0 && ( first + 1 ) / 2 > second ) System . out . print ( " Player ▁ A " ) ; else System . out . print ( " Player ▁ B " ) ; } public static void main ( String [ ] args ) { String S = "1100011" ; int N = S . length ( ) ; findWinner ( S , N ) ; } }