package tsp.application;

import tsp.hashmap.HashMap;

public class SumTwo {

    public static int[] sumTwo(int[] tab, int target){
        // Construction de l'index inversé
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < tab.length; i++){
            hashMap.put(tab[i], i);
        }
        for (int i = 0; i < tab.length; i++) {
            int searchTerm = target - tab[i];
            Integer result = hashMap.get(searchTerm);
            if (result != null && result != i){
                return new int[]{tab[i], tab[result]};
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] tab = {6, 8, 2, 29, 31, 9, 4, 11, 15};
        int[] res = SumTwo.sumTwo(tab, 12);
        System.out.println(res[0] + " + " + res[1]);

        res = SumTwo.sumTwo(tab, 38);
        System.out.println(res[0] + " + " + res[1]);
    }
}
