#C12646. Closest Duration Pair
Closest Duration Pair
Closest Duration Pair
Given a list of durations (in minutes) and a target total duration \(T\), your task is to find a pair of durations whose sum is closest to \(T\). If there are multiple pairs with the same closest sum, select the pair with the smallest absolute difference between the two durations. If there is still a tie, choose the pair with the smallest individual duration first. The resulting pair must be output in ascending order. If it is impossible to form a pair (i.e. the list contains fewer than 2 durations), output None
.
Formally, let the durations be denoted by \(d_1, d_2, \dots, d_n\) and the target duration by \(T\). For any pair \((d_i, d_j)\) with \(i \neq j\), define the sum difference as \(|(d_i + d_j) - T|\) and the pair difference as \(|d_i - d_j|\). The pair with the minimum sum difference should be chosen. In case of a tie, the pair with the minimum pair difference is selected. If a tie still exists, choose the pair with the smallest minimum duration.
inputFormat
The first line contains two integers: n
(the number of durations) and T
(the target duration).
The second line contains n
space-separated integers representing the durations.
outputFormat
If a valid pair exists, output the two durations in ascending order separated by a space. Otherwise, output None
.
5 75
45 30 60 70 15
30 45
</p>