FastPrepMaximum Game Board Score
Problem · Array

Maximum Game Board Score

Learn this problem
MediumJPMorgan Chase logoJPMorgan ChaseNEW GRADINTERNOA

Problem statement

You are given a game board represented by an integer array gameVal. The spaces are arranged in a straight line, and each value is the number of points on that space.

Choose any space as the starting position. Add the points on that space, then repeatedly jump exactly k spaces to the right and add the points on every space you land on. The game ends when the next jump would move past the final space.

Return the maximum total score obtainable by choosing the best starting position.

Function

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

Examples

Example 1

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

Starting at index 0 visits values 2, 4, and 1, producing 2 + 4 + 1 = 7. No other starting position produces a larger total.

Example 2

gameVal = [-5,-2,-7]k = 1return = -7

Every selected starting position must continue jumping until it leaves the board. Starting at the final space produces -7, which is greater than the totals -14 and -9 from the earlier positions.

Constraints

  • 1 <= gameVal.length <= 10^6
  • -10^3 <= gameVal[i] <= 10^3
  • 1 <= k < gameVal.length

More JPMorgan Chase problems

drafts saved locally
public int maxGameScore(int[] gameVal, int k) {
  // Write your code here.
}
gameVal[2,-3,4,6,1]
k2
expected7
checking account