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