Maximize Sum possible from an Array by the given moves
Last Updated :
07 May, 2021
Given three integers N, M and K and an array a[] consisting of N integers, where M and K denotes the total number of possible moves and the number of possible moves(shift by an index) on the left of the current element in an array respectively, the task is to maximize the sum possible by traversing the array utilizing all the available moves.
Examples:
Input: N = 5, M = 4, K = 0, a[] = {1, 5, 4, 3, 2}
Output: 15
Explanation:
Since no moves towards left is possible, therefore, the only possible path is a[0] -> a[1] -> a[2] -> a[3] -> a[4].
Therefore, the sum calculated is 15.
Input: N = 5, M = 4, K = 1, a[]= {1, 5, 4, 3, 2}
Output: 19
Explanation:
The maximum sum can be obtained in the path a[0] -> a[1] -> a[2] -> a[1] -> a[2]
Therefore, the maximum possible sum = 19
Approach: The above problem can be solved using Dynamic Programming. Follow the steps below to solve the problem:
- Initialize a dp[][] matrix such that dp[i][j] stores the maximum sum possible up to ith index by using j left moves.
- It can be observed that left move is possible only if i ? 1 and k > 0 and a right move is possible if i < n - 1.
- Check the conditions and update the maximum of the sums possible from the above two moves and store in dp[i][j].
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
const int k = 1;
const int m = 4;
// Function to find the maximum sum possible
// by given moves from the array
int maxValue(int a[], int n, int pos,
int moves, int left,
int dp[][k + 1])
{
// Checking for boundary
if (moves == 0 || (pos > n - 1 || pos < 0))
return 0;
// If previously computed subproblem occurs
if (dp[pos][left] != -1)
return dp[pos][left];
int value = 0;
// If element can be moved left
if (left > 0 && pos >= 1)
// Calculate maximum possible sum
// by moving left from current index
value = max(value, a[pos] +
maxValue(a, n, pos - 1, moves - 1,
left - 1, dp));
// If element can be moved right
if (pos <= n - 1)
// Calculate maximum possible sum
// by moving right from current index
// and update the maximum sum
value = max(value, a[pos] +
maxValue(a, n, pos + 1,
moves - 1, left, dp));
// Store the maximum sum
return dp[pos][left] = value;
}
// Driver Code
int main()
{
int n = 5;
int a[] = { 1, 5, 4, 3, 2 };
int dp[n + 1][k + 1];
memset(dp, -1, sizeof(dp));
cout << (a[0] + maxValue(a, n, 1, m, k, dp))
<< endl;
}
// This code is contributed by sapnasingh4991
Java
// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
public class GFG {
// Function to find the maximum sum possible
// by given moves from the array
public static int maxValue(int a[], int n, int pos,
int moves, int left,
int dp[][])
{
// Checking for boundary
if (moves == 0 || (pos > n - 1 || pos < 0))
return 0;
// If previously computed subproblem occurs
if (dp[pos][left] != -1)
return dp[pos][left];
int value = 0;
// If element can be moved left
if (left > 0 && pos >= 1)
// Calculate maximum possible sum
// by moving left from current index
value = Math.max(
value, a[pos] + maxValue(a, n, pos - 1,
moves - 1, left - 1, dp));
// If element can be moved right
if (pos <= n - 1)
// Calculate maximum possible sum
// by moving right from current index
// and update the maximum sum
value = Math.max(
value, a[pos]
+ maxValue(a, n, pos + 1,
moves - 1, left, dp));
// Store the maximum sum
return dp[pos][left] = value;
}
// Driver Code
public static void main(String args[])
{
int n = 5;
int a[] = { 1, 5, 4, 3, 2 };
int k = 1;
int m = 4;
int dp[][] = new int[n + 1][k + 1];
for (int i[] : dp)
Arrays.fill(i, -1);
System.out.println(
(a[0] + maxValue(a, n, 1, m, k, dp)));
}
}
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum sum possible
# by given moves from the array
def maxValue(a, n, pos, moves, left, dp):
# Checking for boundary
if(moves == 0 or (pos > n - 1 or pos < 0)):
return 0
# If previously computed subproblem occurs
if(dp[pos][left] != -1):
return dp[pos][left]
value = 0
# If element can be moved left
if(left > 0 and pos >= 1):
# Calculate maximum possible sum
# by moving left from current index
value = max(value, a[pos] +
maxValue(a, n, pos - 1,
moves - 1,
left - 1, dp))
# If element can be moved right
if(pos <= n - 1):
# Calculate maximum possible sum
# by moving right from current index
# and update the maximum sum
value = max(value, a[pos] +
maxValue(a, n, pos + 1,
moves - 1,
left, dp))
# Store the maximum sum
dp[pos][left] = value
return dp[pos][left]
# Driver Code
n = 5
a = [ 1, 5, 4, 3, 2 ]
k = 1
m = 4
dp = [[-1 for x in range(k + 1)]
for y in range(n + 1)]
# Function call
print(a[0] + maxValue(a, n, 1, m, k, dp))
# This code is contributed by Shivam Singh
C#
// C# Program to implement
// the above approach
using System;
class GFG
{
// Function to find the maximum sum possible
// by given moves from the array
public static int maxValue(int []a, int n, int pos,
int moves, int left,
int [,]dp)
{
// Checking for boundary
if (moves == 0 || (pos > n - 1 || pos < 0))
return 0;
// If previously computed subproblem occurs
if (dp[pos, left] != -1)
return dp[pos, left];
int value = 0;
// If element can be moved left
if (left > 0 && pos >= 1)
// Calculate maximum possible sum
// by moving left from current index
value = Math.Max(
value, a[pos] + maxValue(a, n, pos - 1,
moves - 1,
left - 1, dp));
// If element can be moved right
if (pos <= n - 1)
// Calculate maximum possible sum
// by moving right from current index
// and update the maximum sum
value = Math.Max(
value, a[pos] + maxValue(a, n, pos + 1,
moves - 1,
left, dp));
// Store the maximum sum
return dp[pos, left] = value;
}
// Driver Code
public static void Main(String []args)
{
int n = 5;
int []a = { 1, 5, 4, 3, 2 };
int k = 1;
int m = 4;
int [,]dp = new int[n + 1, k + 1];
for(int i = 0; i <= n; i++)
for(int j =0; j <= k; j++)
dp[i, j] = -1;
Console.WriteLine(
(a[0] + maxValue(a, n, 1, m, k, dp)));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript program for the
// above approach
// Function to find the maximum sum possible
// by given moves from the array
function maxValue(a, n, pos, moves, left,
dp)
{
// Checking for boundary
if (moves == 0 || (pos > n - 1 || pos < 0))
return 0;
// If previously computed subproblem occurs
if (dp[pos][left] != -1)
return dp[pos][left];
let value = 0;
// If element can be moved left
if (left > 0 && pos >= 1)
// Calculate maximum possible sum
// by moving left from current index
value = Math.max(
value, a[pos] + maxValue(a, n, pos - 1,
moves - 1, left - 1, dp));
// If element can be moved right
if (pos <= n - 1)
// Calculate maximum possible sum
// by moving right from current index
// and update the maximum sum
value = Math.max(
value, a[pos]
+ maxValue(a, n, pos + 1,
moves - 1, left, dp));
// Store the maximum sum
return dp[pos][left] = value;
}
// Driver Code
let n = 5;
let a = [ 1, 5, 4, 3, 2 ];
let k = 1;
let m = 4;
let dp = new Array(n + 1);
// Loop to create 2D array using 1D array
for (var i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
for (var i = 0; i < dp.length; i++) {
for (var j = 0; j < dp.length; j++) {
dp[i][j] = -1;
}
}
document.write(
(a[0] + maxValue(a, n, 1, m, k, dp)));
</script>
Time Complexity: O(N * K)
Auxiliary Space: O(N * K)
Similar Reads
Maximize the sum of selected numbers from an array to make it empty Given an array A[] of N numbers, we need to maximize the sum of selected numbers following the given operation: At each step, you need to select a number Ai, delete one occurrence and add it to the sum.Delete one occurrence of Ai-1 and Ai+1 (if they exist in the array). Repeat these steps until the
15+ min read
Maximize sum of selected numbers from Array to empty it | Set 2 Given an array arr[] of N integers, the task is to maximize the sum of selected numbers over all the operations such that in each operation, choose a number Ai, delete one occurrence of it and delete all occurrences of Ai - 1 and Ai + 1 (if they exist) in the array until the array gets empty. Exampl
7 min read
Maximum score possible from an array with jumps of at most length K Given an array arr[] and an integer k. The task is to find the maximum score we can achieve by performing the following operations:Start at the 0th index of the array.Move to the last index of the array by jumping at most k indices at a time. For example, from the index i, we can jump to any index b
15+ min read
Reconstructing the Array with Maximum Possible Sum by Given Operations Consider an array A[] of length N. Suppose, you can create obtain B[] from A[] using below steps: Iterate N/2 times on A and follow below the step below:Insert (Ai + Ai+(N/2)) and |Ai - Ai+(N/2)| into B[]After that rearrange B[] in any random order.You are given B[], the task is to reconstructing an
12 min read
Collect maximum points in an array with k moves Given an array of integer and two values k and i where k is the number of moves and i is the index in the array. The task is to collect maximum points in the array by moving either in single or both directions from given index i and making k moves. Note that every array element visited is considered
9 min read
Maximize sum by shifting from one Array to other at most once Given two arrays X[] and Y[] of length N each. You can shift from X[] to Y[] at most once. The task is to output the maximum sum possible sum. Examples: Input: N = 4, X[] = {7, 5, 3, 4}, Y[] = {2, 3, 1, 3}Output: 19Explanation: It is optimal to not shift from X[] to Y[]. The total maximum possible s
7 min read