## DEV Community 👩‍💻👨‍💻 is a community of 970,177 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Ilya Nevolin

Posted on

# Road to Genius: superior #54

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

``````function intersected(a, b) {
if (a[0] > b[1] || a[1] < b[0]) return false;
return true;
}

function mergeTwo(a, b) {
return [Math.min(a[0], b[0]), Math.max(a[1], b[1])];
}

function merge(VLS) {
VLS.sort((a, b) => a[0] - b[0]);
for (let i = 0; i < VLS.length - 1; i++) {
const cur = VLS[i];
const next = VLS[i + 1];
if (intersected(cur, next)) {
VLS[i] = undefined;
VLS[i + 1] = mergeTwo(cur, next);
}
}
return VLS.filter(q => q);
}

let arr = [
[2,10],
[10,11],
[11,12]
]

let A = merge(arr);
A = A[0][1]

// A = ? (number)
``````

We have encountered this challenge a couple of episodes ago (https://dev.to/codr/road-to-genius-superior-52-4b5m), this time we have to solve it and not just fix some bugs.

What we've learned from the previous post is that this code is designed to merge several input arrays depending on whether they overlap/intersect. In pseudo-code it's explained as such:

``````x = [1, 5]
y = [5, 9]
--> [1, 9] //they overlap so we can merge

x = [1, 5]
y = [6, 9]
--> they do not overlap so cannot merge
``````

In the code above we have the following:

``````let arr = [
[2,10],
[10,11],
[11,12]
]

* the first two arrays overlap
* the last two arrays overlap
--> all 3 arrays can be merged into:
[2, 12]
``````

The challenge asks us to solve `A[0][1]` which is 12.

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Get started and become a certified Codr today at https://nevolin.be/codr/