Find the player with least 0 s after emptying a Binary String by removing non | C ++ program to implement the above approach ; Function to find the player who wins the game ; Stores total count of 0 s in the string ; Stores count of consecutive 1 s ; Stores Nim - Sum on count of consecutive 1 s ; Stores length of the string ; Traverse the string ; If the current character is 1 ; Update cntConOne ; Update nimSum ; Update cntConOne ; Update cntZero ; Update nimSum ; If countZero is an even number ; nimSum is not 0 ; If nimSum is zero ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FindwinnerOfGame ( string & S ) { int cntZero = 0 ; int cntConOne = 0 ; int nimSum = 0 ; int N = S . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { cntConOne += 1 ; } else { nimSum ^= cntConOne ; cntConOne = 0 ; cntZero ++ ; } } nimSum ^= cntConOne ; if ( cntZero % 2 == 0 ) { cout << " Tie " ; } else if ( nimSum ) { cout << " player ▁ 1" ; } else { cout << " player ▁ 2" ; } } int main ( ) { string S = "0110011" ; FindwinnerOfGame ( S ) ; }