I solved (with 4 WAs) it without the Hints and here is my Approach -

import java.util.*;import java.io.*;class Main{public static void main(String[] args) throws IOException{Main m = new Main();m.start();}public void start() throws IOException{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine());ArrayList<Interval> disIntervals = new ArrayList<>();for(int i = 0;i<n;i++){String[] line = br.readLine().split(" ");int a = Integer.parseInt(line[0]);int b = Integer.parseInt(line[1]);boolean merged = false;for(int j = 0;j<disIntervals.size();j++){Interval p = disIntervals.get(j);if((a>=p.start && a<=p.end)||(b>=p.start && b<=p.end)){p.start = Math.max(p.start,a);p.end = Math.min(p.end,b);