implemented merge_intervals algorithm