Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Sum of Variable Length Subarrays

XPChallenge Points: 10
levelLevel: Easy

You are given an array nums of length n. For every index i, you create a subarray that ends at index i, but the starting position depends on the value at index i.

Specifically:

start = max(0, i - nums[i])

So the subarray considered is:

nums[start ... i]

You must find the sum of all elements from all such subarrays for every i in the array and return the total sum.

Example 1:

Input: nums = [1,2,2]

Output: 8

Explanation:

i Subarray Sum 0 [1] 1 1 [1,2] 3 2 [1,2,2] 5 Total Sum = 1 + 3 + 5 = 8

Example 2:

Input: nums = [4,0,3]

Output: 10

Explanation:

i Subarray Sum 0 [4] 4 1 [0] 0 2 [0,3] 3 Total Sum = 4 + 0 + 3 = 7

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result