Skip to main content

Practice · 4 of 5

Debug: Backwards Sort

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** The program must sort n pairs by their first element ASCENDING (ties: any order) and print the first elements. Find and fix the bug. **Broken code:** ``cpp void solve(std::istream& in, std::ostream& out) { int n; in >> n; vector<pair<int,int>> v(n); for (auto& p : v) in >> p.first >> p.second; sort(v.begin(), v.end(), [](const pair<int,int>& x, const pair<int,int>& y) { return x.first > y.first; }); for (int i = 0; i < n; ++i) out << v[i].first << (i + 1 < n ? " " : ""); out << "\\n"; } ` **Input:** Line 1: n (1 <= n <= 100000). Next n lines: a b (|a|, |b| <= 10^9). **Output:** One line: the first elements in ascending order, space-separated. **Example:** 3 / 3 0 / 1 9 / 2 4 -> 1 2 3`.

Back to lesson: Practice: Debugging Problem Set