Maximize the score of a path that jumps at most k steps to the last index

Quick Overview

Given an integer array and a jump limit k, find the largest total of visited values on a path from the first index to the last when every jump moves forward at most k steps. It tests dynamic programming and how to get each window's best value fast enough for inputs of 100,000 elements.

Maximize the score of a path that jumps at most k steps to the last index

Company: Nutanix

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 0-indexed integer array `nums` and an integer `k`. You are initially standing at index `0`. In one move, you can jump at most `k` steps forward without going outside the boundaries of the array. That is, you can jump from index `i` to any index in the range `[i + 1, min(n - 1, i + k)]` inclusive, where `n` is the length of `nums`. You want to reach the last index of the array (index `n - 1`). Your score is the sum of all `nums[j]` for each index `j` you visited in the array, including both the first and the last index. Scores may be negative. Return the maximum score you can get. ### Function Signature ```python def max_jump_score(nums: list[int], k: int) -> int: ``` ### Rules - Index `0` and index `n - 1` always count toward the score, even when their values are negative. When `n` is 1 you are already on the last index, and the score is `nums[0]`. - Only the indices you land on count. Indices you jump over add nothing. - Return one integer: the largest total over all valid jump sequences. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= k <= 10^5` - `-10^4 <= nums[i] <= 10^4` ### Examples ```text Input: nums = [1, -1, -2, 4, -7, 3], k = 2 Output: 7 ``` Landing on indices 0, 1, 3 and 5 visits the values `[1, -1, 4, 3]`, which sum to 7. Skipping both index 1 and index 2 would take a jump of 3, so one of those two negatives must be paid, and -1 is the cheaper one. ```text Input: nums = [10, -5, -2, 4, 0, 3], k = 3 Output: 17 ``` A jump of 3 from index 0 to index 3 skips both negatives, and a jump of 2 reaches index 5. The visited values `[10, 4, 3]` sum to 17. ```text Input: nums = [-1, -5, -3, -2], k = 2 Output: -6 ``` Every path pays `nums[0]` and `nums[3]`. Index 3 is out of reach from index 0, so the path must also land on index 1 or index 2, and landing only on index 2 gives `-1 + (-3) + (-2) = -6`, the largest total available.

Overview: Given an integer array and a jump limit k, find the largest total of visited values on a path from the first index to the last when every jump moves forward at most k steps. It tests dynamic programming and how to get each window's best value fast enough for inputs of 100,000 elements.

Nutanix logo
Nutanix
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a 0-indexed integer array nums and an integer k.

You are initially standing at index 0. In one move, you can jump at most k steps forward without going outside the boundaries of the array. That is, you can jump from index i to any index in the range [i + 1, min(n - 1, i + k)] inclusive, where n is the length of nums.

You want to reach the last index of the array (index n - 1). Your score is the sum of all nums[j] for each index j you visited in the array, including both the first and the last index. Scores may be negative.

Return the maximum score you can get.

Function Signature

def max_jump_score(nums: list[int], k: int) -> int:

Rules

  • Index 0 and index n - 1 always count toward the score, even when their values are negative. When n is 1 you are already on the last index, and the score is nums[0] .
  • Only the indices you land on count. Indices you jump over add nothing.
  • Return one integer: the largest total over all valid jump sequences.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= k <= 10^5
  • -10^4 <= nums[i] <= 10^4

Examples

Input: nums = [1, -1, -2, 4, -7, 3], k = 2
Output: 7

Landing on indices 0, 1, 3 and 5 visits the values [1, -1, 4, 3], which sum to 7. Skipping both index 1 and index 2 would take a jump of 3, so one of those two negatives must be paid, and -1 is the cheaper one.

Input: nums = [10, -5, -2, 4, 0, 3], k = 3
Output: 17

A jump of 3 from index 0 to index 3 skips both negatives, and a jump of 2 reaches index 5. The visited values [10, 4, 3] sum to 17.

Input: nums = [-1, -5, -3, -2], k = 2
Output: -6

Every path pays nums[0] and nums[3]. Index 3 is out of reach from index 0, so the path must also land on index 1 or index 2, and landing only on index 2 gives -1 + (-3) + (-2) = -6, the largest total available.