Check if given intervals can be made non-overlapping by adding/subtracting some X

Given an array arr[] containing N intervals, the task is to check that if the intervals can be added or subtracted by X after which there are no overlapping intervals. Here X be any real number.


Input: arr[] = {[1, 3], [2, 4], [4, 5], [5, 6]}
Output: YES
We can add X = 1000 in 1^{st} and 3^{rd} intervals
ans substract X = 1000 in 2^{nd} and 4^{th} intervals.

Input: arr[] = {[1, 2], [3, 4], [5, 6]}
Output: YES
No intervals are overalapping.

Input: arr[] = {[1, 4], [2, 2], [2, 3]}
Output: NO
There is no possible X such that intervals don’t overlap.

Approach: The idea is to compare each intervals as a pair with the help of the Nested loops and then for each interval check that they overlap. If any three intervals overlap with each other then there is no way to add any value of X to form Non-overlapping.
We can find if there is a overlapping in the three intervals with each other using union find or disjoint set data structures.

Below is the implementation of the above approach:






# Python implementation to check if
# the intervals can be non-overlapping by 
# by adding or subtracting 
# X to each interval
# Function to check if two intervals 
# overlap with each other
def checkOverlapping(a, b):
    a, b = max(a, b), min(a, b)
    # Condition to check if the
    # intervals overlap
    if b[0]<= a[0]<= b[1]:
        return True
    return False
# Function to check if there 
# is a existing overlapping 
# intervals
def find(a, i):
    if a[i]== i:
        return i
    # Path compression
    a[i]= find(a, a[i])
    return a[i] 
# Union of two intervals
# Returns True 
# if there is a overlapping 
# with the same another interval
def union(a, x, y):     
    xs = find(a, x)
    ys = find(a, y)
    if xs == ys:
        # Both have same
        # another 
        # overlapping interval
        return True
    a[ys]= xs
    return False
# Function to check if the intervals
# can be added by X to form 
# non-overlapping intervals
def checkNonOverlapping(arr, n):
    dsu =[i for i in range(n + 1)]
    for i in range(n):
        for j in range(i + 1, n):
            # If the intervals 
            # overlaps
            # we will union them
            if checkOverlapping(arr[i], \
                if union(dsu, i, j):
                    return False
    # There is no cycle
    return True
# Driver Code
if __name__ == "__main__":
    arr =[[1, 4], [2, 2], [2, 3]]
    n = len(arr)
    print("YES" if checkNonOverlapping\
       (arr, n) else "NO")





Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Recommended Posts:

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.