title: How to intersect two arrays of ranges?
body: <p>Let a <em>range</em> be an array of two integers: the <code>start</code> and the <code>end</code> (e.g. <code>[40, 42]</code>).</p>
  
  <p>Having two arrays of ranges (which are sorted), I want to find the optimal way to calculate their intersection (which will result into another array of ranges):</p>
  
  <pre><code>A = [[1, 3], [7, 9], [12, 18]]
  B = [[2, 3], [4,5], [6,8], [13, 14], [16, 17]]
  </code></pre>
  
  <p>Intersection:</p>
  
  <pre><code>[[2, 3], [7, 8], [13, 14], [16, 17]]
  </code></pre>
  
  <p>What is the optimal algorithm for this?</p>
  
  <hr>
  
  <p>The naive way would be to check each one with all the other ones, but that's obviously not optimal.</p>
  
  <p>I found a similar question asking for the same thing in VBA: <a href="https://stackoverflow.com/q/23683939/1420197">Intersection of two arrays of ranges</a></p>