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

Use HashMap to store the target value.

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
	HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
	int[] result = new int[2];
 
	for (int i = 0; i < numbers.length; i++) {
		if (map.containsKey(numbers[i])) {
			int index = map.get(numbers[i]);
			result[0] = index ;
			result[1] = i;
			break;
		} else {
			map.put(target - numbers[i], i);
		}
	}
 
	return result;
    }
}

Time complexity depends on the put and get operations of HashMap which is normally O(1).

Time complexity of this solution 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>
  • 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[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;
    }

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