Sum Algorithms

Two Sum III Data structure design


Design and implement a TwoSum class.  It should support the following operations:
add and find.
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.


For example,
add(1);
add(3);
add(5);
find(4) -> true
find(7) -> false


 Java Solution
Since the desired class need add and get operations,  HashMap is a good option for this purpose

 

Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index 1 must be less than index 2 . Please note that your returned
answers (both index 1 and index 2) are not zero-based.


For example:
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=0, index2

 

Add a comment