Skip to main content

Debug: The Missing Reset

Challenge on lesson: Checkpoint — Debugging

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** This program must print, for each of q values x, how many array elements are less than x — but it degrades after the first query. Find and fix the bug. **Broken code:** ``cpp void solve(std::istream& in, std::ostream& out) { int n, q; in >> n >> q; vector<long long> a(n); for (auto& x : a) in >> x; sort(a.begin(), a.end()); int cnt = 0; for (int i = 0; i < q; ++i) { long long x; in >> x; while (cnt < n && a[cnt] < x) ++cnt; out << cnt << "\\n"; } } ` **Input:** Line 1: n q (1 <= n, q <= 100000). Line 2: n integers (|a[i]| <= 10^9). Next q lines: x (|x| <= 10^9). **Output:** q lines: the count of elements strictly less than x. **Example:** 4 2 / 1 3 5 7 / 4 / 6 -> 2 / 3`.

Back to lesson: Checkpoint — Debugging