How to efficiently get the Complement of a Domain and several Intervals (IntervalComplement?)
int = {{3, 7}, {17, 43}, {64, 70}};
com = Partition[#, 2]& @ {1, Sequence @@ Riffle[int[[All, 1]] - 1, int[[All, 2]] + 1], 100}
{{1, 2}, {8, 16}, {44, 63}, {71, 100}}
NumberLinePlot[{Interval @@ int, Interval @@ com}, PlotTheme -> "Detailed"]
f1 = Partition[Flatten[{0, Select[Flatten@#, Function[x, x < #2]], #2 + 1}],
2, 2, {1, -1}, {}, ({1, -1} + {##}) /. {1, 0} -> Nothing &]&;
f1[{{3, 7}, {17, 43}, {64, 70}}, 80]
{{1, 2}, {8, 16}, {44, 63}, {71, 80}}
Also
f2 = BlockMap[({1, -1} + #) /. {1, 0} -> Nothing &,
Flatten[{0, Select[Flatten@#, Function[x, x < #2]], #2 + 1}], 2] &;
f2[{{3, 7}, {17, 43}, {64, 70}}, 80]
{{1, 2}, {8, 16}, {44, 63}, {71, 80}}
Note: In versions before 10.2 you can use Developer`PartitionMap
in place of BlockMap
above. (thanks: @CarlWoll)