Skip to content
Related Articles

Related Articles

Improve Article

Maximize sum of odd-indexed array elements by repeatedly selecting at most 2*M array elements from the beginning

  • Difficulty Level : Hard
  • Last Updated : 16 Jun, 2021

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Sum of all numbers in the array
// after start index
int sum(int arr[], int start, int N)
{
    int sum1 = 0;
    for(int i = start; i < N; i++)
    {
        sum1 += arr[i];
    }
    return sum1;
}
 
// Function to find the maximum sum of
// array elements chosen by Player A
// according to the given criteria
int recursiveChoosing(int arr[], int start,
                      int M, int N)
{
     
    // Corner Case
    if (start >= N)
    {
        return 0;
    }
 
    // Check if all the elements can
    // be taken
    if (N - start <= 2 * M)
    {
         
        // If the difference is less than
        // or equal to the available
        // chances then pick all numbers
        return sum(arr, start, N);
    }
 
    int psa = 0;
 
    // Sum of all numbers in the array
    int total = sum(arr, start, N);
 
    // Explore each element X
 
    // Skipping the k variable as per
    // the new updated chance of utility
    for(int x = 1; x < 2 * M + 1; x++)
    {
         
        // Sum of elements for Player A
        int psb = recursiveChoosing(arr, start + x,
                                    max(x, M), N);
 
        // Even chance sum can be obtained
        // by subtracting the odd chances
        // sum - total and picking up the
        // maximum from that
        psa = max(psa, total - psb);
    }
 
    // Return the maximum sum of odd chances
    return psa;
}
 
// Driver Code
int main()
{
     
    // Given array arr[]
    int arr[] = { 2, 7, 9, 4, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << recursiveChoosing(arr, 0, 1, N);
}
 
// This code is contributed by ipg2016107

Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the maximum sum of
// array elements chosen by Player A
// according to the given criteria
static int recursiveChoosing(int arr[], int start,
                             int M, int N)
{
 
    // Corner Case
    if (start >= N)
    {
        return 0;
    }
 
    // Check if all the elements can
    // be taken
    if (N - start <= 2 * M)
    {
         
        // If the difference is less than
        // or equal to the available
        // chances then pick all numbers
        return sum(arr, start);
    }
 
    int psa = 0;
 
    // Sum of all numbers in the array
    int total = sum(arr, start);
 
    // Explore each element X
 
    // Skipping the k variable as per
    // the new updated chance of utility
    for(int x = 1; x < 2 * M + 1; x++)
    {
         
        // Sum of elements for Player A
        int psb = recursiveChoosing(arr, start + x,
                                    Math.max(x, M), N);
 
        // Even chance sum can be obtained
        // by subtracting the odd chances
        // sum - total and picking up the
        // maximum from that
        psa = Math.max(psa, total - psb);
    }
 
    // Return the maximum sum of odd chances
    return psa;
}
 
// Sum of all numbers in the array after start index
static int sum(int arr[], int start)
{
    int sum = 0;
    for(int i = start; i < arr.length; i++)
    {
        sum += arr[i];
    }
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 2, 7, 9, 4, 4 };
    int N = arr.length;
 
    // Function Call
    System.out.print(recursiveChoosing(
        arr, 0, 1, N));
}
}
 
// This code is contributed by Kingash

Python3




# Python program for the above approach
 
# Function to find the maximum sum of
# array elements chosen by Player A
# according to the given criteria
def recursiveChoosing(arr, start, M):
   
    # Corner Case
    if start >= N:
        return 0
       
    # Check if all the elements can
    # be taken
    if N - start <= 2 * M:
       
        # If the difference is less than
        # or equal to the available
        # chances then pick all numbers
        return sum(arr[start:])
       
    psa = 0
     
    # Sum of all numbers in the array
    total = sum(arr[start:])
     
    # Explore each element X
     
    # Skipping the k variable as per
    # the new updated chance of utility
    for x in range(1, 2 * M + 1):
       
        # Sum of elements for Player A
        psb = recursiveChoosing(arr,
                            start + x, max(x, M))
         
        # Even chance sum can be obtained
        # by subtracting the odd chances
        # sum - total and picking up the
        # maximum from that
        psa = max(psa, total - psb) 
         
    # Return the maximum sum of odd chances
    return psa
 
# Driver Code
 
# Given array arr[]
arr = [2, 7, 9, 4, 4]
N = len(arr)
 
# Function Call
print(recursiveChoosing(arr, 0, 1))

C#




// C# program for the above approach
using System;
         
class GFG{
     
// Function to find the maximum sum of
// array elements chosen by Player A
// according to the given criteria
static int recursiveChoosing(int[] arr, int start,
                             int M, int N)
{
     
    // Corner Case
    if (start >= N)
    {
        return 0;
    }
  
    // Check if all the elements can
    // be taken
    if (N - start <= 2 * M)
    {
         
        // If the difference is less than
        // or equal to the available
        // chances then pick all numbers
        return sum(arr, start);
    }
  
    int psa = 0;
  
    // Sum of all numbers in the array
    int total = sum(arr, start);
  
    // Explore each element X
  
    // Skipping the k variable as per
    // the new updated chance of utility
    for(int x = 1; x < 2 * M + 1; x++)
    {
         
        // Sum of elements for Player A
        int psb = recursiveChoosing(arr, start + x,
                                    Math.Max(x, M), N);
  
        // Even chance sum can be obtained
        // by subtracting the odd chances
        // sum - total and picking up the
        // maximum from that
        psa = Math.Max(psa, total - psb);
    }
  
    // Return the maximum sum of odd chances
    return psa;
}
  
// Sum of all numbers in the array after start index
static int sum(int[] arr, int start)
{
    int sum = 0;
    for(int i = start; i < arr.Length; i++)
    {
        sum += arr[i];
    }
    return sum;
}
     
// Driver Code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 2, 7, 9, 4, 4 };
    int N = arr.Length;
  
    // Function Call
    Console.WriteLine(recursiveChoosing(
        arr, 0, 1, N));
}
}
 
// This code is contributed by susmitakundugoaldanga

Javascript




<script>
 
// Javascript program for the above approach
 
// Sum of all numbers in the array
// after start index
function sum(arr, start, N)
{
    var sum1 = 0;
    for(var i = start; i < N; i++)
    {
        sum1 += arr[i];
    }
    return sum1;
}
 
// Function to find the maximum sum of
// array elements chosen by Player A
// according to the given criteria
function recursiveChoosing(arr, start, M, N)
{
     
    // Corner Case
    if (start >= N)
    {
        return 0;
    }
 
    // Check if all the elements can
    // be taken
    if (N - start <= 2 * M)
    {
         
        // If the difference is less than
        // or equal to the available
        // chances then pick all numbers
        return sum(arr, start, N);
    }
 
    var psa = 0;
 
    // Sum of all numbers in the array
    var total = sum(arr, start, N);
 
    // Explore each element X
 
    // Skipping the k variable as per
    // the new updated chance of utility
    for(var x = 1; x < 2 * M + 1; x++)
    {
         
        // Sum of elements for Player A
        var psb = recursiveChoosing(arr, start + x,
                                    Math.max(x, M), N);
 
        // Even chance sum can be obtained
        // by subtracting the odd chances
        // sum - total and picking up the
        // maximum from that
        psa = Math.max(psa, total - psb);
    }
 
    // Return the maximum sum of odd chances
    return psa;
}
 
// Driver Code
 
// Given array arr[]
var arr = [ 2, 7, 9, 4, 4 ];
var N = arr.length
 
// Function Call
document.write(recursiveChoosing(arr, 0, 1, N));
 
</script>
Output: 
10

 

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:

Python3




# Python program for the above approach
 
# Function to find the maximum sum of
# array elements chosen by Player A
# according to the given criteria
def recursiveChoosing(arr, start, M, dp):
   
    # Store the key
    key = (start, M)
      
    # Corner Case
    if start >= N:
        return 0
       
    # Check if all the elements can
    # be taken or not
    if N - start <= 2 * M:
       
        # If the difference is less than
        # or equal to the available
        # chances then pick all numbers
        return sum(arr[start:])
       
        
    psa = 0
      
    # Find the sum of array elements
    # over the range [start, N]
    total = sum(arr[start:])
      
    # Checking if the current state is
    # previously calculated or not
     
    # If yes then return that value
    if key in dp:
        return dp[key]
        
    # Traverse over the range [1, 2 * M]
    for x in range(1, 2 * M + 1):
          
        # Sum of elements for Player A
        psb = recursiveChoosing(arr,
                          start + x, max(x, M), dp)
          
        # Even chance sum can be obtained
        # by subtracting the odd chances
        # sum - total and picking up the
        # maximum from that
        psa = max(psa, total - psb)
          
    # Storing the value in dictionary
    dp[key] = psa 
      
    # Return the maximum sum of odd chances
    return dp[key] 
 
# Driver Code
 
# Given array arr[]
arr = [2, 7, 9, 4, 4
N = len(arr) 
  
# Stores the precomputed values
dp = {} 
  
# Function Call
print(recursiveChoosing(arr, 0, 1, dp))
Output: 
10

 

Time Complexity: O(K*N2), where K is over the range [1, 2*M]
Auxiliary Space: O(N2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :