i have tried this problem for quite some time but cannot find a good solution :
suppose we have ranges of integers like
100 - 1000
200 - 300
500 - 600
550 - 575
800 - 1200
1100 - 2300
so i want to get these intermixing ranges into each separate distinct range
100 - 199
200 - 300
301 - 499
500 - 549
550 - 575
575 - 600
800 - 1099
1100 - 1200
1201 - 2300
is there some standard algorithm for this ?
thanks
Shove every number into a single array, sort and dedupe. In C++, inserting them all into a
std::set<int>has the desired effect. In Python, you can writesorted(set(x for (a, b) in ranges for x in (a, b))).In the resulting collection, adjacent pairs represent non-overlapping ranges.
It’s important to note that this technique relies on the [a, b) convention (i.e., inclusive on the left; exclusive on the right) for describing ranges.