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

30 thoughts on “LeetCode – Two Sum (Java)”

  1. Whats the advantage in saving the complement of the number, instead of the number itself ? Runtime would still be O(n).

  2. It will be highly appreciable if you can provide explanation of every algorithm too. I use C++ but not Java.

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

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

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

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

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

  8. 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;

    }

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

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

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

Leave a Comment