Data Structures & Algorithms using JavaScript
/Expert
Prefix Sum
Definition
An array `P` where `P[i]` is the sum of all elements from index `0` to `i`. It allows calculating the sum of any subarray `[L, R]` in O(1) time using the formula `P[R] - P[L-1]`.
Explain Like I'm New
Pre-calculating a running total so that if someone asks 'what is the sum from day 5 to day 10', you just take 'Total at day 10' minus 'Total at day 4'.
Interactive Coding Challenges
Calculate Prefix Sum Array
Solution Code
Loading...
Console output will appear here...