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.

**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"` `) ` |

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Check if given array can be made 0 with given operations performed any number of times
- Check if elements of array can be made equal by multiplying given prime numbers
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Check if X and Y can be made zero by using given operation any number of times
- Check if item can be measured using a scale and some weights
- Elements of Array which can be expressed as power of some integer to given exponent K
- Check if all bits can be made same by single flip
- Check whether an array can be made strictly decreasing by modifying at most one element
- Check whether the number can be made perfect square after adding 1
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if the rows of a binary matrix can be made unique by removing a single column
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check if N and M can be made equal by increasing N by A and decreasing M by B
- Check whether the number can be made perfect square after adding K
- Check whether the number can be made palindromic after adding K
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Check if number can be made prime by deleting a single digit
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Count the number of intervals in which a given value lies
- Find intersection of intervals given by two lists

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.