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.
Examples
Example 1
Input: nums = [2,3,1]
Output: 11
Explanation:
The total sum is 11. Hence, 11 is the output.
Example 2
Input: nums = [3,1,1,2]
Output: 13
Explanation:
The total sum is 13. Hence, 13 is the output.
Constraints
1 <= n == nums.length <= 100
1 <= nums[i] <= 1000
3427. Sum of Variable Length Subarrays
Easy
10 Points
Array
Prefix Sum
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.
Examples
Example 1
Input: nums = [2,3,1]
Output: 11
Explanation:
The total sum is 11. Hence, 11 is the output.
Example 2
Input: nums = [3,1,1,2]
Output: 13
Explanation:
The total sum is 13. Hence, 13 is the output.
Constraints
1 <= n == nums.length <= 100
1 <= nums[i] <= 1000
Sum of Variable Length Subarrays - Practice Coding | SlaveCode