# LeetCode – Two Sum (Java)

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).

Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
1. Kevin on 2014-1-30

You can do a better solution with hashmap. Also, the input may contains negative number.

2. xmpy on 2014-2-16

Maybe something wrong in if(numbers[i] <= target) considering negative number~~

3. Tia on 2014-2-19

In Better Solution, You don’t need to check if index < i, index will always smaller than i.

4. ryanlr on 2014-2-21

True. Corrected.

5. ryanlr on 2014-2-21

True, corrected. Thanks!

6. JackyZhuang on 2014-5-13

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.

7. coder on 2014-6-24

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;
}
}

8. d0103 on 2014-7-1

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];
int i = 0;

while(true){
if(line == null) break;
data[i++] = Long.parseLong(line);
}

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;
}

9. Brave on 2014-8-20

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)

{

}

foreach (int i in numbers)

{

if (pairs.ContainsKey(pairs[i]))

{

}

}

return listOfSums;

}

10. YvDa on 2014-12-5

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.

11. ravi on 2015-3-17

We can use Binary search that would be better than this. It will take O(nlogn) time.

12. zcr on 2015-8-21

O(nlogn) is not better than given solution O(n).

13. Mubar on 2015-10-27

Your solution is giving an O(N^2) complexity. N(N-1)/2

14. suprchan on 2016-1-23

you can’t apply binary search because the array is not sorted. If you sort, you will end up losing the given order.

15. Vatsal Mahajan on 2016-2-2

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.

16. Darren Haken on 2016-2-2

I don’t understand how the HashMap works. Can someone try to explain it to more to help enlighten me?

17. Tom Dawn on 2016-2-2

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.

18. Darren Haken on 2016-2-3

That makes sense and is a brilliant optimisation. So it’s a bit like a reverse lookup in a way?

19. Tom on 2016-2-3

Exactly. This can save up lots of work instead of doing it in the normal way.

ttes comment

21. cegprakash on 2016-4-13

dworke

22. Jeff on 2016-7-17

The index are not 0 based, so it should be `map.put(target - numbers[i], i+1)`. Please see my solution

23. Trendisoft on 2017-2-21

I also think that there should a consideration for negative numbers.