import java.util.*; public class Solution { public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) { if(array.length == 0){ return new ArrayList<Integer>(); } ArrayList<Integer> list = new ArrayList<Integer>(); ArrayList<ArrayList<Integer>> listList = new ArrayList<>(); for(int i = 0; i < array.length; i++){ list.add(array[i]); ArrayList<Integer> res = new ArrayList<Integer>(); if(list.contains(sum-array[i])){ res.add(array[i]); res.add(sum - array[i]); res.add(array[i] * (sum - array[i])); listList.add(res); } } if(listList.size() == 0){ return new ArrayList<Integer>(); } Collections.sort(listList, new Comparator<ArrayList<Integer>>() { @Override public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) { return o1.get(2).compareTo(o2.get(2)); } }); ArrayList<Integer> result = new ArrayList<Integer>(); ArrayList<Integer> temp = listList.get(0); for(int i = 0;i<2;i++){ result.add(temp.get(i)); } Collections.sort(result); return result; } }