Skip to content

Latest commit

 

History

History
30 lines (16 loc) · 1.24 KB

Ep2-Prefix-Sums.md

File metadata and controls

30 lines (16 loc) · 1.24 KB

Part 3 Episode 2/3: Prefix Sums

Given an array A of n elements, prefix sums are a very powerful technique that allows you to calculate the sum of any range of elements [l, r] for 1 <= l <= r <= n in O(1) constant time with the help of O(n) pre-processing.

The trick is to pre-calculate in another array prefix, of cumulative sums, such that for each i from 1 to n, prefix[i] stores the sum a[1] + a[2] + ... + a[i]. Then, the sum of elements in the range [l,r] can be easily found out as prefix[r] - prefix[l-1].

Tutorial on Prefix Sums

Video Tutorial

Peltorator on Prefix Sums: Great educational video, however it is in Russian with english subtitles.


Here are some links to practice prefix sum based problems:

  1. Spoj CSUM

  2. Codechef Shivgod

  3. Codeforces 433 B

  4. Codeforces 1118 B


Challenge Problems:

  1. Codeforces 1363 B