Maximum money that can be collected by both the players in a game of removal of coins | C # 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
using System ; class GFG { static void findAmountPlayers ( int [ ] arr , int N ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int amountA = 0 ; int amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; Console . WriteLine ( " ( 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 ] ; } } Console . WriteLine ( " ( A ▁ : ▁ " + amountA + " ) " ) ; Console . WriteLine ( " ( B ▁ : ▁ " + amountB + " ) " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 } ; int N = arr . Length ; findAmountPlayers ( arr , N ) ; } }