Imagine you're analyzing data streams where each position determines how far back to look for relevant information. You are given an integer array nums of size n. Your task is to process each index i (where 0 <= i < n) by creating a variable-length subarray.
For each index i, the subarray starts at position start = max(0, i - nums[i]) and ends at position i (inclusive). This means the value at each position tells you how many elements to look back from that position.
Goal: Return the total sum of all elements from all the subarrays defined for each index in the array.
Example: If nums = [1, 2, 3]:
• At index 0: subarray from max(0, 0-1) = 0 to 0 → [1]
• At index 1: subarray from max(0, 1-2) = 0 to 1 → [1, 2]
• At index 2: subarray from max(0, 2-3) = 0 to 2 → [1, 2, 3]
Total sum = 1 + (1+2) + (1+2+3) = 10
Input & Output
Constraints
- 1 ≤ nums.length ≤ 1000
- 0 ≤ nums[i] ≤ 100
- All array elements are non-negative integers