Maximum money that can be collected by both the players in a game of removal of coins | 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
function findAmountPlayers ( arr , N ) { arr . sort ( ( a , b ) => b - a ) let amountA = 0 ; let amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; document . write ( " " + amountA + " " + " " + amountB + " " ) ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( let i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } document . write ( " " + amountA + " " + " " + amountB + " " ) ; } let arr = [ 1 , 1 , 1 ] ; let N = arr . length findAmountPlayers ( arr , N ) ;