Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Data Structures and Algorithms

Insert Interval Problem Solved (C++, Java, Python)

Mansi Agarwal by Mansi Agarwal
February 1, 2024
Reading Time: 9 mins read
Insert Interval Problem
Follow us on Google News   Subscribe to our newsletter

Interval-related questions are frequently asked in technical interviews. Some of the common questions among them are Insert Interval, Merge Interval, Meeting Room, etc. This article will explore various approaches to solving the Insert Interval Problem, with implementation in C++, Java, and Python.

What is the Insert Interval Problem?

In the Insert Interval problem, you are given an array containing intervals in sorted and non-overlapping intervals, And you are also provided with an additional interval, Now the task is to insert this interval into the array such that the given array will still be sorted and in a non-overlapping interval. Return a new array as a result.

Let us understand this with an example:

  • Consider you have an array arr = [[2,5], [6,8], [10,13]]. Intervals in the given array look like this,
Example Array
  • And an additional interval = [4,7] will look like this
Adding interval 4-7
  • After overlapping that new interval into the given array the new array will be 
Overlapping the intervals
  • So the resultant arr = [[2,8], [10,13]].
Output

Brute Force Approach: Using Sorting 

Before using this method, you may have to go through some sorting algorithms and understand their time complexity. This problem incorporates concepts from sorting.

Sorting can be used to solve the Insert Interval problem. First, create a new array, then copy the given array into the new array result and insert the new interval into the original array. Then sort the whole array and merge the intervals if they are overlapping.

We can take an example:

  • Let the given array be [[2,5], [6,8], [10,13]] and interval be interval= [4,7]
Input Array and Interval 4-7
  •  Now create a new array ans, copy the values in the array, and then push the interval into the ans. 
Push interval in ans array
  • Now sort the new array ans, it will look like this
Sort the new array
  • Now merge the overlapping intervals in this case overlapping intervals are [2,5], [4,7], [6,8], and they are all combined into a single interval [2,8].
  • The new array ans=[[2,8], [10,13]] will look like this 
New answer array

The Time complexity of solving the problem using sorting is Nlog(N) where N is the size of the array. While sorting, we need Nlog(N) to sort the array and N to merge the whole array into a single array, so the total time will be (Nlog(N)+ N) ~  Nlog(N). The Space Complexity is O(N) for the new array as it stores all the values of the array.

Optimized Approach to Solve It

As in the above approach, we are making use of sorting which increases our time complexity to O(Nlog(N)) which can be reduced by taking advantage of the sorted array as we know that the given array arr is already sorted. So insert the element into the resultant array and compare the values. This can easily be done by following the steps,

  • First, create a resultant array, so your interval, array, and the new resultant array will look like this 
Resultant Array
  • Second step, we will keep on inserting the values from the array i.e. arr into the ans if interval[0] > arr[i][1] where i is the current index. So in this case, as arr[0][1] is greater than interval[0] we will not push the interval into the array.
Condition to push
  • In the third step, we will keep on updating the interval till arr[i][0] < interval[1], and if not we will add that interval to the ans. 
updating the interval till arr[i][0] < interval[1]
  • At last, we will add all the remaining arr intervals into the ans and return ans as a resultant array. As in this case only interval left is [10,13] so the resultant array will be ans= [[2,8], [10,13]].
Resultant array

C++ Code

Here is the C++ program to solve the Insert Interval problem:

#include <bits/stdc++.h>
using namespace std;

int main(){

    vector<vector<int>>arr = {{2,5},{6,8},{10,13}};
    vector<int>interval={4,7};
    vector<vector<int>>ans;

    int n= arr.size(), i=0;  // n is the size of arr 

    // keep on pushing the intervals of array into the ans till they are not overlapping and index is  less than the current size of array

    while(i<n && (arr[i][1] < interval[0])){
        ans.push_back(arr[i]);
        i++;
    }

    // keep updating values till they are overlapping
    while(i<n && (arr[i][0] <= interval[1])){
        interval[0] = min(arr[i][0], interval[0]);
        interval[1] = max(arr[i][1],interval[1]);
        i++;
    }

    ans.push_back(interval);

    while(i<n){
        ans.push_back(arr[i]);
        i++;
    }

    for(int j=0;j<ans.size();j++){
        cout<<ans[j][0]<<" "<<ans[j][1]<<endl;
    }
    // the array in this case is 
    // Output ans={{2,8}, {10,13}}

    return 0;

}

Java Code

Here is the implementation in Java:

import java.util.ArrayList;

class MergeIntervals {

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
        arr.add(new ArrayList<Integer>() {{ add(2); add(5); }});
        arr.add(new ArrayList<Integer>() {{ add(6); add(8); }});
        arr.add(new ArrayList<Integer>() {{ add(10); add(13); }});

        ArrayList<Integer> interval = new ArrayList<Integer>() {{ add(4); add(7); }};
        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();

        int n = arr.size();
        int i = 0;

        while (i < n && arr.get(i).get(1) < interval.get(0)) {
            ans.add(arr.get(i));
            i++;
        }

        while (i < n && arr.get(i).get(0) <= interval.get(1)) {
            interval.set(0, Math.min(arr.get(i).get(0), interval.get(0)));
            interval.set(1, Math.max(arr.get(i).get(1), interval.get(1)));
            i++;
        }

        ans.add(interval);

        while (i < n) {
            ans.add(arr.get(i));
            i++;
        }

        for (ArrayList<Integer> pair : ans) {
            System.out.println(pair.get(0) + " " + pair.get(1));
        }
    }
}

Python Code

You can do it in Python as well:

arr = [[2, 5], [6, 8], [10, 13]]
interval = [4, 7]
ans = []

n = len(arr)
i = 0

# Keep pushing intervals into ans until they are not overlapping and index is less than the current size of an array
while i < n and arr[i][1] < interval[0]:
    ans.append(arr[i])
    i += 1

# Keep updating values until intervals are overlapping
while i < n and arr[i][0] <= interval[1]:
    interval[0] = min(arr[i][0], interval[0])
    interval[1] = max(arr[i][1], interval[1])
    i += 1

ans.append(interval)

# Push the remaining intervals into ans
while i < n:
    ans.append(arr[i])
    i += 1

# Print the result
for j in ans:
    print(j[0], j[1])

Output:

2 8
10 13

In the above approach the time complexity will be O(N) as in this case, we are iterating over the whole array only once. Space Complexity will also be O(N) as we are making an array ans as a resultant array.

Here is a small comparison of both the approaches:

ApproachTime Complexity Space ComplexityDescription
Brute Force: Using SortingO(N*logN)O(N)Sort the whole array and merge the intervals if they are overlapping with the copy of the original array.
Optimized ApproachO(N)O(N)Reduce the time complexity by using the arr array as it is already sorted.

Conclusion

We solved the Insert Interval problem from leetcode in this article, as well as found out how can we approach a problem through different methods. Practice more similar problems to get a good grasp of this concept.

ShareTweetShareSendSend
Mansi Agarwal

Mansi Agarwal

I am Mansi Agarwal, a final-year student at IIT BHU. I am very keen on competitive programming, with my primary area of interest being Data Structures & Algorithms. I have solved over 1000 LeetCode problems.

RelatedPosts

validate ip address

Validate IP Address Problem ( C++, JAVA, Python)

April 3, 2025
Shortest Palindrome Problem

Shortest Palindrome Problem ( C++, JAVA, Python)

April 4, 2025
Zigzag Conversion Problem

Zigzag Conversion Problem (C++, Java, Python)

April 4, 2025
Next Right Pointers In Each Node

Populating Next Right Pointers In Each Node (with code)

March 27, 2024
Coin Change II

Coin Change II Problem (C++, Java, Python)

April 7, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.