package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 170. Two Sum III - Data structure design
 * <p>
 * Design and implement a _1 class. It should support the following operations: add and find.
 * <p>
 * add - Add the number to an internal data structure.
 * find - Find if there exists any pair of numbers which sum is equal to the value.
 * <p>
 * For example,
 * add(1); add(3); add(5);
 * find(4) -> true
 * find(7) -> false
 */

//Your _1 object will be instantiated and called as such:
//_1 twoSum = new _1();
//twoSum.add(number);
//twoSum.find(value);
public class _170 {
    private Map<Integer, Integer> map = new HashMap();
    private List<Integer> list = new ArrayList();

    // Add the number to an internal data structure.
    public void add(int number) {
        list.add(number);
        map.put(number, map.getOrDefault(number, 0) + 1);
    }

    // Find if there exists any pair of numbers which sum is equal to the value.
    public boolean find(int value) {
        for (int i = 0; i < list.size(); i++) {
            int val1 = list.get(i);
            int val2 = value - val1;
            if (map.containsKey(val2)) {
                if (val1 == val2) {
                    if (map.get(val2) > 1) {
                        return true;
                    }
                } else {
                    return true;
                }
            }
        }
        return false;
    }
}

@@ -129,17 +129,16 @@
 l.Map;%0A%0A
-%0A
 /**%0A * 1
@@ -179,20 +179,16 @@
 esign%0A *
- %3Cp%3E
 %0A * Desi
@@ -206,18 +206,22 @@
 ement a 
-_1
+TwoSum
  class. 
@@ -276,28 +276,24 @@
 and find.%0A *
- %3Cp%3E
 %0A * add - Ad
@@ -426,28 +426,22 @@
 .%0A *
- %3Cp%3E%0A * For e
+%0A * E
 xample
-,
+ 1:
 %0A * 
@@ -509,154 +509,145 @@
 e%0A *
-/%0A%0A//Your _1 object will be instantiated and called as such:%0A//_1 twoSum = new _1();%0A//twoSum.add(number);%0A//twoSum.find(value);%0Apublic class _170
+%0A * Example 2:%0A * add(3); add(1); add(2);%0A * find(3) -%3E true%0A * find(6) -%3E false%0A */%0A%0Apublic class _170 %7B%0A  public static class Solution1
  %7B%0A 
@@ -844,18 +844,16 @@
 %7B%0A      
-  
 list.add
@@ -862,18 +862,16 @@
 umber);%0A
-  
       ma
@@ -1049,18 +1049,16 @@
 %7B%0A      
-  
 for (int
@@ -1093,28 +1093,24 @@
 ) %7B%0A        
-    
 int val1 = l
@@ -1121,20 +1121,16 @@
 get(i);%0A
-    
         
@@ -1154,36 +1154,32 @@
 - val1;%0A        
-    
 if (map.contains
@@ -1187,30 +1187,24 @@
 ey(val2)) %7B%0A
-      
           if
@@ -1233,24 +1233,16 @@
         
-        
 if (map.
@@ -1268,34 +1268,24 @@
             
-          
 return true;
@@ -1293,37 +1293,26 @@
             
- 
-       %7D%0A   
+%7D%0A
             
@@ -1309,19 +1309,16 @@
         
-   
 %7D else %7B
@@ -1326,32 +1326,24 @@
             
-       
- 
 return true;
@@ -1357,27 +1357,18 @@
     
-      %7D%0A   
+%7D%0A
         
  %7D%0A 
@@ -1367,21 +1367,18 @@
     
- 
 %7D%0A
-  
       %7D%0A
     
@@ -1369,26 +1369,24 @@
   %7D%0A      %7D%0A
-  
       return
@@ -1399,10 +1399,14 @@
 ;%0A    %7D%0A
+  %7D%0A
 %7D%0A

refactor 170