Maximum money that can be collected by both the players in a game of removal of coins
Last Updated :
29 Sep, 2022
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[0](= 1).
- Player B removes arr[1](= 1) and arr[2](= 1).
- Player A removes arr[3](= 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[0].
- If the value of N is greater than or equal to 2, then update the value of amountA to arr[0] and the value of amountB to arr[1].
- 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++
#include <bits/stdc++.h>
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 << ")\n"
<< "(B : " << amountB << ")" ;
}
int main()
{
int arr[] = { 1, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
findAmountPlayers(arr, N);
return 0;
}
|
Java
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);
}
}
|
Python3
def findAmountPlayers(arr, N):
arr = sorted (arr)[:: - 1 ]
amountA = 0
amountB = 0
if (N = = 1 ):
amountA + = arr[ 0 ]
print ( "(A :" ,mountA, "), (B :" , str (amountB) + ")" )
return
amountA = arr[ 0 ]
amountB = arr[ 1 ]
for i in range ( 2 , N):
if (i % 2 = = 0 ):
amountB + = arr[i]
else :
amountA + = arr[i]
print ( "(A :" , str (amountA) + ")\n(B :" , str (amountB) + ")" )
if __name__ = = '__main__' :
arr = [ 1 , 1 , 1 ]
N = len (arr)
findAmountPlayers(arr, N)
|
C#
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);
}
}
|
Javascript
<script>
function findAmountPlayers(arr, N) {
arr.sort((a, b) => b - a)
let amountA = 0;
let amountB = 0;
if (N == 1) {
amountA += arr[0];
document.write( "(A : " + amountA + "), "
+ "(B : " + 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( "(A : " + amountA + ")<br>"
+ "(B : " + amountB + ")" );
}
let arr = [1, 1, 1];
let N = arr.length
findAmountPlayers(arr, N);
</script>
|
Time Complexity: O(N*logN) because using sort function
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...