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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findAmountPlayers ( int arr [ ] , int N ) { sort ( arr , arr + N , greater < int > ( ) ) ; int amountA = 0 ; int amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; cout << " ( 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 ] ; } } cout << " ( A ▁ : ▁ " << amountA << " ) STRNEWLINE " << " ( B ▁ : ▁ " << amountB << " ) " ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findAmountPlayers ( arr , N ) ; return 0 ; }