# 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>
```
• Durga Pradeep

This solution fails with this testcase [2,11,15,-9,6,5]
9

• Apoorva

The above code fails for duplicate elements as Hashmap is used.

• Hitesh

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.

• Jeff

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

• cegprakash

dworke

• RadhaKrishnan Venkataramani

ttes comment

• Tom

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

• Darren Haken

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

• Tom Dawn

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.

• Darren Haken

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

• Vatsal Mahajan

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.

• suprchan

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

• Mubar

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

• zcr

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

• ravi

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

• YvDa

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.

• Brave

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;

}

• d0103

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

• coder

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

• JackyZhuang

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.

• ryanlr

True, corrected. Thanks!

• ryanlr

True. Corrected.

• Tia

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

• xmpy

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

• Kevin

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