# NumberOfDiscIntersec tions https app codility com programmers lessons

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50``` ```""" NumberOfDiscIntersections https://app.codility.com/programmers/lessons/6-sorting/number_of_disc_intersections/ """ def solution(A): limit = 10000000 code_out_of_limit = -1 len_A = len(A) mid_opening_counts = [0 for _ in A] mid_closing_counts = [0 for _ in A] currently_seen_disc_count = 0 total_intersections = 0 for i, x in enumerate(A): if i - x >= 0: mid_opening_counts[i-x] += 1 else: total_intersections += currently_seen_disc_count currently_seen_disc_count += 1 if i + x < len_A: mid_closing_counts[i+x] += 1 for i, mid_opening_counts_i in enumerate(mid_opening_counts): total_intersections += currently_seen_disc_count * mid_opening_counts_i total_intersections += mid_opening_counts_i*(mid_opening_counts_i - 1)//2 currently_seen_disc_count += mid_opening_counts_i currently_seen_disc_count -= mid_closing_counts[i] if total_intersections > limit: return code_out_of_limit return total_intersections if __name__ == "__main__": for arr, expected in [ ([0,0,0], 0), ([1,2,3], 3), ([1,5,2,1,4,0], 11), ([3,2,1,3], 6) ]: solved = solution(arr) assert expected == solved, (expected, solved) ```