Merge Overlapping Intervals
Last Updated :
26 Mar, 2025
Given an array of time intervals where arr[i] = [starti, endi], the task is to merge all the overlapping intervals into one and output the result which should have only mutually exclusive intervals.
Examples:
Input: arr[] = [[1, 3], [2, 4], [6, 8], [9, 10]]
Output: [[1, 4], [6, 8], [9, 10]]
Explanation: In the given intervals, we have only two overlapping intervals [1, 3] and [2, 4]. Therefore, we will merge these two and return [[1, 4]], [6, 8], [9, 10]].
Input: arr[] = [[7, 8], [1, 5], [2, 4], [4, 6]]
Output: [[1, 6], [7, 8]]
Explanation: We will merge the overlapping intervals [[1, 5], [2, 4], [4, 6]] into a single interval [1, 6].
[Naive Approach] Checking All Possible Overlaps – O(n^2) Time and O(n) Space
A simple approach is to group all the intervals by sorting them then start from the first interval and compare it with all other intervals for overlaps. If the first interval overlaps with any other interval, then remove the other interval from the list and merge the other into the first interval. Repeat the same steps for the remaining intervals after the first.
C++
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> mergeOverlap(vector<vector<int>> &arr) {
int n = arr.size();
sort(arr.begin(), arr.end());
vector<vector<int>> res;
// Checking for all possible overlaps
for (int i = 0; i < n; i++) {
int start = arr[i][0];
int end = arr[i][1];
// Skipping already merged intervals
if (!res.empty() && res.back()[1] >= end)
continue;
// Find the end of the merged range
for (int j = i + 1; j < n; j++) {
if (arr[j][0] <= end)
end = max(end, arr[j][1]);
}
res.push_back({start, end});
}
return res;
}
int main() {
vector<vector<int>> arr = {{7, 8}, {1, 5}, {2, 4}, {4, 6}};
vector<vector<int>> res = mergeOverlap(arr);
for (auto interval : res)
cout << interval[0] << " " << interval[1] << endl;
return 0;
}
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class GfG {
static List<int[]> mergeOverlap(int[][] arr) {
int n = arr.length;
Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0]));
List<int[]> res = new ArrayList<>();
// Checking for all possible overlaps
for (int i = 0; i < n; i++) {
int start = arr[i][0];
int end = arr[i][1];
// Skipping already merged intervals
if (!res.isEmpty() && res.get(res.size() - 1)[1] >= end) {
continue;
}
// Find the end of the merged range
for (int j = i + 1; j < n; j++) {
if (arr[j][0] <= end) {
end = Math.max(end, arr[j][1]);
}
}
res.add(new int[]{start, end});
}
return res;
}
public static void main(String[] args) {
int[][] arr = {{7, 8}, {1, 5}, {2, 4}, {4, 6}};
List<int[]> res = mergeOverlap(arr);
for (int[] interval : res) {
System.out.println(interval[0] + " " + interval[1]);
}
}
}
Python
def mergeOverlap(arr):
n = len(arr)
arr.sort()
res = []
# Checking for all possible overlaps
for i in range(n):
start = arr[i][0]
end = arr[i][1]
# Skipping already merged intervals
if res and res[-1][1] >= end:
continue
# Find the end of the merged range
for j in range(i + 1, n):
if arr[j][0] <= end:
end = max(end, arr[j][1])
res.append([start, end])
return res
if __name__ == "__main__":
arr = [[7, 8], [1, 5], [2, 4], [4, 6]]
res = mergeOverlap(arr)
for interval in res:
print(interval[0], interval[1])
C#
using System;
using System.Collections.Generic;
class GfG {
static List<int[]> mergeOverlap(int[][] arr) {
int n = arr.Length;
Array.Sort(arr, (a, b) => a[0].CompareTo(b[0]));
List<int[]> res = new List<int[]>();
// Checking for all possible overlaps
for (int i = 0; i < n; i++) {
int start = arr[i][0];
int end = arr[i][1];
// Skipping already merged intervals
if (res.Count > 0 && res[res.Count - 1][1] >= end)
continue;
// Find the end of the merged range
for (int j = i + 1; j < n; j++) {
if (arr[j][0] <= end)
end = Math.Max(end, arr[j][1]);
}
res.Add(new int[] { start, end });
}
return res;
}
static void Main() {
int[][] arr = new int[][] {
new int[] { 7, 8 },
new int[] { 1, 5 },
new int[] { 2, 4 },
new int[] { 4, 6 }
};
List<int[]> res = mergeOverlap(arr);
foreach (var interval in res)
Console.WriteLine($"{interval[0]} {interval[1]}");
}
}
JavaScript
function mergeOverlap(arr) {
let n = arr.length;
arr.sort((a, b) => a[0] - b[0]);
let res = [];
// Checking for all possible overlaps
for (let i = 0; i < n; i++) {
let start = arr[i][0];
let end = arr[i][1];
// Skipping already merged intervals
if (res.length > 0 && res[res.length - 1][1] >= end) {
continue;
}
// Find the end of the merged range
for (let j = i + 1; j < n; j++) {
if (arr[j][0] <= end) {
end = Math.max(end, arr[j][1]);
}
}
res.push([start, end]);
}
return res;
}
const arr = [[7, 8], [1, 5], [2, 4], [4, 6]];
const res = mergeOverlap(arr);
for (const interval of res)
console.log(interval[0], interval[1]);
[Expected Approach] Checking Last Merged Interval – O(n*log(n)) Time and O(n) Space
In the previous approach, for each range we are checking for possible overlaps by iterating over all the remaining ranges till the end. We can optimize this by checking only those intervals that overlap with the last merged interval. Since the intervals will be sorted based on starting point, so if we encounter an interval whose starting time lies outside the last merged interval, then all further intervals will also lie outside it.
The intuition is to first sort the intervals based on their starting points. This allows us to easily identify overlapping intervals by comparing each interval with the last merged interval. Now, iterate over each interval and if the current interval overlaps with the last merged interval, then merge them both. Otherwise, append the merged interval to the result.
C++
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> mergeOverlap(vector<vector<int>>& arr) {
// Sort intervals based on start values
sort(arr.begin(), arr.end());
vector<vector<int>> res;
res.push_back(arr[0]);
for (int i = 1; i < arr.size(); i++) {
vector<int>& last = res.back();
vector<int>& curr = arr[i];
// If current interval overlaps with the last merged
// interval, merge them
if (curr[0] <= last[1])
last[1] = max(last[1], curr[1]);
else
res.push_back(curr);
}
return res;
}
int main() {
vector<vector<int>> arr = {{7, 8}, {1, 5}, {2, 4}, {4, 6}};
vector<vector<int>> res = mergeOverlap(arr);
for (vector<int>& interval: res)
cout << interval[0] << " " << interval[1] << endl;
return 0;
}
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class GfG {
static List<int[]> mergeOverlap(int[][] arr) {
// Sort intervals based on start values
Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0]));
List<int[]> res = new ArrayList<>();
res.add(new int[]{arr[0][0], arr[0][1]});
for (int i = 1; i < arr.length; i++) {
int[] last = res.get(res.size() - 1);
int[] curr = arr[i];
// If current interval overlaps with the last merged
// interval, merge them
if (curr[0] <= last[1])
last[1] = Math.max(last[1], curr[1]);
else
res.add(new int[]{curr[0], curr[1]});
}
return res;
}
public static void main(String[] args) {
int[][] arr = {{7, 8}, {1, 5}, {2, 4}, {4, 6}};
List<int[]> res = mergeOverlap(arr);
for (int[] interval : res)
System.out.println(interval[0] + " " + interval[1]);
}
}
Python
def mergeOverlap(arr):
# Sort intervals based on start values
arr.sort()
res = []
res.append(arr[0])
for i in range(1, len(arr)):
last = res[-1]
curr = arr[i]
# If current interval overlaps with the last merged
# interval, merge them
if curr[0] <= last[1]:
last[1] = max(last[1], curr[1])
else:
res.append(curr)
return res
if __name__ == "__main__":
arr = [[7, 8], [1, 5], [2, 4], [4, 6]]
res = mergeOverlap(arr)
for interval in res:
print(interval[0], interval[1])
C#
using System;
using System.Collections.Generic;
class GfG {
static List<int[]> mergeOverlap(int[][] arr) {
// Sort intervals based on start values
Array.Sort(arr, (a, b) => a[0].CompareTo(b[0]));
List<int[]> res = new List<int[]>();
res.Add(new int[] { arr[0][0], arr[0][1] });
for (int i = 1; i < arr.Length; i++) {
int[] last = res[res.Count - 1];
int[] curr = arr[i];
// If current interval overlaps with the last merged
// interval, merge them
if (curr[0] <= last[1])
last[1] = Math.Max(last[1], curr[1]);
else
res.Add(new int[] { curr[0], curr[1] });
}
return res;
}
static void Main() {
int[][] arr = new int[][] {
new int[] { 7, 8 },
new int[] { 1, 5 },
new int[] { 2, 4 },
new int[] { 4, 6 }
};
List<int[]> res = mergeOverlap(arr);
foreach (int[] interval in res)
Console.WriteLine(interval[0] + " " + interval[1]);
}
}
JavaScript
function mergeOverlap(arr) {
// Sort intervals based on start values
arr.sort((a, b) => a[0] - b[0]);
const res = [];
res.push(arr[0]);
for (let i = 1; i < arr.length; i++) {
const last = res[res.length - 1];
const curr = arr[i];
// If current interval overlaps with the last merged
// interval, merge them
if (curr[0] <= last[1])
last[1] = Math.max(last[1], curr[1]);
else
res.push(curr);
}
return res;
}
// Driver Code
const arr = [[7, 8], [1, 5], [2, 4], [4, 6]];
const res = mergeOverlap(arr);
for (const interval of res)
console.log(interval[0] + " " + interval[1]);
[Alternate Approach] In-Place Merging – O(n*log(n)) time and O(1) space
The above solution requires O(n) space for sorting the merged intervals. We can avoid the extra space by doing merge operations in place.
Follow the steps mentioned below to implement the approach:
- Sort all intervals in increasing order of start time.
- Traverse sorted intervals starting from the first interval,
- Do the following for every interval.
- If the current interval is not the first interval and it overlaps with the previous interval, then merge it with the previous interval. Keep doing it while the interval overlaps with the previous one.
- If the current interval does not overlap with the previous interval, move to the next interval.
C++
#include <bits/stdc++.h>
using namespace std;
// Merge overlapping intervals in-place. We return
// modified size of the array arr.
int mergeOverlap(vector<vector<int>>& arr) {
// Sort intervals based on start values
sort(arr.begin(), arr.end());
// Index of the last merged
int resIdx = 0;
for (int i = 1; i < arr.size(); i++) {
// If current interval overlaps with the
// last merged interval
if (arr[resIdx][1] >= arr[i][0])
arr[resIdx][1] = max(arr[resIdx][1], arr[i][1]);
// Move to the next interval
else {
resIdx++;
arr[resIdx] = arr[i];
}
}
// Returns size of the merged intervals
return (resIdx + 1);
}
int main() {
vector<vector<int>> arr = {{7, 8}, {1, 5}, {2, 4}, {4, 6}};
// Get the new size of the array after merging
int newSize = mergeOverlap(arr);
// Print the merged intervals based on the new size
for (int i = 0; i < newSize; i++) {
cout << arr[i][0] << " " << arr[i][1] << "\n";
}
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int (*)[2])a)[0] - (*(int (*)[2])b)[0];
}
// Merge overlapping intervals in-place. We return
// modified size of the array arr.
int mergeOverlap(int arr[][2], int size) {
// Sort intervals based on start values
qsort(arr, size, sizeof(arr[0]), compare);
// Index of the last merged
int resIdx = 0;
for (int i = 1; i < size; i++) {
// If current interval overlaps with the
// last merged interval
if (arr[resIdx][1] >= arr[i][0]) {
arr[resIdx][1] = arr[resIdx][1] > arr[i][1] ? arr[resIdx][1] : arr[i][1];
}
// Move to the next interval
else {
resIdx++;
arr[resIdx][0] = arr[i][0];
arr[resIdx][1] = arr[i][1];
}
}
// Returns size of the merged intervals
return (resIdx + 1);
}
int main() {
int arr[][2] = {{7, 8}, {1, 5}, {2, 4}, {4, 6}};
int size = sizeof(arr) / sizeof(arr[0]);
// Get the new size of the array after merging
int newSize = mergeOverlap(arr, size);
// Print the merged intervals based on the new size
for (int i = 0; i < newSize; i++) {
printf("%d %d\n", arr[i][0], arr[i][1]);
}
return 0;
}
Java
import java.util.Arrays;
class GfG {
// Merge overlapping intervals in-place. We return
// modified size of the array arr.
static int mergeOverlap(int[][] arr) {
// Sort intervals based on start values
Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0]));
// Index of the last merged
int resIdx = 0;
for (int i = 1; i < arr.length; i++) {
// If current interval overlaps with the
// last merged interval
if (arr[resIdx][1] >= arr[i][0])
arr[resIdx][1] = Math.max(arr[resIdx][1], arr[i][1]);
// Move to the next interval
else {
resIdx++;
arr[resIdx] = arr[i];
}
}
// Returns size of the merged intervals
return (resIdx + 1);
}
public static void main(String[] args) {
int[][] arr = {{7, 8}, {1, 5}, {2, 4}, {4, 6}};
// Get the new size of the array after merging
int newSize = mergeOverlap(arr);
// Print the merged intervals based on the new size
for (int i = 0; i < newSize; i++) {
System.out.println(arr[i][0] + " " + arr[i][1]);
}
}
}
Python
def mergeOverlap(arr):
# Sort intervals based on start values
arr.sort()
# Index of the last merged
resIdx = 0
for i in range(1, len(arr)):
# If current interval overlaps with the
# last merged interval
if arr[resIdx][1] >= arr[i][0]:
arr[resIdx][1] = max(arr[resIdx][1], arr[i][1])
# Move to the next interval
else:
resIdx += 1
arr[resIdx] = arr[i]
# Returns size of the merged intervals
return resIdx + 1
if __name__ == "__main__":
arr = [[7, 8], [1, 5], [2, 4], [4, 6]]
# Get the new size of the array after merging
newSize = mergeOverlap(arr)
# Print the merged intervals based on the new size
for i in range(newSize):
print(arr[i][0], arr[i][1])
C#
using System;
using System.Collections.Generic;
class GfG {
// Merge overlapping intervals in-place. We return
// modified size of the array arr.
static int mergeOverlap(List<List<int>> arr) {
// Sort intervals based on start values
arr.Sort((a, b) => a[0].CompareTo(b[0]));
// Index of the last merged
int resIdx = 0;
for (int i = 1; i < arr.Count; i++) {
// If current interval overlaps with the
// last merged interval
if (arr[resIdx][1] >= arr[i][0])
arr[resIdx][1] = Math.Max(arr[resIdx][1], arr[i][1]);
// Move to the next interval
else {
resIdx++;
arr[resIdx] = arr[i];
}
}
// Returns size of the merged intervals
return (resIdx + 1);
}
static void Main() {
List<List<int>> arr = new List<List<int>> {
new List<int> {7, 8},
new List<int> {1, 5},
new List<int> {2, 4},
new List<int> {4, 6}
};
// Get the new size of the array after merging
int newSize = mergeOverlap(arr);
// Print the merged intervals based on the new size
for (int i = 0; i < newSize; i++) {
Console.WriteLine(arr[i][0] + " " + arr[i][1]);
}
}
}
JavaScript
function mergeOverlap(arr) {
// Merge overlapping intervals in-place. We return
// modified size of the array arr.
// Sort intervals based on start values
arr.sort((a, b) => a[0] - b[0]);
// Index of the last merged
let resIdx = 0;
for (let i = 1; i < arr.length; i++) {
// If current interval overlaps with the
// last merged interval
if (arr[resIdx][1] >= arr[i][0])
arr[resIdx][1] = Math.max(arr[resIdx][1], arr[i][1]);
// Move to the next interval
else {
resIdx++;
arr[resIdx] = arr[i];
}
}
// Returns size of the merged intervals
return resIdx + 1;
}
// Driver Code
const arr = [[7, 8], [1, 5], [2, 4], [4, 6]];
// Get the new size of the array after merging
const newSize = mergeOverlap(arr);
// Print the merged intervals based on the new size
for (let i = 0; i < newSize; i++) {
console.log(arr[i][0] + " " + arr[i][1]);
}
Similar Reads
Merge Overlapping Intervals
Given an array of time intervals where arr[i] = [starti, endi], the task is to merge all the overlapping intervals into one and output the result which should have only mutually exclusive intervals. Examples: Input: arr[] = [[1, 3], [2, 4], [6, 8], [9, 10]]Output: [[1, 4], [6, 8], [9, 10]]Explanatio
13 min read
Non-Overlapping Intervals
Given a list of intervals with starting and ending values, the task is to find the minimum number of intervals that are required to be removed to make remaining intervals non-overlapping. Examples: Input: intervals[][] = [[1, 2], [2, 3], [3, 4], [1, 3]]Output: 1 Explanation: Removal of [1, 3] makes
9 min read
Maximum number of overlapping Intervals
Given different intervals, the task is to print the maximum number of overlaps among these intervals at any time. Examples: Input: v = {{1, 2}, {2, 4}, {3, 6}} Output: 2 Explanation: The maximum overlapping is 2(between (1 2) and (2 4) or between (2 4) and (3 6)) Input: v = {{1, 8}, {2, 5}, {5, 6},
7 min read
Interval Tree
You are given a set of intervals, where each interval contains two variables, low and high, that defines the start and end time of the interval. The task is perform the following operations efficiently: Add an intervalRemove an intervalGiven an interval x, find if x overlaps with any of the existing
15+ min read
Find the point where maximum intervals overlap
Consider a big party where a log register for guest's entry and exit times is maintained. Find the time at which there are maximum guests in the party. Given the Entry(Entry[]) and Exit (Exit[]) times of individuals at a place. Note: Entries in the register are not in sorted order. Examples: Input:
14 min read
Find least non-overlapping number from a given set of intervals
Given an array interval of pairs of integers representing the starting and ending points of the interval of size N. The task is to find the smallest non-negative integer which is a non-overlapping number from the given set of intervals. Input constraints: [Tex]1 ⤠N ⤠10^{5}0 ⤠interval[i] ⤠10^{9}
15+ min read
Find Non-overlapping intervals among a given set of intervals
Given a 2d array arr[][] of time intervals, where each interval is of the form [start, end]. The task is to determine all intervals from the given array that do not overlap with any other interval in the set. If no such interval exists, return an empty list. Examples: Input: arr[] = [[1, 3], [2, 4],
5 min read
Find a pair of overlapping intervals from a given Set
Given a 2D array arr[][] with each row of the form {l, r}, the task is to find a pair (i, j) such that the ith interval lies within the jth interval. If multiple solutions exist, then print anyone of them. Otherwise, print -1. Examples: Input: N = 5, arr[][] = { { 1, 5 }, { 2, 10 }, { 3, 10}, {2, 2}
11 min read
Alternatively Merge two Strings
Given 2 strings, merge them in an alternate way, i.e. the final string's first character is the first character of the first string, the second character of the final string is the first character of the second string and so on. And if once you reach end of one string while if another string is stil
4 min read
Insert and Merge Interval
Given a set of non-overlapping intervals and a new interval, the task is to insert the interval at the correct position such that after insertion, the intervals remain sorted. If the insertion results in overlapping intervals, then merge the overlapping intervals. Assume that the set of non-overlapp
9 min read