@@ -0,0 +1,1827 @@
+package com.freetymekiyan.algorithms.level.easy;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A/**%0A * Design and implement a TwoSum class. It should support the following operations: add and find.%0A * %3Cp%3E%0A * add - Add the number to an internal data structure.%0A * find - Find if there exists any pair of numbers which sum is equal to the value.%0A * %3Cp%3E%0A * For example,%0A * add(1); add(3); add(5);%0A * find(4) -%3E true%0A * find(7) -%3E false%0A * %3Cp%3E%0A * Company Tags: LinkedIn%0A * Tags: Hash Table, Design%0A * Similar Problems: (E) Two Sum, (E) Unique Word Abbreviation%0A */%0Apublic class TwoSum3DataStructureDesign %7B%0A%0A    public class TwoSum %7B%0A%0A        List%3CInteger%3E nums = new ArrayList%3C%3E();%0A        Map%3CInteger, Integer%3E count = new HashMap%3C%3E(); // Map iterator is slow.%0A%0A        // Add the number to an internal data structure.%0A        public void add(int number) %7B%0A            if (!count.containsKey(number)) %7B%0A                count.put(number, 0);%0A                nums.add(number);%0A            %7D%0A            count.put(number, count.get(number) + 1);%0A        %7D%0A%0A        // Find if there exists any pair of numbers which sum is equal to the value.%0A        public boolean find(int value) %7B%0A            for (int num1 : nums) %7B%0A                int num2 = value - num1;%0A                if (!count.containsKey(num2)) %7B%0A                    continue;%0A                %7D%0A                if (num1 != num2 && count.get(num2) %3E 0) %7B%0A                    return true;%0A                %7D%0A                if (num1 == num2 && count.get(num2) %3E= 2) %7B%0A                    return true;%0A                %7D%0A            %7D%0A            return false;%0A        %7D%0A    %7D%0A%0A// Your TwoSum object will be instantiated and called as such:%0A// TwoSum twoSum = new TwoSum();%0A// twoSum.add(number);%0A// twoSum.find(value);%0A%7D%0A

add two sum 3