Skip to main content

Three Equal Parts

Challenge on lesson: Checkpoint — Prefix Sums

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** Count the ways to cut the array a[1..n] into three contiguous, non-empty parts with equal sums. Cuts are after position i and after position j (1 <= i < j <= n-1... precisely: part 1 is a[1..i], part 2 is a[i+1..j], part 3 is a[j+1..n]). Print the number of valid (i, j) pairs. The answer fits in 64 bits. **Input:** Line 1: n (3 <= n <= 2*10^5). Line 2: n integers (-10^9 <= a_i <= 10^9). **Output:** One integer — the number of ways. **Example:** 5 / 1 2 3 0 3 -> 2 (cuts after 2 and 3; after 2 and 4).

Back to lesson: Checkpoint — Prefix Sums