package leetcode;

import java.util.Hashtable;

/**
 * Created by Yulin on 6/09/2017.
 */
public class TwoSum {
    public int[] twoSum(int[] nums, int target){
        Hashtable<Integer, Integer> map = new Hashtable<Integer, Integer>();

        for (int i = 0; i < nums.length; i++){
            map.put(nums[i], i);
        }

        for (int i = 0; i < nums.length; i++){
            int complement = target - nums[i];

            if (map.containsKey(complement) && map.get(complement) != i){
                return new int[] {i, map.get(complement)};
            }
        }

        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args){
        int[] input = new int[] {1, 2, 3};
        int target = 5;

        int[] result = new TwoSum().twoSum(input, target);

        for (int i : result){
            System.out.println(i);
        }
    }
}
