Find the winner of a game where scores are given as a binary string | Java program for predicting winner ; function for winner prediction ; increase count ; check losing condition ; check winning condition ; check tie on n - 1 point ; increase count ; check for 2 point lead ; condition of lost ; condition of win ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void predictWinner ( String score , int n ) { int count [ ] = new int [ 2 ] , i ; for ( i = 0 ; i < score . length ( ) ; i ++ ) { count [ score . charAt ( i ) - '0' ] ++ ; if ( count [ 0 ] == n && count [ 1 ] < n - 1 ) { System . out . print ( " GEEKS ▁ lost " ) ; return ; } if ( count [ 1 ] == n && count [ 0 ] < n - 1 ) { System . out . print ( " GEEKS ▁ won " ) ; return ; } if ( count [ 0 ] == n - 1 && count [ 1 ] == n - 1 ) { count [ 0 ] = 0 ; count [ 1 ] = 0 ; break ; } } for ( i ++ ; i < score . length ( ) ; i ++ ) { count [ score . charAt ( i ) - '0' ] ++ ; if ( Math . abs ( count [ 0 ] - count [ 1 ] ) == 2 ) { if ( count [ 0 ] > count [ 1 ] ) System . out . print ( " GEEKS ▁ lost " ) ; else System . out . print ( " GEEKS ▁ won " ) ; return ; } } } public static void main ( String [ ] args ) { String score = "1001010101111011101111" ; int n = 15 ; predictWinner ( score , n ) ; } }