Two sum closest to target

Write codeMedium20 min

Your task

Given an integer array a (possibly with negatives, length >= 2) and an integer target, pick two distinct elements whose sum is closest to target. Return that sum (the value of the closest sum, not the indices). If two sums are equally close, return either.

Examples

closest_pair_sum(a: list[int], target: int) -> int
Example 1
Inputclosest_pair_sum([1, 2, 3, 4, 5], 8)
Output8