Find the winner of a game where scores are given as a binary string | Cpp 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 program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void predictWinner ( string score , int n ) { int count [ 2 ] = { 0 } , i ; for ( i = 0 ; i < score . size ( ) ; i ++ ) { count [ score [ i ] - '0' ] ++ ; if ( count [ 0 ] == n && count [ 1 ] < n - 1 ) { cout << " GEEKS ▁ lost " ; return ; } if ( count [ 1 ] == n && count [ 0 ] < n - 1 ) { cout << " GEEKS ▁ won " ; return ; } if ( count [ 0 ] == n - 1 && count [ 1 ] == n - 1 ) { count [ 0 ] = 0 ; count [ 1 ] = 0 ; break ; } } for ( i ++ ; i < score . size ( ) ; i ++ ) { count [ score [ i ] - '0' ] ++ ; if ( abs ( count [ 0 ] - count [ 1 ] ) == 2 ) { if ( count [ 0 ] > count [ 1 ] ) cout << " GEEKS ▁ lost " ; else cout << " GEEKS ▁ won " ; return ; } } } int main ( ) { string score = "1001010101111011101111" ; int n = 15 ; predictWinner ( score , n ) ; return 0 ; }