Given an array arr[] consisting of N integers and an integer M (initially 1), the task is to find the maximum sum of array elements chosen by Player A when two players A and B plays the game optimally according to the following rules:
- Player A starts the game.
- At every chance, X number of elements can be chosen from the beginning of the array, where X is inclusive over the range [1, 2*M] is chosen by the respective player in their turn.
- After choosing array elements in the above steps, remove those elements from the array and update the value of M as the maximum of X and M.
- The above process will continue till all the array elements are chosen.
Examples:
Input: arr[] = {2, 7, 9, 4, 4}
Output: 10
Explanation:
Initially the array is arr[] = {2, 7, 9, 4, 4} and the value of M = 1, Below are the order of ch0osing array elements by both the players:
Player A: The number of elements can be chosen over the range [1, 2*M] i.e., [1, 2]. So, choose element {2} and remove it. Now the array modifies to {7, 9, 4, 4} and the value of M is max(M, X) = max(1, 1) = 1(X is 1).
Player B: The number of elements can be chosen over the range [1, 2*M] i.e., [1, 2]. So, choose element {7, 9} and remove it. Now the array modifies to {4, 4} and the value of M is max(M, X) = max(1, 2) = 2(X is 2).
Player A: The number of elements can be chosen over the range [1, 2*2] i.e., [1, 1]. So, choose element {4, 4} and remove it. Now the array becomes empty.
Therefore, the sum of elements chosen by the Player A is 2 + 4 + 4 = 10.
Input: arr[] = {1}
Output: 1
Naive Approach: The simplest approach is to solve the given problem is to use recursion and generate all possible combinations of choosing elements for both the players from the beginning according to the given rules and print the maximum sum of chosen elements obtained for Player A. Follow the below steps to solve the given problem:
- Declare a recursive function, say recursiveChoosing(arr, start, M) that takes parameters array, starting index of the current array, and initial value of M and perform the following operations in this function:
- If the value of start is greater than N, then return 0.
- If the value of (N – start) is at most 2*M, then return the sum of the element of the array from the index start for the respective score of the player.
- Initialize a maxSum as 0 that stores the maximum sum of array elements chosen by Player A.
- Find the total sum of the array elements from the start and store it in a variable, say total.
- Iterate over the range [1, 2*M], and perform the following steps:
- For each element X, chooses X elements from the start and recursively calls for choosing elements from the remaining (N – X) elements. Let the value returned by this call be stored in maxSum.
- After the above recursive call ends update the value of maxSum to the maximum of maxSum and (total – maxSum).
- Return the value of maxSum in each recursive call.
- After completing the above steps, print the value returned by the function recursiveChoosing(arr, 0, 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int arr[], int start, int N)
{
int sum1 = 0;
for ( int i = start; i < N; i++)
{
sum1 += arr[i];
}
return sum1;
}
int recursiveChoosing( int arr[], int start,
int M, int N)
{
if (start >= N)
{
return 0;
}
if (N - start <= 2 * M)
{
return sum(arr, start, N);
}
int psa = 0;
int total = sum(arr, start, N);
for ( int x = 1; x < 2 * M + 1; x++)
{
int psb = recursiveChoosing(arr, start + x,
max(x, M), N);
psa = max(psa, total - psb);
}
return psa;
}
int main()
{
int arr[] = { 2, 7, 9, 4, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << recursiveChoosing(arr, 0, 1, N);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int recursiveChoosing( int arr[], int start,
int M, int N)
{
if (start >= N)
{
return 0 ;
}
if (N - start <= 2 * M)
{
return sum(arr, start);
}
int psa = 0 ;
int total = sum(arr, start);
for ( int x = 1 ; x < 2 * M + 1 ; x++)
{
int psb = recursiveChoosing(arr, start + x,
Math.max(x, M), N);
psa = Math.max(psa, total - psb);
}
return psa;
}
static int sum( int arr[], int start)
{
int sum = 0 ;
for ( int i = start; i < arr.length; i++)
{
sum += arr[i];
}
return sum;
}
public static void main(String[] args)
{
int arr[] = { 2 , 7 , 9 , 4 , 4 };
int N = arr.length;
System.out.print(recursiveChoosing(
arr, 0 , 1 , N));
}
}
|
Python3
def recursiveChoosing(arr, start, M):
if start > = N:
return 0
if N - start < = 2 * M:
return sum (arr[start:])
psa = 0
total = sum (arr[start:])
for x in range ( 1 , 2 * M + 1 ):
psb = recursiveChoosing(arr,
start + x, max (x, M))
psa = max (psa, total - psb)
return psa
arr = [ 2 , 7 , 9 , 4 , 4 ]
N = len (arr)
print (recursiveChoosing(arr, 0 , 1 ))
|
C#
using System;
class GFG{
static int recursiveChoosing( int [] arr, int start,
int M, int N)
{
if (start >= N)
{
return 0;
}
if (N - start <= 2 * M)
{
return sum(arr, start);
}
int psa = 0;
int total = sum(arr, start);
for ( int x = 1; x < 2 * M + 1; x++)
{
int psb = recursiveChoosing(arr, start + x,
Math.Max(x, M), N);
psa = Math.Max(psa, total - psb);
}
return psa;
}
static int sum( int [] arr, int start)
{
int sum = 0;
for ( int i = start; i < arr.Length; i++)
{
sum += arr[i];
}
return sum;
}
public static void Main()
{
int [] arr = { 2, 7, 9, 4, 4 };
int N = arr.Length;
Console.WriteLine(recursiveChoosing(
arr, 0, 1, N));
}
}
|
Javascript
<script>
function sum(arr, start, N)
{
var sum1 = 0;
for ( var i = start; i < N; i++)
{
sum1 += arr[i];
}
return sum1;
}
function recursiveChoosing(arr, start, M, N)
{
if (start >= N)
{
return 0;
}
if (N - start <= 2 * M)
{
return sum(arr, start, N);
}
var psa = 0;
var total = sum(arr, start, N);
for ( var x = 1; x < 2 * M + 1; x++)
{
var psb = recursiveChoosing(arr, start + x,
Math.max(x, M), N);
psa = Math.max(psa, total - psb);
}
return psa;
}
var arr = [ 2, 7, 9, 4, 4 ];
var N = arr.length
document.write(recursiveChoosing(arr, 0, 1, N));
</script>
|
Time Complexity: O(K*2N), where K is over the range [1, 2*M]
Auxiliary Space: O(N2)
Efficient Approach: The above approach can also be optimized by using Dynamic Programming as it has Overlapping Subproblems and Optimal Substructure that can be stored and used further in the same recursive calls.
Therefore, the idea is to use a dictionary to store the state of each recursive call so that the already computed state can be accessed faster and result in less time complexity.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int recursiveChoosing( int arr[], int start, int M, map<pair< int , int >, int > dp, int N)
{
pair< int , int > key(start, M);
if (start >= N)
{
return 0;
}
if (N - start <= 2 * M)
{
int Sum = 0;
for ( int i = start; i < N; i++)
{
Sum = Sum + arr[i];
}
return Sum;
}
int sum = 0;
for ( int i = start; i < N; i++)
{
sum = sum + arr[i];
}
int total = sum;
if (dp.find(key) != dp.end())
{
return dp[key];
}
int psa = 0;
for ( int x = 1; x < 2 * M + 1; x++)
{
int psb = recursiveChoosing(arr, start + x, max(x, M), dp, N);
psa = max(psa, total - psb);
}
dp[key] = psa;
return dp[key];
}
int main()
{
int arr[] = {2, 7, 9, 4, 4};
int N = sizeof (arr) / sizeof (arr[0]);
map<pair< int , int >, int > dp;
cout << recursiveChoosing(arr, 0, 1, dp, N);
return 0;
}
|
Java
import java.util.*;
import java.awt.Point;
public class GFG
{
static int recursiveChoosing( int [] arr, int start, int M,
HashMap<Point,Integer> dp)
{
Point key = new Point(start, M);
if (start >= arr.length)
{
return 0 ;
}
if (arr.length - start <= 2 * M)
{
int Sum = 0 ;
for ( int i = start; i < arr.length; i++)
{
Sum = Sum + arr[i];
}
return Sum;
}
int sum = 0 ;
for ( int i = start; i < arr.length; i++)
{
sum = sum + arr[i];
}
int total = sum;
if (dp.containsKey(key))
{
return dp.get(key);
}
int psa = 0 ;
for ( int x = 1 ; x < 2 * M + 1 ; x++)
{
int psb = recursiveChoosing(arr, start + x, Math.max(x, M), dp);
psa = Math.max(psa, total - psb);
}
dp.put(key, psa);
return dp.get(key);
}
public static void main(String[] args) {
int [] arr = { 2 , 7 , 9 , 4 , 4 };
int N = arr.length;
HashMap<Point,Integer> dp = new HashMap<Point,Integer>();
System.out.print(recursiveChoosing(arr, 0 , 1 , dp));
}
}
|
Python3
def recursiveChoosing(arr, start, M, dp):
key = (start, M)
if start > = N:
return 0
if N - start < = 2 * M:
return sum (arr[start:])
psa = 0
total = sum (arr[start:])
if key in dp:
return dp[key]
for x in range ( 1 , 2 * M + 1 ):
psb = recursiveChoosing(arr,
start + x, max (x, M), dp)
psa = max (psa, total - psb)
dp[key] = psa
return dp[key]
arr = [ 2 , 7 , 9 , 4 , 4 ]
N = len (arr)
dp = {}
print (recursiveChoosing(arr, 0 , 1 , dp))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int recursiveChoosing( int [] arr, int start, int M,
Dictionary<Tuple< int , int >, int > dp)
{
Tuple< int , int > key = new Tuple< int , int >(start, M);
if (start >= arr.Length)
{
return 0;
}
if (arr.Length - start <= 2 * M)
{
int Sum = 0;
for ( int i = start; i < arr.Length; i++)
{
Sum = Sum + arr[i];
}
return Sum;
}
int sum = 0;
for ( int i = start; i < arr.Length; i++)
{
sum = sum + arr[i];
}
int total = sum;
if (dp.ContainsKey(key))
{
return dp[key];
}
int psa = 0;
for ( int x = 1; x < 2 * M + 1; x++)
{
int psb = recursiveChoosing(arr, start + x, Math.Max(x, M), dp);
psa = Math.Max(psa, total - psb);
}
dp[key] = psa;
return dp[key];
}
static void Main()
{
int [] arr = {2, 7, 9, 4, 4};
int N = arr.Length;
Dictionary<Tuple< int , int >, int > dp = new Dictionary<Tuple< int , int >, int >();
Console.Write(recursiveChoosing(arr, 0, 1, dp));
}
}
|
Javascript
<script>
function recursiveChoosing(arr, start, M, dp)
{
let key = [start, M];
if (start >= N)
{
return 0;
}
if (N - start <= 2 * M)
{
let sum = 0;
for (let i = start; i < arr.length; i++)
{
sum = sum + arr[i];
}
return sum;
}
let psa = 0;
let sum = 0;
for (let i = start; i < arr.length; i++)
{
sum = sum + arr[i];
}
let total = sum;
if (dp.has(key))
{
return dp[key];
}
for (let x = 1; x < 2 * M + 1; x++)
{
let psb = recursiveChoosing(arr,
start + x, Math.max(x, M), dp)
psa = Math.max(psa, total - psb);
}
dp[key] = psa;
return dp[key];
}
let arr = [2, 7, 9, 4, 4];
let N = arr.length;
let dp = new Map();
document.write(recursiveChoosing(arr, 0, 1, dp))
</script>
|
Time Complexity: O(K*N2), where K is over the range [1, 2*M]
Auxiliary Space: O(N2)