Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,3,1]
[3,1,1,2]
range-sum-query-immutable)maximum-sum-of-3-non-overlapping-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3427: Sum of Variable Length Subarrays
class Solution {
public int subarraySum(int[] nums) {
int n = nums.length;
int[] s = new int[n + 1];
for (int i = 1; i <= n; ++i) {
s[i] = s[i - 1] + nums[i - 1];
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += s[i + 1] - s[Math.max(0, i - nums[i])];
}
return ans;
}
}
// Accepted solution for LeetCode #3427: Sum of Variable Length Subarrays
func subarraySum(nums []int) (ans int) {
s := make([]int, len(nums)+1)
for i, x := range nums {
s[i+1] = s[i] + x
}
for i, x := range nums {
ans += s[i+1] - s[max(0, i-x)]
}
return
}
# Accepted solution for LeetCode #3427: Sum of Variable Length Subarrays
class Solution:
def subarraySum(self, nums: List[int]) -> int:
s = list(accumulate(nums, initial=0))
return sum(s[i + 1] - s[max(0, i - x)] for i, x in enumerate(nums))
// Accepted solution for LeetCode #3427: Sum of Variable Length Subarrays
fn subarray_sum(nums: Vec<i32>) -> i32 {
let mut ret = 0;
for i in 0..nums.len() {
let start = std::cmp::max(0, i as i32 - nums[i]) as usize;
for j in start..=i {
ret += nums[j];
}
}
ret
}
fn main() {
let nums = vec![2, 3, 1];
let ret = subarray_sum(nums);
println!("ret={ret}");
}
#[test]
fn test() {
{
let nums = vec![2, 3, 1];
let ret = subarray_sum(nums);
assert_eq!(ret, 11);
}
{
let nums = vec![3, 1, 1, 2];
let ret = subarray_sum(nums);
assert_eq!(ret, 13);
}
}
// Accepted solution for LeetCode #3427: Sum of Variable Length Subarrays
function subarraySum(nums: number[]): number {
const n = nums.length;
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] + nums[i];
}
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += s[i + 1] - s[Math.max(0, i - nums[i])];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.