title: Integers range overlap algorithm
body: <p>I have have a logic problem I cannot seem to wrap my head around. 
  I'm working with sets of discount ranges that a user can change add or remove which I need to validate for overlapping values. </p>
  
  <p>For example, I have an array of min and max integer values [{min: 0, max: 33},{min: 33, max: 66},{min: 66, max: 100}]</p>
  
  <p>I need to determine if any of the given ranges overlap with each other, but the ranges are allowed to overlap by 1 only. The amount of ranges is also variable so there might be any number of sets per array.</p>
  
  <p>eg:<br>
  <code>[{min: 0, max: 33},{min: 33, max: 66},{min: 66, max: 100}]</code> 
  or <code>[{min: 5, max: 50},{min: 51, max: 66},{min: 66, max: 100}]</code>
   should evaluate to <code>true</code> because there is overlapping ranges but only by 1.</p>
  
  <p><code>[{min: 0, max: 34},{min: 33, max: 66},{min: 66, max: 100}]</code> 
  or 
  <code>[{min: 0, max: 33},{min: 32, max: 66},{min: 66, max: 100}]</code> should evaluate to<code>false</code> because the ranges overlap by more than 1.</p>
  
  <p>The following works to find the overlaps but doesn't allow for an overlap by 1. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var overlappedRanges = [{
    min: 0,
    max: 33
  }, {
    min: 33,
    max: 66
  }, {
    min: 66,
    max: 100
  }];
  var nonOverlappedRanges = [{
    min: 0,
    max: 32
  }, {
    min: 33,
    max: 66
  }, {
    min: 67,
    max: 100
  }];
  
  var checkForOverlap = function(arr) {
    var err = false;
    arr.forEach(function(e, i) {
      arr.forEach(function(ee, ii) {
        if (ee !== e) {
          if (e.min &gt;= ee.min &amp;&amp; e.min &lt;= ee.max) {
            err = true;
          }
  
          if (e.max &gt;= ee.min &amp;&amp; e.max &lt;= ee.max) {
            err = true;
          }
        }
      });
    });
  
    if (err) {
      return 'Overlap Found in ' + JSON.stringify(arr);
    } else {
      return 'NO Overlap Found in ' + JSON.stringify(arr);
    }
  }
  
  $('#overlapped').text(checkForOverlap(overlappedRanges));
  $('#notOverlapped').text(checkForOverlap(nonOverlappedRanges))</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;span id="overlapped" style="color: red;"&gt;&lt;/span&gt;
  &lt;br&gt;
  &lt;span id="notOverlapped" style="color: green;"&gt;&lt;/span&gt;</code></pre>
  </div>
  </div>
  </p>