{"id":389,"date":"2020-03-18T11:24:00","date_gmt":"2020-03-18T11:24:00","guid":{"rendered":"https:\/\/danwritescode.com\/?p=389"},"modified":"2020-06-25T12:06:58","modified_gmt":"2020-06-25T12:06:58","slug":"number-of-disc-intersections-codility-100-correct-javascript-solution","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/number-of-disc-intersections-codility-100-correct-javascript-solution\/","title":{"rendered":"Number Of Disc Intersections – Codility 100% Correct Javascript Solution"},"content":{"rendered":"\n

The Number of Disc Intersections is the hardest challenge so far, at least for me. I had to go seriously out of the box for this one, but in the end it proved to be a very fun solution.<\/p>\n\n\n\n

Description of the algorithm<\/h2>\n\n\n\n

It may be a bit hard to understand so I commented the code a lot. The trick here is to create two arrays containing the left boundaries of all circles (opening positions) in the first array, and the right boundaries (the closing positions) in the other one.<\/p>\n\n\n\n

The second step is to sort these two ascending.<\/p>\n\n\n\n

The idea here is that we will count the number of open circles to the left of each closing boundary. <\/p>\n\n\n\n

To do that, once we have them sorted ascending, we iterate over both of them with two counters: i <\/em>over the right array and j<\/em> over the left array. We use a for loop for the right array and an inner while loop to iterate over the left array under the conditions that :<\/p>\n\n\n\n