VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-score-possible-from-an-array-with-jumps-of-at-most-length-k/

⇱ Maximum score possible from an array with jumps of at most length K - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum score possible from an array with jumps of at most length K

Last Updated : 23 Jul, 2025

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 between i + 1 and i + k (inclusive) as long as it is within the bounds of the array.
  • Collect the value of each index we land on, including the value at the starting index (0th index).

Examples:

Input: arr[] = [100, -30, -50, -15, -20, -30], k = 3
Output: 55
Explanation: From 0th index, jump 3 indices ahead to arr[3]. From 3rd, jump 2 steps ahead to arr[5]. Therefore, the maximum score possible = (100 + (-15) + (-30)) = 55

Input: arr[] = [-44, -17, -54, 79],  k = 2
Output: 18
Explanation: From 0th index, jump 1 index ahead to arr[1]. From index 1, jump 2 steps ahead to arr[3]. Therefore, the maximum score possible = -44 + (-17) + 79 = 18.

Using Top-Down DP (Memoization) - O(n*k) Time and O(n) Space

The idea is to use recursion with memoization to explore all possible paths from the first index to the last, aiming to find the maximum score.

  • Starting from any index, the function calculates the score by adding the current index value and the score obtained from valid jumps within a range of k.
  • If the last index is reached, the value at that index is returned as the base case.
  • If a result for the current index has already been computed, it is retrieved from the memo[] array to avoid redundant calculations.

Steps to implement the above idea:

  • Initialize a memoization array with -1 to store computed results.
  • Define a recursive function that computes the maximum score from the current index, checking memoized values to avoid recomputation.
  • Base case: If at the last index, return its value.
  • Iterate through jumps (from 1 to k), recursively computing the score for each valid jump.
  • Memoize the maximum score obtained from all possible jumps at the current index.

Output
55

Using Bottom-Up DP (Tabulation) - O(n*k) Time and O(n) Space

The idea is to use dynamic programming with a bottom-up approach to find the maximum score:

  • Instead of recursively exploring paths, a dp array stores the best possible score from each index to the end.
  • The solution builds backward, ensuring each index considers the best jump within k steps using previously computed results.
  • This avoids redundant calculations, making it more efficient than recursion. The final answer is found at dp[0], representing the maximum score from the start.

Steps to implement the above idea:

  • Initialize a dp array of size n with minimum integer value.
  • Set the last index of dp as arr[n-1] (base case).
  • Iterate backward from the second last index to the first.
  • For each index, check all valid jumps up to k steps.
  • Update dp[i] as the maximum possible score from that index.
  • Return dp[0], which gives the maximum score from the start.

Output
55

Using DP + Heap - O(n*log(k)) Time and O(k) Space

The idea is to improve the efficiency of the basic tabulation method by using a max-heap.

  • In the simple tabulation method, for each index, all possible jumps within the range k are evaluated, which can be time-consuming when k is large.
  • To optimize this, the max-heap dynamically tracks the maximum scores within the valid range, ensuring faster access to the best possible jump.
  • The heap allows the algorithm to focus only on the relevant scores, making the computation significantly faster.

Steps to implement the above idea:

  • Initialize a dp array of size n with minimum integer value.
  • Set dp[n-1] as arr[n-1] and push it into a max-heap.
  • Iterate backward from the second last index to the first.
  • Remove out-of-range elements from the heap (index > i + k).
  • Set dp[i] as the max heap's top value + arr[i].
  • Push dp[i] and its index into the max-heap and return dp[0].

Output
55

Using DP + Deque - O(n) Time and O(k) Space

The Deque (Double-Ended Queue) provides an efficient alternative to the max-heap by maintaining a sliding window of indices corresponding to maximum values in the dp array. Unlike a heap, which requires logarithmic operations, the deque allows for constant time operations to remove outdated indices and insert new ones, making it ideal for this problem.

Steps to implement the above idea:

  • Initialize a dp array of size n with minimum integer value.
  • Set dp[n-1] as arr[n-1] and push its index into a deque.
  • Iterate backward from the second last index to the first.
  • Remove out-of-range indices from the front of the deque.
  • Set dp[i] as arr[i] + dp[dq.front()].
  • Maintain deque order, remove smaller values from the back, and push I.

Output
55
Comment