Original problem · C++ solution
Every route must cover the distance between the extremes.
Every route must cover the distance between the extremes. Visiting tasks in sorted order attains that distance.
- Time: O(1).
- Extra space: O(1).
Integer division truncates toward zero; use remainder to test divisibility. Evaluate products in long long before assigning the result.
Follow the exact input and output formats; check the smallest allowed input.
State the invariant without looking at the code. Give one input that breaks the most tempting incorrect approach, then add it to the tests.