public static ArrayList<ArrayList<Integer>> twoSum(int[] a, int sum) {
        if (a == null) return null;
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();

        for (int i = 0; i < a.length; ++i) {
            int comp = sum - a[i];
            if (map.containsKey(comp)) {
                for (int compIndex : map.get(comp)) {
                    ArrayList<Integer> pair = new ArrayList<Integer>();
                    pair.add(compIndex);
                    pair.add(i);
                    result.add(pair);
                }
            }
            ArrayList<Integer> compIndexList = new ArrayList<Integer>();
            if (map.containsKey(a[i])) compIndexList = map.get(a[i]);
            compIndexList.add(i);
            map.put(a[i], compIndexList);
        }
        return result;
    }