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 index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
For example:
Input: numbers={2, 7, 11, 15}, target=9 Output: index1=0, index2=1
Java Solution
The optimal solution to solve this problem is using a HashMap. For each element of the array, (target-nums[i]) and the index are stored in the HashMap.
public int[] twoSum(int[] nums, int target) { if(nums==null || nums.length<2) return new int[]{0,0}; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i=0; i<nums.length; i++){ if(map.containsKey(nums[i])){ return new int[]{map.get(nums[i]), i}; }else{ map.put(target-nums[i], i); } } return new int[]{0,0}; } |
Time complexity is O(n).
This works for all test cases on leetcode, but is slower than 57% of other submissions on leet code. For a solution that is 99% faster see here: https://medium.com/@7anac/cs-interview-prep-leetcode-two-sum-pair-sum-f254e20bc04e
Wheres your solution?
HashMap will have a high probability to run in O(n) for n operations assuming that nothing bad happens with the hash function.
Whats the advantage in saving the complement of the number, instead of the number itself ? Runtime would still be O(n).
This solution fails with this testcase [2,11,15,-9,6,5]
9
The above code fails for duplicate elements as Hashmap is used.
It will be highly appreciable if you can provide explanation of every algorithm too. I use C++ but not Java.
I also think that there should a consideration for negative numbers.
The index are not 0 based, so it should be
map.put(target - numbers[i], i+1)
. Please see my solutiondworke
ttes comment
Exactly. This can save up lots of work instead of doing it in the normal way.
That makes sense and is a brilliant optimisation. So it’s a bit like a reverse lookup in a way?
The solution here is a bit tricky. Consider an array [4,1,6] where target equals 10. What it does is put complementary number of numbers[i] into the hashmap together with index value i. Here, 10-4=6, so it is key 6 value 0. That 6 is really what we are looking for to add up to 10, since we already have a 4. Later on if it finds 6, it will simply return the index of the previous complementary number and index of 6, which is 0+1 and 2+1.
I don’t understand how the HashMap works. Can someone try to explain it to more to help enlighten me?
The O(n) is average case complexity using HashMap (whereas worst case for hashmaps is O(n^2) OR O(nlogn) in a better implementation using red-black Trees). Hence O(n) is expected performance not guaranteed. But the binary search approach has worst case performance O(nlogn), so this is a better approach.
you can’t apply binary search because the array is not sorted. If you sort, you will end up losing the given order.
Your solution is giving an O(N^2) complexity. N(N-1)/2
O(nlogn) is not better than given solution O(n).
We can use Binary search that would be better than this. It will take O(nlogn) time.
Assuming the numbers are sorted, there is a simple O(N) solution without a hash table:
– take the first number and scan the list from the tail as long as the sum exceeds or equals the target (O(N) comparisons at worse).
– take the next number and continue the backward scan as long as the sum exceeds or equals the target (O(N) comparisons at worse).
The O(N) behavior is guaranteed by the fact that on every iteration the “distance” between the indexes decreases. Correctness is ensured by monotonicity.
Here is the Log n(logn) solution
public static List<KeyValuePair> TwoSum(int[] numbers, int target)
{
Dictionary pairs = new Dictionary();
List<KeyValuePair> listOfSums = new List<KeyValuePair>();
foreach (int number in numbers)
{
pairs.Add(number, target – number);
}
foreach (int i in numbers)
{
if (pairs.ContainsKey(pairs[i]))
{
listOfSums.Add(new KeyValuePair(i,pairs[i]));
}
}
return listOfSums;
}
but why does it take more time than O(nlogn) solution :
code using sort ::
public static void findTwoSum(int[] A, int x) {
Arrays.sort(A);
int start = 0;
int end = A.length – 1;
boolean found = false;
while (!found && start x)
end–;
else if (A[start] + A[end] < x)
start++;
}
if (found)
System.out.println("Sum " + x
+ " is found, values the making sum are " + A[start] + " , "
+ A[end]);
else
System.out.println("No pair exists whose sum is " + x);
}
code using hashmap:
import java.io.*;
import java.util.*;
class TwoSumProblem{
private static final int MIN_T = -10000;
private static final int MAX_T = 10000;
private static final String filename = “algo1-programming_prob-2sum.txtâ€;
public static void main(String args[]) throws IOException{
long[] data = new long[1000000];
BufferedReader rd = new BufferedReader(new FileReader(filename));
int i = 0;
while(true){
String line = rd.readLine();
if(line == null) break;
data[i++] = Long.parseLong(line);
}
System.out.println(“data successfully readâ€);
map = new HashMap();
for(int j = 0;j<data.length;j++){
Integer v = map.get(data[j]);
if(v==null){
v = 1;
}else{
v++;
}
map.put(data[j], v);
}
for(int t = MIN_T;t<=MAX_T;t++){
System.out.println(t);
findPair(t,data);
}
System.out.println(count);
}
private static void findPair(int t, long[] data){
for(int i=0;i=2) || map.containsKey(diff)){
count++;
break;
}
}
}
private static HashMap map;
private static int count;
}
public class TwoSum {
public static void main(String… argc) {
System.out.println(Arrays.toString(findPair(9,4,6,8,1,0,1)));
System.out.println(Arrays.toString(findPair(10,4,6,8,1,0,1)));
System.out.println(Arrays.toString(findPair(14,4,6,8,1,0,1)));
System.out.println(Arrays.toString(findPair(12,4,6,8,1,0,1)));
}
private static int[] findPair(int target, int… values) {
System.out.println(target);
System.out.println(Arrays.toString(values));
int start = 0, end = 0 ;
int[] result = new int[values.length];
for(int i=0, j = 1;j <values.length && i<j ;i++, j++) {
if(values[i] + values[j] == target) {
start = i;
end = j;
}
}
for(int i= start, j= 0; i<=end; i++, j++) {
result[j] = values[i];
}
return result;
}
}
Something wrong may happened in a better solution. you need to judge which one of two number is bigger then put them in the right order.
True, corrected. Thanks!
True. Corrected.
In Better Solution, You don’t need to check if index < i, index will always smaller than i.
Maybe something wrong in if(numbers[i] <= target) considering negative number~~
You can do a better solution with hashmap. Also, the input may contains negative number.