title: Which algorithm for finding empty ranges of numbers in a range of numbers?
body: <p>I'm doing some file analysis, where I mark explored regions in the file.  Now I would like to find the <em>un</em>explored regions, so I know what to look at next. This is much like what defragmentation software shows for free and used regions.</p>
  
  <p><strong>Example:</strong></p>
  
  <p>In this picture, let's say that explored regions are red, unexplored regions are gray. I need to determine the gray-region boundaries from these red regions.</p>
  
  <p><a href="https://i.stack.imgur.com/mSRGF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mSRGF.png" alt="enter image description here"></a></p>
  
  <p><strong>My current code, a custom binary reader that logs what's been read:</strong></p>
  
  <pre><code>public class CustomBinaryReader : BinaryReader {
  
      private readonly List&lt;Block&gt; _blocks;
  
      public CustomBinaryReader([NotNull] Stream input) : this(input, Encoding.Default) { }
  
      public CustomBinaryReader(Stream input, Encoding encoding, bool leaveOpen = true) : base(input, encoding, leaveOpen) {
          _blocks = new List&lt;Block&gt;();
      }
  
      public override byte[] ReadBytes(int count) {
          Log(count);
          return base.ReadBytes(count);
      }
  
      private void Log(int count) {
          _blocks.Add(new Block(BaseStream.Position, count));
      }
  
      private IEnumerable&lt;Block&gt; GetUnreadBlocks() {
          // how to get unread blocks in the stream, from read blocks ?
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>And the type that defines what a region is:</p>
  
  <pre><code>public class Block {
      public Block(long position, long length) {
          Position = position;
          Length = length;
      }
  
      public long Position { get; }
      public long Length { get; }
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>Is there a class of algorithms or data structures to solve such problem (like a tree or a graph) ?  If such thing does not exist, can you give me some approach or tips on how to solve such problem?</p>