<commit_before>package lesson4;

import java.util.Comparator;
import java.util.TreeSet;

public class NumberOfDiscIntersections {

    public static void main(String[] args) {
        Solution numberOfDiscIntersections = new Solution();

        int[] input = {1, 5, 2, 1, 4, 0};
        int solution = numberOfDiscIntersections.solution(input);

        System.out.println(solution);
    }

    static class Solution {

        public int solution(int[] discs) {
            final TreeSet<Pair> solutions = new TreeSet<Pair>(new Comparator<Pair>() {
                @Override
                public int compare(Pair left, Pair right) {
                    final int lX = left.x;
                    final int lY = left.y;

                    final int rX = right.x;
                    final int rY = right.y;

                    final int deltaX = lX - rX;
                    if (deltaX == 0) {
                        return lY - rY;
                    } else {
                        return deltaX;
                    }
                }
            });

            final int nbDiscs = discs.length;
            for (int i = 0; i < nbDiscs; i++) {
                final int discRadius = discs[i];

                for (int k = i - discRadius; k <= i + discRadius; k++) {
                    if (k >= 0 && k < nbDiscs && k != i) {
                        solutions.add(new Pair(i, k));

                        if (solutions.size() > 10000000) {
                            return -1;
                        }
                    }
                }
            }

            return solutions.size();
        }

        private static final class Pair {

            private final int x;
            private final int y;

            private Pair(int x, int y) {
                if (x <= y) {
                    this.x = x;
                    this.y = y;
                } else {
                    this.x = y;
                    this.y = x;
                }
            }

        }

    }

}<commit_msg>Improved correctness.<commit_after>package lesson4;

import java.util.Arrays;
import java.util.Comparator;

public class NumberOfDiscIntersections {

    public static void main(String[] args) {
        Solution numberOfDiscIntersections = new Solution();

        int[] input = {1, 5, 2, 1, 4, 0};
        int solution = numberOfDiscIntersections.solution(input);

        System.out.println(solution);
    }

    static class Solution {

        public int solution(int[] discs) {
            final int nbDiscs = discs.length;
            final Interval[] intervals = new Interval[nbDiscs];

            for (int i = 0; i < nbDiscs; i++) {
                final int left = i - discs[i];
                final int right = i + discs[i];
                intervals[i] = new Interval(left, right);
            }

            Arrays.sort(intervals, new IntervalComparator());

            int totalNbIntersections = 0;
            for (int i = 0; i < nbDiscs; i++) {
                final Interval interval = intervals[i];
                final int targetRight = interval.right;

                int nbIntersections = 0;
                int k = i + 1;
                boolean intersecting = true;
                while (intersecting && k < nbDiscs) {
                    Interval sourceInterval = intervals[k];
                    if (sourceInterval.left > targetRight) {
                        intersecting = false;
                    } else {
                        k++;
                        nbIntersections++;
                    }
                }

                totalNbIntersections += nbIntersections;
            }

            return totalNbIntersections;
        }

        private static final class Interval {

            private final int left;
            private final int right;

            private Interval(int left, int right) {
                if (left <= right) {
                    this.left = left;
                    this.right = right;
                } else {
                    this.left = right;
                    this.right = left;
                }
            }

        }

        private static final class IntervalComparator implements Comparator<Interval> {

            @Override
            public int compare(Interval cLeft, Interval cRight) {
                final int deltaLeft = cLeft.left - cRight.left;
                if (deltaLeft == 0) {
                    return cLeft.right - cRight.right;
                } else {
                    return deltaLeft;
                }
            }

        }

    }

}
{'fraction_non_alphanumeric': 0.08700392498909726, 'fraction_numerical': 0.006977758395115569, 'mean_word_length': 1.1177285318559558, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7315863', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1108, 'n_words': 327}