Editorial for DMPG '17 S1 - Molly and Difference
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For the subtask, it is possible to iterate over all pairs of numbers and compare the absolute value of their differences, and print the minimum.
Time Complexity:
Subtask 2
For the subtask, a greedy approach can be used. Sort all the elements, and iterate through each pair of adjacent numbers in the sorted array, and print the minimum absolute value of their difference.
Proof of greedy algorithm:
Let the array be the sorted array. The minimum difference between and any number is . For the sake of contradiction, assume there exists some , , such that Thus we have two cases:
Case 1:
This implies that .
is sorted, .
, for .
Substituting, this gives , which gives:
, this is a contradiction.
Case 2:
This implies that .
is sorted, .
, for .
Substituting, this gives , which gives:
, this is a contradiction.
we have proven that does not exist.
Time Complexity:
Comments