import java.util.Arrays;
public class Main {
static boolean hasOverlap(int[][] intervals) {
if (intervals.length == 0) {
return true;
}
// Sorting is essential because once intervals are ordered by
// start time, any overlap can only occur between adjacent intervals.
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
// (5,9), (11,12), (15,17)
// (5,9) and (11,12) → 11 < 9? No
// (11,12) and (15,17) → 15 < 12? No
// The loop compares each interval with the one before it.
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false; // Overlap found
}
}
return true; // No overlap
}
public static void main(String[] args) {
int[][] intervals = {
{11, 12}, {5, 9}, {15, 17}
};
if (hasOverlap(intervals))
System.out.println("There are NO overlapping intervals");
else
System.out.println("There ARE overlapping intervals");
}
}
/*
run:
There are NO overlapping intervals
*/