Solution to Two Numbers With Sum from Jobdu

8 Sep

Question (in Chinese):

Question Name: Two Numbers With Sum

Question Description: within a sorted integer array, find two integers, whose sum is the given target value. If there are multiply pairs, find the one with the smallest product.

Input: the input might contain multiple test cases. Inside each test case, the first line includes two integers N and N (1 <= N <= 10^6), saying the number of items in the array and the target number. The second line contains N integers as the content of the array.

Output: print the two numbers, with the smaller one firstly, if they exist. Otherwise, print “-1 -1”.

This challenge is a variant of the 2-sum problem. And the outtest pair holds the smallest product.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please put your code into a <pre>YOUR CODE</pre> section. Thanks and Happy Coding!