public class num7 {
public ArrayList<Interval> insert(Interval[] intervals,Interval newInt){
ArrayList<Interval> res = new ArrayList<Interval>();
if(intervals == null){
res.add(newInt);
}
int i = 0, n = intervals.length;
while(i<n && newInt.start>intervals[i].end){
res.add(intervals[i++]);
}
while(i<n && newInt.end >= intervals[i].start){
newInt.end = Math.max(newInt.end, intervals[i].end);
newInt.start = Math.min(newInt.start, intervals[i].start);
i++;
}
res.add(newInt);
while(i<n){
res.add(intervals[i++]);
}
return res;
}
public static void main(String[] args) {
Interval[] A = new Interval[2];
A[0] = new Interval(1, 5);
A[1] = new Interval(6, 10);
Interval newInterval = new Interval(4, 6);
ArrayList<Interval> res = new ArrayList<Interval>();
res = new num7().insert(A, newInterval);
for (Interval interval : res) {
System.out.println(interval.start + "," + interval.end);
}
}
}