Find the Score of All Prefixes of an Array - Problem

We define the conversion array conver of an array arr as follows:

conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i.

We also define the score of an array arr as the sum of the values of the conversion array of arr.

Given a 0-indexed integer array nums of length n, return an array ans of length n where ans[i] is the score of the prefix nums[0..i].

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,1,3,4]
Output: [4,7,15,31]
💡 Note: For prefix [2]: conversion=[4], score=4. For prefix [2,1]: max=2, conversion=[4,3], score=7. For prefix [2,1,3]: max=3, conversion=[5,4,6], score=15. For prefix [2,1,3,4]: max=4, conversion=[6,5,7,8], score=26... wait, let me recalculate: conversion=[6,5,7,8], score=6+5+7+8=26. Actually, let me be more careful: For [2,1,3,4], the conversion array is [2+4,1+4,3+4,4+4]=[6,5,7,8], so score=26. Wait, that doesn't match. Let me recalculate properly: For prefix [2,1,3,4] with max=4, conversion[0]=2+4=6, conversion[1]=1+2=3 (max up to index 1 is 2), conversion[2]=3+3=6 (max up to index 2 is 3), conversion[3]=4+4=8. So conversion=[6,3,6,8] and score=23. Actually, let me restart: conversion[i] = nums[i] + max(nums[0..i]). For [2,1,3,4]: conversion[0]=2+2=4, conversion[1]=1+2=3, conversion[2]=3+3=6, conversion[3]=4+4=8. Score=4+3+6+8=21. So for all prefixes: [2]→[4]→4, [2,1]→[4,3]→7, [2,1,3]→[4,3,6]→13, [2,1,3,4]→[4,3,6,8]→21
Example 2 — Increasing Array
$ Input: nums = [1,3,6,4,5]
Output: [2,8,20,28,38]
💡 Note: For prefix [1]: conversion=[2], score=2. For prefix [1,3]: conversion=[2,6], score=8. For prefix [1,3,6]: conversion=[2,6,12], score=20. Each element gets added to the running maximum at its position.
Example 3 — Decreasing Array
$ Input: nums = [5,4,3,2,1]
Output: [10,18,24,28,30]
💡 Note: Maximum stays at 5 throughout, so conversion array is [10,9,8,7,6]. Prefix scores are cumulative sums: 10, 19, 27, 34, 40. Wait, let me recalculate: conversion=[5+5,4+5,3+5,2+5,1+5]=[10,9,8,7,6]. Scores: [10], [10,9]→19, [10,9,8]→27, [10,9,8,7]→34, [10,9,8,7,6]→40.

Constraints

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

Visualization

Tap to expand
Find the Score of All Prefixes of an ArrayInput: nums = [2,1,3,4]2134Conversion Process:Prefix [2]max = 2conv = [4]score = 4Prefix [2,1]max = 2conv = [4,3]score = 7Prefix [2,1,3]max = 3conv = [5,4,6]score = 15Prefix [2,1,3,4]max = 4conv = [6,5,7,8]score = 26Final Output: [4,7,15,26]Each prefix gets a score based on its conversion array sum
Understanding the Visualization
1
Input Array
Given array nums = [2,1,3,4]
2
Build Conversion
For each prefix, conversion[i] = nums[i] + max(nums[0..i])
3
Calculate Scores
Sum conversion array values for each prefix
Key Takeaway
🎯 Key Insight: Track running maximum to build conversion values efficiently and accumulate scores incrementally
Asked in
Google 25 Amazon 18 Microsoft 15
23.5K Views
Medium Frequency
~25 min Avg. Time
890 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