31 August 2026 ยท Advanced track
Closest Crate Match
Find the pair of shipping crates that adds up exactly to a target volume, preferring the most evenly matched pair.
You are given a list of container volumes at a shipping depot, sorted from smallest to largest, along with a target combined volume a customer has ordered. Find two different containers whose volumes add up exactly to the target. More than one pair can add up to the target. When that happens, choose the pair whose two volumes are closest to each other in size โ the depot prefers to keep the more balanced load. Return the matching pair as a tuple `(smaller, larger)`. If no two containers add up to the target, return `None`.
Constraints
volumes has at least 2 entriesAll volumes are distinct positive integersvolumes is already sorted in increasing ordertarget is a positive integerMore than one pair may sum to target; return the pair with the smallest difference between its two volumesIf no pair sums to target, return None
Function signature
def closest_matched_crates(volumes, target):