Sum of Variable Length Subarrays

You are given an integer array nums of size n. For each index i where 0 <= i < n, define a subarray nums[start ... i] where start = max(0, i - nums[i]).

Return the total sum of all elements from the subarray defined for each index in the array.

Example 1
Inputnums = [2,3,1]
Output11
The subarray sums for indices 0, 1, and 2 are 2, 5, and 4, so the total sum is 11.
Example 2
Inputnums = [3,1,1,2]
Output13
The subarray sums for indices 0, 1, 2, and 3 are 3, 4, 2, and 4, so the total sum is 13.

Constraints

  • 1 <= n == nums.length <= 100
  • 1 <= nums[i] <= 1000

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate