package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
 */
public class Problem167 {
    public int[] twoSum(int[] numbers, int target) {
        int[] array = new int[2];
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            if (!map.containsKey(numbers[i])) {
                List<Integer> list = new ArrayList<>();
                list.add(i + 1);
                map.put(numbers[i], list);
            } else {
                map.get(numbers[i]).add(i + 1);
            }
        }
        for (int i = 0; i < numbers.length; i++) {
            int a = target - numbers[i];
            if (map.containsKey(a)) {
                for (int idx : map.get(a)) {
                    array[0] = Math.min(i + 1, idx);
                    array[1] = Math.max(i + 1, idx);
                    if (array[0] != array[1]) {
                        return array;
                    }
                }
            }
        }
        return array;
    }

    public static void main(String[] args) {
        Problem167 prob = new Problem167();
        System.out.println(Arrays.toString(prob.twoSum(new int[]{2, 7, 11, 15}, 9)));
        System.out.println(Arrays.toString(prob.twoSum(new int[]{0, 0, 3, 4}, 0)));
    }
}

@@ -44,33 +44,8 @@
 st;%0A
-import java.util.Arrays;%0A
 impo
@@ -1145,273 +1145,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem167 prob = new Problem167();%0A        System.out.println(Arrays.toString(prob.twoSum(new int%5B%5D%7B2, 7, 11, 15%7D, 9)));%0A        System.out.println(Arrays.toString(prob.twoSum(new int%5B%5D%7B0, 0, 3, 4%7D, 0)));%0A    %7D
 %0A%7D%0A

Update problem 167