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.
For example, I have an array of min and max integer values [{min: 0, max: 33},{min: 33, max: 66},{min: 66, max: 100}]
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.
eg:
[{min: 0, max: 33},{min: 33, max: 66},{min: 66, max: 100}]
or [{min: 5, max: 50},{min: 51, max: 66},{min: 66, max: 100}]
should evaluate to true because there is overlapping ranges but only by 1.
[{min: 0, max: 34},{min: 33, max: 66},{min: 66, max: 100}]
or
[{min: 0, max: 33},{min: 32, max: 66},{min: 66, max: 100}] should evaluate tofalse because the ranges overlap by more than 1.
The following works to find the overlaps but doesn't allow for an overlap by 1.
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 >= ee.min && e.min <= ee.max) {
err = true;
}
if (e.max >= ee.min && e.max <= 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))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="overlapped" style="color: red;"></span>
<br>
<span id="notOverlapped" style="color: green;"></span>