Search⌘ K
AI Features

Solution: Two Sum III - Data structure design

Explore how to implement the TwoSum class using a hash map that tracks number frequencies to quickly determine if two numbers sum to a given value. Understand and apply the add and find methods to solve the problem efficiently in linear time using custom data structures.

Statement

Design a data structure that takes in a stream of numbers and can check if any two numbers add up to a specific value.

Implement the TwoSum class with the following constructor and methods:

  • Constructor: Sets up the TwoSum object with an empty list at the start.

  • void add(int number): Adds a new number to the list.

  • boolean find(int value): Returns TRUE if any two numbers in the list add up to the given value. If not, it returns FALSE.

Constraints:

  • 105-10^5 \leqnumber 105\leq 10^5 ...