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 a move including arr[i].
Constraints :
n is the size of array.
0 <= i < n.
1 <= k <= n
Examples:
Input : arr[] = { 5, 6, 4, 2, 8, 3, 1 }
k = 3, i = 3
Output : Maximum point: 14
Explanation: arr[i] is 2
All possible ways to collect points in the array
by moving either both or a single direction are:
case 1: 6 + 4 + 2 = 12
case 2: 4 + 2 + 8 = 14
case 3: 2 + 8 + 3 = 13
So maximum points we collects in k moves : 14
Input : arr[] = { 5, 6, 4, 2, 8, 3, 1 }
k = 2, i = 5
Output : Maximum point: 11 ( 8 + 3 )
A Simple Solution is to generate all subarrays of size k (one thing to notice that we only generate subarrays that contain index(I)), compute their sums, and finally return the maximum of all sums. The time complexity of this solution is O(n*k)
An Efficient Solution is based on the article maximum sum_subarray of size k. The idea is to use the fact that the sum of a subarray of size k can be obtained using the sum of the previous subarray of size k. Except for the first subarray of size k, for other subarrays, we compute the sum by removing the first element of the last window and adding the last element of the current window.
Time complexity: O(n)
Below is the implementation of the above idea.
C++
#include <iostream>
using namespace std;
int maximumPoints( int arr[], int n, int k, int i)
{
int start;
if (k > i)
start = 0;
else
start = i - k;
int res = 0;
for ( int j = start; j <= start + k && j < n; j++)
res += arr[j];
int curr_sum = res;
for ( int j = start + k + 1; j < n && j <= i + k; j++)
{
curr_sum += arr[j] - arr[j - k - 1];
res = max(res, curr_sum);
}
return res;
}
int main()
{
int arr[] = { 5, 6, 4, 2, 8, 3, 1 };
int k = 3, i = 3;
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Maximum points : "
<< maximumPoints(arr, n, k - 1, i);
return 0;
}
|
Java
class GFG {
static int maximumPoints( int arr[], int n, int k, int i)
{
int start;
if (k > i)
start = 0 ;
else
start = i - k;
int res = 0 ;
for ( int j = start; j <= start + k && j < n; j++)
res += arr[j];
int curr_sum = res;
for ( int j = start + k + 1 ; j < n && j <= i + k; j++)
{
curr_sum += arr[j] - arr[j - k - 1 ];
res = Math.max(res, curr_sum);
}
return res;
}
public static void main (String[] args)
{
int arr[] = { 5 , 6 , 4 , 2 , 8 , 3 , 1 };
int k = 3 , i = 3 ;
int n = arr.length;
System.out.print( "Maximum points : "
+maximumPoints(arr, n, k - 1 , i));
}
}
|
Python3
def maximumPoints(arr, n, k, i):
start = 0
if (k > i):
start = 0
else :
start = i - k
res = 0
j = start
while (j < = start + k and j < n):
res + = arr[j]
j + = 1
curr_sum = res
j = start + k + 1
while (j < n and j < = i + k):
curr_sum + = arr[j] - arr[j - k - 1 ]
res = max (res, curr_sum)
j + = 1
return res
arr = [ 5 , 6 , 4 , 2 , 8 , 3 , 1 ]
k, i = 3 , 3
n = len (arr)
print ( "Maximum points :" , maximumPoints(arr, n, k - 1 , i))
|
C#
using System;
class GFG {
static int maximumPoints( int []arr, int n, int k, int i)
{
int start;
if (k > i)
start = 0;
else
start = i - k;
int res = 0;
for ( int j = start; j <= start + k && j < n; j++)
res += arr[j];
int curr_sum = res;
for ( int j = start + k + 1; j < n && j <= i + k; j++)
{
curr_sum += arr[j] - arr[j - k - 1];
res = Math.Max(res, curr_sum);
}
return res;
}
public static void Main ()
{
int []arr = { 5, 6, 4, 2, 8, 3, 1 };
int k = 3, i = 3;
int n = arr.Length;
Console.Write( "Maximum points : "
+maximumPoints(arr, n, k - 1, i));
}
}
|
PHP
<?php
function maximumPo( $arr , $n , $k , $i )
{
$start ;
if ( $k > $i )
$start = 0;
else
$start = $i - $k ;
$res = 0;
for ( $j = $start ; $j <= $start +
$k and $j < $n ; $j ++)
$res += $arr [ $j ];
$curr_sum = $res ;
for ( $j = $start + $k + 1; $j < $n and
$j <= $i + $k ; $j ++)
{
$curr_sum += $arr [ $j ] - $arr [ $j - $k - 1];
$res = max( $res , $curr_sum );
}
return $res ;
}
$arr = array (5, 6, 4, 2, 8, 3, 1);
$k = 3; $i = 3;
$n = count ( $arr );
echo "Maximum pos : " ,
maximumPo( $arr , $n , $k - 1, $i );
?>
|
Javascript
<script>
function maximumPoints(arr, n, k, i)
{
var start;
if (k > i)
start = 0;
else
start = i - k;
var res = 0;
for ( var j = start; j <= start + k && j < n; j++)
res += arr[j];
var curr_sum = res;
for ( var j = start + k + 1; j < n && j <= i + k; j++)
{
curr_sum += arr[j] - arr[j - k - 1];
res = Math.max(res, curr_sum);
}
return res;
}
var arr = [ 5, 6, 4, 2, 8, 3, 1 ];
var k = 3, i = 3;
var n = arr.length;
document.write( "Maximum points : "
+ maximumPoints(arr, n, k - 1, i));
</script>
|
Output
Maximum points : 14
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...