Set<Integer> nums = new HashSet<>(); Set<Integer> sums = new HashSet<>();

/** Add the number to an internal data structure.. */ publicvoidadd(int number){ if (nums.contains(number)) { sums.add(number * 2); } else { for (int val : nums) { sums.add(number + val); } nums.add(number); } }

/** Find if there exists any pair of numbers which sum is equal to the value. */ publicbooleanfind(int value){ return sums.contains(value); }

Time:add would take $O(N)$ time each time. Space: Since we have sums set, it takes $O(N^2)$ space.

/** Add the number to an internal data structure.. */ publicvoidadd(int number){ map.put(number, map.getOrDefault(number, 0) + 1); }

/** Find if there exists any pair of numbers which sum is equal to the value. */ publicbooleanfind(int value){ for (int val1 : map.keySet()) { int val2 = value - val1; if (map.containsKey(val2)) { if (val1 == val2) { if (map.get(val1) >= 2) returntrue; } else { returntrue; } } } returnfalse; }