Prefix Sum Range Query

L4 Easy Sliding Window
Concept
Build a prefix-sum array once, and any range sum becomes a single subtraction — O(1) per query no matter how large the range is.
Given an array nums and two indices left and right (inclusive), return the sum of the elements between those indices. If the array is empty, return 0.
Examples
▸ nums = [-2, 0, 3, -5, 2, -1], left = 0, right = 2
→ 1
▸ nums = [-2, 0, 3, -5, 2, -1], left = 2, right = 5
→ -1
▸ nums = [2, 4, 6, 8], left = 1, right = 2
→ 10
Progressive Hints
Hint 1 · Nudge
Precompute once, then answer every range instantly with a subtraction.
Hint 2 · Plan
Build a prefix array where prefix[i] is the sum of the first i elements, with prefix[0] = 0. The inclusive range sum from left to right is prefix[right + 1] - prefix[left]. Return 0 when the array is empty.
Hint 3 · Approach
If nums is empty, return 0. Build prefix with prefix[0] = 0 and prefix[i + 1] = prefix[i] + nums[i]. Return prefix[right + 1] - prefix[left].
Output
// Run your code to see the output here.