I have two sets of ranges represented by [start, stop] values. Some ranges overlap, which means that the start of one range is between [start, stop] of another range. I would like to create a new set of ranges that does not have such an overlap and also does not contain any new values in the range.
Ranges are as follows:
@starts @ends
5 108
5 187
44 187
44 229
44 236
64 236
104 236
580 644
632 770
The result that I expect will be as follows:
@starts @ends
5 236
580 770
This is due to the fact that the first seven ranges overlap with an interval of 5 => 236, and the last two overlap with an interval of 632 => 770.
Here is the code I tried:
$fix = 0;
foreach (@ends) {
if ($starts[$fix + 1] < $ends[$fix]) {
splice(@ends, $fix, $fix);
splice(@starts, $fix + 1, $fix + 1);
} else {
$fix += 1;
}
}
I can print the values myself, I just need help with the merge algorithm.