public static ArrayList<ArrayList<Integer>> twoSum2(int[] a, int sum) {
        if (a == null) return null;
        Arrays.sort(a);
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

        int left = 0, right = a.length - 1;
        while (left < right) {
            if (a[left] + a[right] == sum) {
                ArrayList<Integer> pair = new ArrayList<Integer>();
                pair.add(a[left]);
                pair.add(a[right]);
                result.add(pair);
                ++left;
                --right;
            } else if (a[left] + a[right] < sum) {
                ++left;
            } else {
                --right;
            }
        }
        return result;
    }