package pl.polgrabia.demos.crackingcodeinterview.demos.t16x24;

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

public class Solver {
    public static List<Solution> resolve(int[] nrs, int sum) {
        List<Solution> solutions = new ArrayList<>();
        Map<Integer, Integer> s = new HashMap<>();
        for (var nr : nrs) {
            var c = s.computeIfAbsent(nr, k -> 0);
            s.put(nr, c + 1);
        }

        for (var nr: s.keySet()) {
            var el = sum - nr;
            if (nr > el) {
                continue;
            }
            int existing_count = s.getOrDefault(nr, 0);
            int required_count = el == nr ? 2 : 1;
            if (required_count > existing_count) {
                continue;
            }
            solutions.add(new Solution(nr, el));
        }
        return solutions;
    }
}
