The formula uses 1-indexed prefixes. If your array is 0-indexed, be careful. Common mistake: using when .
This accesses . Handle the edge case or use 1-indexed prefixes. Test your implementation: sum of should be . Sum of should be total sum. If these fail, check your indices. I recommend 1-indexed prefixes with . Then works for all valid ranges.