*This post is part of a series on Mohammad Anwar’s excellent Perl Weekly Challenge, where Perl and Raku hackers submit solutions to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)*

Task #1 this week asks us to merge integer ranges. Here is the task description:

*Write a script to merge the given intervals where ever possible.*

[2,7], [3,9], [10,12], [15,19], [18,22]

*The script should merge [2, 7] and [3, 9] together to return [2, 9].*

*Similarly it should also merge*

**[15, 19]**and**[18, 22]**together to return**[15, 22]**.*The final result should be something like below:*

[2, 9], [10, 12], [15, 22]

Of course, we could brute force it by comparing every interval with every other interval for an O(*n*²) solution. But there is a relatively straightforward algorithm to solve this in O(*n* log *n*) time.