Find the player who wins the game of placing alternate + and | C ++ program for the above approach ; Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Driver Code ; Given Input ; Function call to check which player wins the game
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkWinner ( int arr [ ] , int N ) { int diff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { diff -= arr [ i ] ; } if ( diff % 2 == 0 ) { cout << " A " ; } else { cout << " B " ; } } int main ( ) { int arr [ ] = { 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkWinner ( arr , N ) ; return 0 ; }