Close
Topic list
Insert Interval
MEDIUM
30 mins
Stacks & Queues
Arrays
Topics (Covered in this problem)
Problem solved
Skill meter
Stacks & Queues
-
-
Arrays
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Insert Interval

Contributed by
Paritosh Singh
Medium
0/80
Avg time to solve 30 mins
Success Rate 65 %
Share

## Problem Statement

#### You are given a 2-dimensional array ‘Intervals’ containing a list of non-overlapping intervals sorted by their start time. You are given an interval ‘newInterval’. Your task is to insert the given interval at the correct position and merge all necessary intervals to produce a list with only mutually exclusive intervals.

##### For Example:
``````Consider 'Intervals' = [[1, 3], [5, 7], [8, 12]], and 'newInterval' = [4, 6]
The interval [4, 6] overlaps with [5, 7]. Therefore we can merge the intervals and produce an interval [4, 7]. Hence the answer [[1,3], [4,7], [8,12]]
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````2
3
1 3
5 7
8 12
4 6
3
1 3
5 7
8 12
4 10
``````
##### Sample Output 1:
``````1 3
4 7
8 12
1 3
4 12
``````
##### Explanation:
``````For the first test case,
The interval [4, 6] overlaps with [5, 7]. Therefore we can merge the intervals and produce an interval [4, 7]. Hence the answer [[1,3], [4,7], [8,12]].

For the second test case,
The interval [4, 10] overlaps with [5, 7] and [8, 12]. Therefore we can merge the intervals and produce an interval [4, 12]. Hence the answer is [[1, 3], [4, 12]].
``````
##### Sample Input 2:
``````2
2
2 3
5 7
1 4
2
1 2
6 9
3 5
``````
##### Sample Output 2:
``````1 4
5 7
1 2
3 5
6 9
``````
Auto
Console