In: Computer Science
USE JAVA
Check if any two intervals overlap among a given set of intervals, return true if they overlap and false if they don't.
Input: arr[] = {{1, 3}, {5, 7}, {2, 4}, {6, 8}} Output: true The intervals {1, 3} and {2, 4} overlap
import java.util.*;
import java.io.*;
import javafx.util.*;
public class MyClass {
public static boolean overlap(ArrayList
if(list.size() < 1)
return false;
list.sort((i1, i2) -> Integer.compare(i1.getKey(),
i2.getKey()));
for(int i = 1; i < list.size(); i++) {
if(list.get(i).getKey() <= list.get(i-1).getValue()) {
return true;
}
}
return false;
}
public static void main(String args[]) {
ArrayList
list.add(new Pair
list.add(new Pair
list.add(new Pair
list.add(new Pair
System.out.println(overlap(list));
}
}