Collect maximum points in an array with k moves
Last Updated :
18 Sep, 2023
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++
// C++ program to Collect maximum point
// in array with k moves.
#include <iostream>
using namespace std;
// function return maximum point
// that we can collect with K moves
int maximumPoints(int arr[], int n, int k, int i)
{
// Compute sum of first window of size k in which
// we consider subArray from index ( 'i-k' to 'i' )
// store starting index of sub_array
int start;
if (k > i)
// sub_array from ( 0 to I+(K-I))
start = 0;
else
// sub_array from ( i-i, to i )
start = i - k;
int res = 0;
for (int j = start; j <= start + k && j < n; j++)
res += arr[j];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
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;
}
// Driver code
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
// Java program to Collect maximum point
// in array with k moves.
class GFG {
// function return maximum point
// that we can collect with K moves
static int maximumPoints(int arr[], int n, int k, int i)
{
// Compute sum of first window of size k in which
// we consider subArray from index ( 'i-k' to 'i' )
// store starting index of sub_array
int start;
if (k > i)
// sub_array from ( 0 to I+(K-I))
start = 0;
else
// sub_array from ( i-i, to i )
start = i - k;
int res = 0;
for (int j = start; j <= start + k && j < n; j++)
res += arr[j];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
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;
}
// driver code
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));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to Collect maximum point
# in array with k moves.
# function return maximum point
# that we can collect with K moves
def maximumPoints(arr, n, k, i):
# Compute sum of first window of size k in which
# we consider subArray from index ( 'i-k' to 'i' )
# store starting index of sub_array
start = 0
if (k > i):
# sub_array from ( 0 to I+(K-I))
start = 0
else:
# sub_array from ( i-i, to i )
start = i - k
res = 0
j = start
while(j <= start + k and j < n):
res += arr[j]
j += 1
# Compute sums of remaining windows by
# removing first element of previous
# window and adding last element of
# current window.
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
# Driver code
arr = [ 5, 6, 4, 2, 8, 3, 1 ]
k, i = 3, 3
n = len(arr)
print ("Maximum points :", maximumPoints(arr, n, k - 1, i))
# This code is contributed by Sachin Bisht
C#
// C# program to Collect maximum point
// in array with k moves.
using System;
class GFG {
// function return maximum point
// that we can collect with K moves
static int maximumPoints(int []arr, int n, int k, int i)
{
// Compute sum of first window of size k
// in which we consider subArray from
// index ( 'i-k' to 'i' ) and store
// starting index of sub_array
int start;
if (k > i)
// sub_array from ( 0 to I+(K-I))
start = 0;
else
// sub_array from ( i-i, to i )
start = i - k;
int res = 0;
for (int j = start; j <= start + k && j < n; j++)
res += arr[j];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
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;
}
// driver code
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));
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program to Collect maximum
// points in array with k moves.
// function return maximum point
// that we can collect with K moves
function maximumPo($arr, $n, $k, $i)
{
// Compute sum of first
// window of size k in which
// we consider subArray from
// index ( 'i-k' to 'i' )
// store starting index of
// sub_array
$start;
if ($k > $i)
// sub_array from (0 to
// I + (K - I))
$start = 0;
else
// sub_array from (i - i, to i )
$start = $i - $k;
$res = 0;
for ($j = $start; $j <= $start +
$k and $j < $n; $j++)
$res += $arr[$j];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
$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;
}
// Driver code
$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);
// This code is contributed by anuj_67
?>
JavaScript
<script>
// Javascript program to Collect maximum point
// in array with k moves.
// function return maximum point
// that we can collect with K moves
function maximumPoints(arr, n, k, i)
{
// Compute sum of first window of size k in which
// we consider subArray from index ( 'i-k' to 'i' )
// store starting index of sub_array
var start;
if (k > i)
// sub_array from ( 0 to I+(K-I))
start = 0;
else
// sub_array from ( i-i, to i )
start = i - k;
var res = 0;
for (var j = start; j <= start + k && j < n; j++)
res += arr[j];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
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;
}
// Driver code
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>
OutputMaximum points : 14
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Collect Maximum Points from all Nodes
Given a tree of N nodes and N-1 edges rooted at Node 1, an array points[] of size N, where points[i] denote the number of points at the vertex i and an integer k. A Node can be visited only if its ancestors have already been visited. When any Node i is visited 2 types of operations can be applied: T
12 min read
Maximize Sum possible from an Array by the given moves
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
8 min read
Count Paths to Exit Matrix Boundary with K Moves
Given three positive integers M, N, K, startRow, and startColumn, there is an M x N matrix and we are at position [startRow, startColumn] within this matrix. We can move at most K times in any four directions: UP, DOWN, LEFT, and RIGHT, and continue moving until we cross the matrix boundary. The tas
9 min read
Find Maximum number of intersecting points with at most K distance
Two players A and B are standing at starting point 0 of an infinite horizontal line. Player A is given an array with jumpA[] of size M and Player B is given an array with jumpB[] of size N. Both players can make jumps as: Player A can jump D distance in both directions (right or left), from its curr
8 min read
Maximum points collected by two persons allowed to meet once
Given a 2-D matrix A[N][M] where A[i][j] denotes the points available on this cell. Two persons, P1 and P2, start from two corners of this matrix. P1 starts from top left and needs to reach bottom right. On the other hand, P2 starts bottom left and needs to reach top right. P1 can move right and dow
12 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
Minimum Element for Non-Positive Array in Limited Moves
Given the array arr[] and another integer max_moves, Find the minimum possible integer x that must be chosen such that all the elements of the array can be made non-positive in most max_moves operations. In each operation, decrease any element of the array by x. Examples: Input: n = 3, arr = [1, 2,
14 min read
Collect maximum points in a grid using two traversals
Given a matrix where every cell represents points. How to collect maximum points using two traversals under following conditions?Let the dimensions of given grid be R x C. The first traversal starts from top left corner, i.e., (0, 0) and should reach left bottom corner, i.e., (R-1, 0). The second tr
14 min read
Minimum number of moves to make a binary array K periodic
Given a binary array arr[] (containing only 0s and 1s) and an integer K. The task is to find the minimum number of moves to make the array K-periodic. An array is said to be K-periodic if the sub-arrays [1 to K], [k+1 to 2K], [2k+1 to 3K], ... are all exactly same. In a single move any 1 can be chan
6 min read
Maximise the size of consecutive element subsets in an array
Given an integer array and an integer k. The array elements denote positions of points on a 1-D number line, find the maximum size of the subset of points that can have consecutive values of points which can be formed by placing another k points on the number line. Note that all coordinates should b
12 min read