# Collect maximum points in an array with k moves

Given an array of integer and two value k and i where k is number of moves and i is index in array. Task is to collect maximum points in 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 )
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A Simple Solution is to generate all subarrays of size k (one thing to be notice that we only generate subarrays that contain index(I) ), compute their sums and finally return maximum of all sums. 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 sum of a subarray of size k can be obtained using sum of previous subarray of size k. Except first subarray of size k, for other subarrays, we compute sum by removing first element of last window and adding last element of current window.
Time complexity: O(n)

Below is the implementation of above idea.

## C++

 `// C++ program to Collect maximum point  ` `// in array with k moves. ` `#include ` `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); ` `    ``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

 ` ``\$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 ` `?> `

Output:

```Maximum point : 14
```

Time Complexity : O(n)
Auxiliary Space : O(1)

This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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.

My Personal Notes arrow_drop_up

Improved By : nitin mittal, vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.