# 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.

Examples:

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

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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## Python3

 `# 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], \ ` `                               ``arr[j]): ` `                ``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"``) `

Output:

```NO
```
Output:

```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 Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.