# Maximum money that can be collected by both the players in a game of removal of coins

• Last Updated : 26 Nov, 2021

Given an array arr[] consisting of N positive integers, such that arr[i] represents the value of the coin, the task is to find the maximum amount of money that can be obtained by each player when two players A and B play the game optimally as per the following rules:

• Player A always starts the game.
• In each turn, a player must remove exactly 1 coin from the given array.
• In each turn, player B, it must remove exactly 2 coins from the given array.

Examples:

Input: arr[] = {1, 1, 1, 1}
Output: (A : 2), (B : 2)
Explanation:
Following are the sequences of coins removed for both the players:

• Player A removes arr(= 1).
• Player B removes arr(= 1) and arr(= 1).
• Player A removes arr(= 1).

Therefore, the total coins obtained by Player A is 2 and Player B is 2.

Input: arr[] = {1, 1, 1}
Output: (A : 1), (B : 2)

Approach: The given problem can be solved by using the Greedy Approach. Follow the below steps to solve the problem:

• Initialize two variables, say amountA and amountB as 0 that stores the total amount of money obtained by the players A and B.
• Sort the given array in descending order.
• If the value of N is 1, then update the value of amountA to arr.
• If the value of N is greater than or equal to 2, then update the value of amountA to arr and the value of amountB to arr.
• Traverse the array arr[] over the range [2, N] using the variable i, and if the value of i is even then add the value arr[i] to amountB. Otherwise, add the value arr[i] to the amountA.
• After completing the above steps, print the value of amountA and amountB as the result score of both the players A and B respectively.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the maximum score``// obtained by the players A and B``void` `findAmountPlayers(``int` `arr[], ``int` `N)``{``    ``// Sort the array in descending order``    ``sort(arr, arr + N, greater<``int``>());` `    ``// Stores the maximum amount of``    ``// money obtained by A``    ``int` `amountA = 0;` `    ``// Stores the maximum amount of``    ``// money obtained by B``    ``int` `amountB = 0;` `    ``// If the value of N is 1``    ``if` `(N == 1) {` `        ``// Update the amountA``        ``amountA += arr;` `        ``// Print the amount of money``        ``// obtained by both players``        ``cout << ``"(A : "` `<< amountA << ``"), "``             ``<< ``"(B : "` `<< amountB << ``")"``;``        ``return``;``    ``}` `    ``// Update the amountA``    ``amountA = arr;` `    ``// Update the amountB``    ``amountB = arr;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 2; i < N; i++) {` `        ``// If i is an odd number``        ``if` `(i % 2 == 0) {` `            ``// Update the amountB``            ``amountB += arr[i];``        ``}``        ``else` `{` `            ``// Update the amountA``            ``amountA += arr[i];``        ``}``    ``}` `    ``// Print the amount of money``    ``// obtained by both the players``    ``cout << ``"(A : "` `<< amountA << ``")\n"``         ``<< ``"(B : "` `<< amountB << ``")"``;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 1, 1 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``findAmountPlayers(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to find the maximum score``// obtained by the players A and B``static` `void` `findAmountPlayers(``int``[] arr, ``int` `N)``{``    ` `    ``// Sort the array in descending order``    ``Arrays.sort(arr);``    ``reverse(arr, N);` `    ``// Stores the maximum amount of``    ``// money obtained by A``    ``int` `amountA = ``0``;` `    ``// Stores the maximum amount of``    ``// money obtained by B``    ``int` `amountB = ``0``;` `    ``// If the value of N is 1``    ``if` `(N == ``1``)``    ``{``        ` `        ``// Update the amountA``        ``amountA += arr[``0``];` `        ``// Print the amount of money``        ``// obtained by both players``        ``System.out.println(``"(A : "` `+ amountA + ``"), "` `+``                           ``"(B : "` `+ amountB + ``")"``);``        ``return``;``    ``}` `    ``// Update the amountA``    ``amountA = arr[``0``];` `    ``// Update the amountB``    ``amountB = arr[``1``];` `    ``// Traverse the array arr[]``    ``for``(``int` `i = ``2``; i < N; i++)``    ``{``        ` `        ``// If i is an odd number``        ``if` `(i % ``2` `== ``0``)``        ``{``            ` `            ``// Update the amountB``            ``amountB += arr[i];``        ``}``        ``else``        ``{``            ` `            ``// Update the amountA``            ``amountA += arr[i];``        ``}``    ``}` `    ``// Print the amount of money``    ``// obtained by both the players``    ``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``;``    ``}``}` `// Driver code``public` `static` `void` `main(String []args)``{``    ``int``[] arr = { ``1``, ``1``, ``1` `};``    ``int` `N = arr.length;``    ` `    ``findAmountPlayers(arr, N);``}``}` `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 program for the above approach` `# Function to find the maximum score``# obtained by the players A and B``def` `findAmountPlayers(arr, N):``    ``# Sort the array in descending order``    ``arr ``=` `sorted``(arr)[::``-``1``]` `    ``# Stores the maximum amount of``    ``# money obtained by A``    ``amountA ``=` `0` `    ``# Stores the maximum amount of``    ``# money obtained by B``    ``amountB ``=` `0` `    ``# If the value of N is 1``    ``if` `(N ``=``=` `1``):` `        ``# Update the amountA``        ``amountA ``+``=` `arr[``0``]` `        ``# Print the amount of money``        ``# obtained by both players``        ``print``(``"(A :"``,mountA,``"), (B :"``,``str``(amountB)``+``")"``)``        ``return`  `    ``# Update the amountA``    ``amountA ``=` `arr[``0``]` `    ``# Update the amountB``    ``amountB ``=` `arr[``1``]` `    ``# Traverse the array arr[]``    ``for` `i ``in` `range``(``2``, N):``      ` `        ``# If i is an odd number``        ``if` `(i ``%` `2` `=``=` `0``):``          ` `            ``# Update the amountB``            ``amountB ``+``=` `arr[i]` `        ``else``:` `            ``# Update the amountA``            ``amountA ``+``=` `arr[i]` `    ``# Print amount of money``    ``# obtained by both the players``    ``print``(``"(A :"``,``str``(amountA)``+``")\n(B :"``,``str``(amountB)``+``")"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``1``, ``1``, ``1``]``    ``N ``=` `len``(arr)``    ``findAmountPlayers(arr, N)` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{``  ` `    ``// Function to find the maximum score``    ``// obtained by the players A and B``    ``static` `void` `findAmountPlayers(``int``[] arr, ``int` `N)``    ``{``      ` `        ``// Sort the array in descending order``        ``Array.Sort(arr);``        ``Array.Reverse(arr);` `        ``// Stores the maximum amount of``        ``// money obtained by A``        ``int` `amountA = 0;` `        ``// Stores the maximum amount of``        ``// money obtained by B``        ``int` `amountB = 0;` `        ``// If the value of N is 1``        ``if` `(N == 1) {` `            ``// Update the amountA``            ``amountA += arr;` `            ``// Print the amount of money``            ``// obtained by both players``            ``Console.WriteLine(``"(A : "` `+ amountA + ``"), "``                              ``+ ``"(B : "` `+ amountB + ``")"``);``            ``return``;``        ``}` `        ``// Update the amountA``        ``amountA = arr;` `        ``// Update the amountB``        ``amountB = arr;` `        ``// Traverse the array arr[]``        ``for` `(``int` `i = 2; i < N; i++) {` `            ``// If i is an odd number``            ``if` `(i % 2 == 0) {` `                ``// Update the amountB``                ``amountB += arr[i];``            ``}``            ``else` `{` `                ``// Update the amountA``                ``amountA += arr[i];``            ``}``        ``}` `        ``// Print the amount of money``        ``// obtained by both the players``        ``Console.WriteLine(``"(A : "` `+ amountA + ``")"``);``        ``Console.WriteLine(``"(B : "` `+ amountB + ``")"``);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 1, 1, 1 };``        ``int` `N = arr.Length;``        ``findAmountPlayers(arr, N);``    ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``

Output:

```(A : 1)
(B : 2)```

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up