As I commented, this is very similar to run length encoding, in fact if
used is sorted it is arguably a bit easier!
With run length encoding you would start with the first number in an array and scan forward counting the number between each transition between used/unused (or in your case recording the start and end index which is essentially equivalent).
However if used is sorted you already know where the first transition from unused->used is so you can start there and scan
used for the next transition from used->unused, you then also know the next unused->used transition as its the next
used isn't sorted you could either sort it first (likely a good idea if used is small compared to
range) or you could use
used to first construct a full array of used and unused numbers e.g.
00011100010 and run length encode this.