#K15051. Find Two Numbers Whose Sum is Closest to Zero
Find Two Numbers Whose Sum is Closest to Zero
Find Two Numbers Whose Sum is Closest to Zero
You are given an array of n integers. Your task is to find a pair of numbers from this array such that the sum of the pair is as close to zero as possible. In other words, if the chosen numbers are \(x\) and \(y\), then \(|x+y|\) should be minimized.
If there are multiple pairs with the same minimum absolute sum, the pair that appears first in the algorithm (using the two-pointer strategy on the sorted order) will be chosen.
Input: The first line contains an integer n, representing the number of elements in the array. The second line contains n space separated integers.
Output: Output two integers separated by a space, denoting the pair whose sum is closest to zero.
The approach typically involves sorting the array and using a two-pointer technique to efficiently find the required pair.
inputFormat
The input is given from standard input (stdin) and is structured as follows:
- The first line contains a single integer n (\(2 \le n \le 10^5\)), which represents the number of elements.
- The second line consists of n space-separated integers. Each integer \(a_i\) satisfies \(|a_i| \le 10^9\).
outputFormat
The output should be printed to standard output (stdout) as two integers separated by a space: the numbers that have the sum closest to zero.
## sample5
-1 2 3 -4 5
-4 5