SalesforceOA #34
Minimum Difference
Estimated EasyJudge-ready
Topics (AI-labelled)In-place Arrays
The array elements are distinct. Output all value pairs whose absolute difference is minimal. In each pair the smaller value comes first, and pairs are sorted ascending by the first item and then by the second. The output of the original example [-1,3,6,-5,0] is wrong: the minimum difference is 1, and the only correct value pair is −1 and 0, not the two pairs with difference 3.
Input
The input gives n and n distinct integers. The original has no numeric bounds; this site adds 2≤n≤100000, −10^9≤a[i]≤10^9.
Output
Output one pair of values with the minimum absolute difference per line, covering all optimal pairs; do not output the number of pairs or the difference.
Example 1
Input:
5
-1 3 6 -5 0
Output:
-1 0
Example 2
Input:
4
4 2 1 3
Output:
1 2
2 3
3 4
Example 3
Input:
2
-7 9
Output:
-7 9
Example notes
The outputs of the three public examples are, in order: -1 0; 1 2 / 2 3 / 3 4; -7 9. They have been checked by independent enumeration or direct simulation.
Editorial
Loading solution…