Practice · 1 of 5
Debug: Off-by-One Sum
CHALLENGE
Difficulty: intermediate+25 XP
**Description:** The program below must print the sum of all n array elements,
but it is wrong. Identify the bug and fix it.
**Broken code:**
``cpp
void solve(std::istream& in, std::ostream& out) {
int n; in >> n;
vector<long long> a(n);
for (auto& x : a) in >> x;
long long s = 0;
for (int i = 0; i < n - 1; ++i) s += a[i];
out << s;
}
`
**Input:** Line 1: n (1 <= n <= 100000). Line 2: n integers (|a[i]| <= 10^9).
**Output:** One integer — the total sum.
**Example:** 3 / 1 2 3 -> 6`.
Press Submit to check your solution.
Back to lesson: Practice: Debugging Problem Set