Problem statement

https://binarysearch.com/problems/Sum-of-Two-Numbers-Online-Version/

Solution

Equal to Leetcode 0170 Two Sum III - Data structure design.

Complexity

Time complexity for add is O(1), for find is O(n). Space complexity of total data structure is O(n).

Code

class TwoSum:
    def __init__(self):
        self.cnt = Counter()
        
    def add(self, number):
        self.cnt[number] += 1
        
    def find(self, value):
        for num in self.cnt:
            if num*2 == value:
                if self.cnt[num] >= 2: return True
            else:
                if value - num in self.cnt: return True
           
        return False