Question TLE error

Solving Merge Intervals question on InterviewBit https://www.interviewbit.com/problems/merge-intervals/

following is the code:-

/**

  • Definition for an interval.
    *struct Interval {
    *int start;
    *int end;
    *Interval() : start(0), end(0) {}
    *Interval(int s, int e) : start(s), end(e) {}

  • };
    */
    vector Solution::insert(vector &intervals, Interval newInterval) {
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
    if(intervals.size()==0){
    intervals.push_back(newInterval);
    return intervals;
    }

    Interval tobeAdded = newInterval;

    int i=0;

    while(i<intervals.size()){
    if(intervals[i].end <tobeAdded.start){
    i++;
    continue;
    }
    else if(tobeAdded.end < intervals[i].start){
    intervals.insert(intervals.begin()+i,tobeAdded);
    break;
    }
    else{
    tobeAdded.start = min(tobeAdded.start,intervals[i].start);
    tobeAdded.end = max(tobeAdded.end,intervals[i].end);
    intervals.erase(intervals.begin()+i);
    }
    }

    if(i == intervals.size()){
    intervals.insert(intervals.begin()+i,tobeAdded);
    }
    return intervals;
    }

please tell the reason of TLE as according to me it shoould be get submitted

@Anujg935

You can solve this problem in log(N) time complexity using Binary Search.

hi can you please elaborate the approach further .
by binary search how can i merge the new interval in case of overlapping of intervals with the interval to be added.

@Anujg935

Example
5
1 4
5 9
13 17
19 25
26 27
To Insert 8 20

The above five intervals are sorted so we can search for a place for ToInsert.start (8) in the sorted array
Here in this case before 9 and a place for ToInsert.end (20) Here in this case after 19 so just remove all the intervals between these two intervals and add interval 5 25
Place for ToInsert.start and ToInsert.end can be searched using binary search in log(N)