An observatory catalogs n stars, each with a distinct brightness magnitude (an integer). Astronomers want to know which pairs of catalogued stars are closest together in magnitude. Find the minimum absolute difference between the magnitudes of any two stars in the catalog, then list every pair of stars whose magnitudes differ by exactly that minimum amount. Each pair should be listed as its smaller magnitude followed by its larger magnitude, and the pairs themselves should be listed in increasing order of their smaller magnitude.
Line 1: an integer n, the number of stars.
Line 2: n space-separated integers, the distinct magnitudes of the stars, in no particular order.
One line per qualifying pair, each containing the pair's smaller magnitude then its larger magnitude, separated by a single space, ordered by increasing smaller magnitude. Print nothing else.
n <= 100000n magnitudes are distinct.Example 1
Input
4 4 2 1 3
Expected
1 2 2 3 3 4
Explanation
Sorted, the magnitudes are 1,2,3,4; every consecutive gap is exactly 1, which is the smallest possible gap, so all three consecutive pairs -- (1,2), (2,3), (3,4) -- qualify and are listed in increasing order of the smaller value.
Example 2
Input
5 1 3 6 10 15
Expected
1 3
Explanation
Sorted, the gaps between consecutive magnitudes are 2, 3, 4, 5; the smallest of those is 2, achieved only by the pair (1,3), so that is the only line printed.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →