/*对于一个递增的序列,存在2个数字的和相等,要想这2个数字的乘积最小,则这2个数字的距离最远*/
/*思想:j指向最后一个元素,然后i从前扫描看sum-a[j]在这个序列中吗?若不在j--*/
import java.util.ArrayList; public class Solution { static boolean binSearch(int a[],int key){ int low=0,high=a.length-1; while(low<=high){ int mid = (low+high)/2; if(a[mid] == key) return true; else if (a[mid]<key) low = mid+1; else high = mid-1; } return false; } public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) { ArrayList<Integer> list = new ArrayList<Integer>(); int flag=-1;int cheng ; int leizhu = 999999999; for(int i=0;i<array.length;i++){ if(binSearch(array,sum-array[i]) && array[i]!=sum-array[i])
{
cheng = array[i]*(sum-array[i]); if(cheng<leizhu) leizhu= cheng; flag=i;} } if(leizhu==999999999) return list; list.add(sum-array[flag]); list.add(array[flag]); return list; } }