FastPrepMaximum Game Board Score

Maximum Game Board Score

JP Morgan Chase logoJP Morgan Chase● MediumNEW GRADINTERNOA

Problem statement

You are given a game board with spaces arranged in a straight line, each space worth a different number of points, either positive or negative. The goal of the game is to accumulate as many points as possible.

Your game piece can start at any point on the board, and it can jump exactly k number of spaces at a time, only moving to the right, accumulating the number of points written on each space your piece lands on. Once your piece jumps past the final space, the game ends.

Given a game board, and a jump length, choose the ideal starting space to maximize the number of points gained. Return the total points.

Function

maxGameScore(gameVal: int[], k: int) → int

Examples

Example 1

gameVal = [2,-3,4,6,1]k = 2return = 7

Suppose game_val = [2, -3, 4, 6, 1] and k = 2.

Output: 7

Choose index = 1 as starting point.

2 + 4 + 1 = 7

Constraints

  • 1 ≤ size of game_val ≤ 10^6
  • -10^3 ≤ game_val[i] ≤ 10^3
  • 1 ≤ k < size of game_val

More JP Morgan Chase problems

See JP Morgan Chase hiring insights
Saved
public int maxGameScore(int[] gameVal, int k) {
  // Write your code here.
}
gameVal[2,-3,4,6,1]
k2
expected7
Checking account…