Watch 4 video solutions for Sum of Variable Length Subarrays, a easy level problem involving Array, Prefix Sum. This walkthrough by Komal Vhanmane has 735 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
Input: nums = [2,3,1]
Output: 11
Explanation:
| i | Subarray | Sum |
|---|---|---|
| 0 | nums[0] = [2] |
2 |
| 1 | nums[0 ... 1] = [2, 3] |
5 |
| 2 | nums[1 ... 2] = [3, 1] |
4 |
| Total Sum | 11 |
The total sum is 11. Hence, 11 is the output.
Example 2:
Input: nums = [3,1,1,2]
Output: 13
Explanation:
| i | Subarray | Sum |
|---|---|---|
| 0 | nums[0] = [3] |
3 |
| 1 | nums[0 ... 1] = [3, 1] |
4 |
| 2 | nums[1 ... 2] = [1, 1] |
2 |
| 3 | nums[1 ... 3] = [1, 1, 2] |
4 |
| Total Sum | 13 |
The total sum is 13. Hence, 13 is the output.
Constraints:
1 <= n == nums.length <= 1001 <= nums[i] <= 1000Problem Overview: You receive an integer array nums. For every index i, form a subarray that ends at i but whose starting index depends on the value nums[i]. Specifically, the subarray starts at max(0, i - nums[i]) and ends at i. The task is to compute the total sum of all these subarrays.
Approach 1: Direct Subarray Iteration (Brute Force) (Time: O(n²), Space: O(1))
The straightforward approach is to simulate exactly what the problem describes. For each index i, compute the starting index start = max(0, i - nums[i]). Then iterate from start to i and accumulate the values. Repeat this process for every index in the array and add each subarray's sum to the global result. This solution uses only basic iteration over the array and requires no additional data structures.
The downside is repeated work. Elements in the middle of the array may be summed multiple times across overlapping ranges. In the worst case, when many ranges are large, each iteration scans almost the entire prefix of the array, producing O(n²) time complexity. Space remains O(1) because only a few counters are used.
Approach 2: Prefix Sum Optimization (Time: O(n), Space: O(n))
The key observation is that every required range sum can be answered instantly if prefix sums are available. Build a prefix array where prefix[i] stores the sum of the first i elements. With this structure, the sum of any range [l, r] becomes prefix[r + 1] - prefix[l]. This eliminates repeated summation and converts each subarray computation into constant time.
Iterate through the array once. For each index i, compute start = max(0, i - nums[i]). Use the prefix sum array to obtain the subarray sum instantly and add it to the result. Building the prefix array takes O(n), and processing all indices also takes O(n), giving a total time complexity of O(n). The extra prefix array requires O(n) space.
This approach works because range-sum queries become constant-time lookups after preprocessing. The pattern appears frequently in problems involving cumulative ranges or repeated interval queries on an array.
Recommended for interviews: Start by describing the brute-force iteration to show you understand the exact subarray definition. Then move to the prefix-sum optimization. Interviewers typically expect the O(n) prefix sum solution because it removes redundant work and demonstrates familiarity with range-sum techniques.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Subarray Iteration (Brute Force) | O(n²) | O(1) | Useful for understanding the problem or when constraints are very small |
| Prefix Sum Optimization | O(n) | O(n) | General case and expected interview solution for efficient range sum queries |