Check overlapping series in C#
I am making an application where the user adds start and end to define a range
The condition is that the range should not overlap:
How to check wh开发者_如何转开发ether a number range is not overlapping e.g.
- Range 1 Start 5 End 15
- Range 2 Start 1 End 4
- Range 3 Start 16 End 20
- Range 4 Start 2 End 4
So the Range 4 makes the set invalid, how do I check this in C#.
Further the user can add the range in any order as in the example above, the entire series should be non overlapping.
Thanks for the help suggestion.
Regards, Sakshi
Answer:
I made the solution is it correct: If start and end is the range which needs to be validated then
- start >startRange and start less than endRange
- end>startRange and end less than endRange
The above 2 condition validates that the series is overlapping.
Where startRange and endRange is the start and end for all existing ranges.
The OP suggests two checks to validate that a new range does not overlap with an existing range. However, these two checks do not cover all the possibilities. For instance, if the existing range is (4,10) and the new range is (2,12), it will not be flagged, because it starts prior to the start of the existing range, and ends afterwards.
Instead, I'd recommend the following approach:
if (newRangeStart <= existingRangeEnd && newRangeEnd >= existingRangeStart) {
// we have an overlap
}
Essentially, there are four possibilities of overlapping ranges:
- A range which starts before the existing range and ends within the existing range
- A range which starts within the existing range and ends afterward
- A range which starts within the existing range and ends within the existing range
- A range which starts before the existing range and ends after the existing range
Cases (1) and (2) include partial overlap, while cases (3) and (4) include complete overlap (either the existing range completely encloses the new range [case 3] or the new range completely encloses the existing range [case 4]).
The OP's code catches cases 1, 2 and 3, but not case 4. The code here catches all 4 possibilities of an overlapping range.
Ranges need to have multiple checks:
You can have many variations of overlap, so you'll need to do several tests.
|-----------| |--------------|
|------------------------|
|-------------|
|--------------------------------|
First Check: Start of Range 1 >= Start of Range 2 and Start of Range 1 <= End of Range 2
Second Check: Start of Range 2 >= Start of Range 1 and Start of Range 2 <= End of Range 1
Third Check: End of Range 1 >= Start of Range 2 and End of Range 1 <= End of Range 2
Fourth Check: End of Range 2 >= Start of Range 1 and End of Range 2 <= End of Range 1
These checks assume that End >= Start on both ranges. If not, you'll need to swap the start and end for the tests.
public static bool DoRangesOverlap(int p_start1, int p_end1, int p_start2, int p_end2)
{
if ((p_start1 >= p_start2 && p_start1 <= p_end2) || (p_start2 >= p_start1 && p_start2 <= p_end1) || (p_end1 >= p_start2 && p_end1 <= p_end2) || (p_end2 >= p_start1 && p_end2 <= p_end1))
{
return true;
}
return false;
}
精彩评论