How to solve solution with binary search
02:33 27 Jul 2026

Shepherd has n dogs and m sheep. The i-th dog is described by a number bi, and the j-th sheep is described by a number aj.

The shepherd wants to send some sheep out for a walk under the supervision of the dogs. A sheep can be sent out only together with two supervising dogs. If the j-th sheep is sent together with the x-th and y-th dogs, then the following inequalities must hold: bx < aj < by.

You must help the shepherd choose the maximum number of sheep that can be sent out at the same time.

Input format: The first line contains two integers m and n (1 ≤ m, n ≤ 10^5). The second line contains m integers a1, a2, …, am (0 ≤ ai ≤ 10^9) — the characteristics of the sheep. The third line contains n integers b1, b2, …, bn (0 ≤ bi ≤ 10^9) — the characteristics of the dogs.

Output format: On the first line, print an integer s — the maximum number of sheep that can be sent out at the same time. On each of the next s lines, print three integers: the index of the sheep j, and the indices of the two dogs x and y. For each printed triple, the inequalities must hold: bx < aj < by.

I started solving this solution and i wrote this code:

public class Main {
    public static HashMap order = new HashMap<>();
    public static HashMap> ans = new HashMap<>();

    public static void main(String[] args) throws IOException, InterruptedException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

        String[] p = bf.readLine().split(" ");
        int m = Integer.parseInt(p[0]);
        int n = Integer.parseInt(p[1]);

        p = bf.readLine().split(" ");

        long[] a = new long[m];
        for (int i = 0; i < m; i++)
            a[i] = Long.parseLong(p[i]);

        p = bf.readLine().split(" ");

        long[] b = new long[n];
        for (int i = 0; i < n; i++)
        {
            b[i] = Long.parseLong(p[i]);
            order.putIfAbsent(b[i], i);
        }

        Arrays.sort(a);
        Arrays.sort(b);

        System.out.println(binarySearch(a, b, n, m));
        for(var item: ans.entrySet()){
            var list = item.getValue();
            System.out.println(item.getKey() + " " + list.get(0) + " " + list.get(1));
        }
    }

    public static long binarySearch(long[] a, long[] b, int n, int m){
        int l = -1, r = Math.min(m, n / 2);
        while(r - l > 1){
            int mid = l + (r - l) / 2;
            if(good(a, b, n, m, mid)) l = mid;
            else r = mid;
        }

        return l;
    }

    public static boolean good(long[] a, long[] b, int n, int m, int mid){
        int pairs = 0;
        for (int i = 0; i < mid; i++) {
            if(b[i] < a[pairs] && a[pairs] < b[n - mid + i]){
                pairs++;
                int l = order.get(b[i]), r = order.get(b[n - mid + i]);
                ans.put(pairs, List.of(l + 1, r + 1));
            }
        }

        return pairs >= mid;
    }
}

But the tests failed. I think the problem is with how I was storing IDs for the answers.

java algorithm binary-search