**Merge New Interval - Amazon Top Interview Questions**

### Problem Statement :

Given a list of intervals that are: Non-overlapping Sorted in increasing order by end times Merge a new interval target into the list so that the above two properties are met. Constraints n ≤ 100,000 where n is the length of intervals Example 1 Input intervals = [ [1, 10], [20, 30], [70, 100] ] target = [5, 25] Output [ [1, 30], [70, 100] ] Explanation [5, 25] is merged with the first two intervals [1, 10], [20, 30]. Example 2 Input intervals = [ [1, 2], [3, 5], [7, 10] ] target = [5, 7] Output [ [1, 2], [3, 10] ] Explanation The [5, 7] is merged with [3, 5] and [7, 10].

### Solution :

` ````
Solution in C++ :
vector<vector<int>> solve(vector<vector<int>>& intervals, vector<int>& target) {
int l = target[0];
int r = target[1];
vector<vector<int>> ans;
bool flag1 = false;
for (int i = 0; i < intervals.size(); i++) {
if ((intervals[i][0] > r) || (intervals[i][1] < l)) {
if ((r < intervals[i][0]) && (!flag1)) {
ans.push_back({l, r});
flag1 = true;
}
ans.push_back(intervals[i]);
} else {
l = min(l, intervals[i][0]);
r = max(r, intervals[i][1]);
}
}
if (!flag1) ans.push_back({l, r});
return ans;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public int[][] solve(int[][] intervals, int[] target) {
LinkedList<int[]> res = new LinkedList();
int i = 0, n = intervals.length;
// first add all the intervals which are less than target
while (i < n && intervals[i][1] < target[0]) {
res.add(intervals[i]);
i++;
}
// now merge the intervals with target
while (i < n && intervals[i][0] <= target[1]) {
target[0] = Math.min(intervals[i][0], target[0]);
target[1] = Math.max(intervals[i][1], target[1]);
i++;
}
// add target interval , once it is merged with valid one
res.add(target);
// add remaining
while (i < n) {
res.add(intervals[i]);
i++;
}
return res.toArray(new int[res.size()][]);
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, intervals, target):
left, right = [], []
s, e = target
for start, end in intervals:
if end < s:
left.append([start, end])
elif start > e:
right.append([start, end])
else:
s, e = min(s, start), max(e, end)
return left + [[s, e]] + right
```

## View More Similar Problems

## Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →## Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →