Last Updated: 8 Sep, 2025

Isolated Intervals Finder

Easy

Problem statement

You are given a 2D array arr representing a collection of N intervals, where each interval is of the form [start, end].

Your task is to identify and return all the "isolated" intervals. An interval is considered isolated if it does not overlap with any other interval in the given set.


Input Format:
The first line of input contains an integer 'N', the number of intervals.

The next 'N' lines each contain two space-separated integers, start and end, representing an interval.


Output Format:
Print the isolated intervals, one per line, with the start and end values separated by a space.

The output intervals must be sorted in ascending order based on their start times.

If no isolated intervals exist, print a single line with the value -1.


Note:
Two intervals [s1, e1] and [s2, e2] are considered to overlap if s1 <= e2 and s2 <= e1. This means they also overlap if they just "touch" at an endpoint (e.g., [1, 5] and [5, 10]).

The most efficient way to solve this is to first sort all intervals by their start times. Then, for each interval, you only need to check for overlaps with its immediate neighbors in the sorted list.