Reach End of Array With Max Score - Problem

You are given an integer array nums of length n. Your goal is to start at index 0 and reach index n - 1. You can only jump to indices greater than your current index.

The score for a jump from index i to index j is calculated as (j - i) * nums[i].

Return the maximum possible total score by the time you reach the last index.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,3,2,5]
Output: 7
💡 Note: Jump from index 0 to 2 (score = 2×1 = 2), then from index 2 to 3 (score = 1×2 = 2). Total = 4. But optimal is 0→2→3: (2×1) + (1×2) = 4, or 0→1→3: (1×1) + (2×3) = 7
Example 2 — Two Elements
$ Input: nums = [3,4]
Output: 3
💡 Note: Only one jump possible from index 0 to 1: score = (1-0) × 3 = 3
Example 3 — Decreasing Array
$ Input: nums = [5,3,1]
Output: 10
💡 Note: Best strategy is to jump directly from 0 to 2: score = (2-0) × 5 = 10

Constraints

  • 2 ≤ nums.length ≤ 105
  • -106 ≤ nums[i] ≤ 106

Visualization

Tap to expand
Reach End of Array With Max Score INPUT Array: nums 1 i=0 3 i=1 2 i=2 5 i=3 Length n = 4 Start: index 0 Goal: reach index 3 Score Formula: (j - i) * nums[i] Jump from i to j Maximize total score ALGORITHM STEPS 1 Track Maximum Value Keep max value seen so far 2 Greedy Selection Always use best value for each distance unit 3 Accumulate Score Add max_val for each step 4 Update Maximum Update max when better found i=0: max=1, score=0 i=1: max=3, score+=1 = 1 i=2: max=3, score+=3 = 4 i=3: max=5, score+=3 = 7 Total: 7 FINAL RESULT Optimal Jump Path 1 3 2 5 Score Breakdown: Jump 0 to 1: (1-0)*1 = 1 Jump 1 to 3: (3-1)*3 = 6 Total: 1 + 6 = 7 OUTPUT 7 Maximum Score OK Key Insight: The greedy approach works because for each position we traverse, we want to use the maximum value seen so far. Each unit of distance contributes max_value to the score. We don't need to track actual jumps - just accumulate score as we scan right, updating max value when improved. TutorialsPoint - Reach End of Array With Max Score | Greedy Maximum Value Approach
Asked in
Google 15 Amazon 12 Microsoft 8
28.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen