Find the player who wins the game of placing alternate + and | Java 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
import java . util . * ; class GFG { static void checkWinner ( int arr [ ] , int N ) { int diff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { diff -= arr [ i ] ; } if ( diff % 2 == 0 ) { System . out . println ( " A " ) ; } else { System . out . println ( " B " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int N = arr . length ; checkWinner ( arr , N ) ; } }