# Codility: NumberOfDiscIntersections Solution

Lesson 6 Sorting

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}function solution(A) {2  let i = 03
4  // 1 means beginning, -1 mean ending5  const points = A.map((a, i) => [6    [i - a, 1],7    [i + a, -1],8  ])9    .reduce((acc, el) => {10      acc[i++] = el[0]11      acc[i++] = el[1]12      return acc13    }, [])14    .sort((a, b) => a[0] - b[0] || b[1] - a[1])15
16  let intersections = (activeCircles = 0)17  for (const [_, place] of points) {18    if (place === 1) {19      intersections += activeCircles20      activeCircles += 121    } else {22      activeCircles -= 123    }24    if (intersections > 1e7) {25      return -126    }27  }28
29  return intersections30}```

## References

http://www.lucainvernizzi.net/blog/2014/11/21/codility-beta-challenge-number-of-disc-intersections/

codility

## Next Post

Codility: MaxProductOfThree

Jan 22, 2021

Lesson 6 Sorting

## Previous Post

Codility: Triangle

Jan 22, 2021

Lesson 6 Sorting

Search Posts