Maximum money that can be collected by both the players in a game of removal of coins | Java program for the above approach ; Function to find the maximum score obtained by the players A and B ; Sort the array in descending order ; Stores the maximum amount of money obtained by A ; Stores the maximum amount of money obtained by B ; If the value of N is 1 ; Update the amountA ; Print the amount of money obtained by both players ; Update the amountA ; Update the amountB ; Traverse the array arr [ ] ; If i is an odd number ; Update the amountB ; Update the amountA ; Print the amount of money obtained by both the players ; Driver code
import java . util . * ; class GFG { static void findAmountPlayers ( int [ ] arr , int N ) { Arrays . sort ( arr ) ; reverse ( arr , N ) ; int amountA = 0 ; int amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; System . out . println ( " ( A ▁ : ▁ " + amountA + " ) , ▁ " + " ( B ▁ : ▁ " + amountB + " ) " ) ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } System . out . println ( " ( A ▁ : ▁ " + amountA + " ) " ) ; System . out . println ( " ( B ▁ : ▁ " + amountB + " ) " ) ; } static void reverse ( int a [ ] , int n ) { int [ ] b = new int [ n ] ; int j = n ; for ( int i = 0 ; i < n ; i ++ ) { b [ j - 1 ] = a [ i ] ; j = j - 1 ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int N = arr . length ; findAmountPlayers ( arr , N ) ; } }