# Remove Interval in C++

C++Server Side ProgrammingProgramming

Suppose we have a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the set of numbers x such that a <= x < b. We have to remove the intersections between any interval in intervals and the interval toBeRemoved. Finally, we have to find the sorted list of intervals after all such removals. So if the input is like − [[0,2], [3,4],[5,7]], and toBeRemoved := [1, 6], so the output will be [[0, 2], [6,7]].

To solve this, we will follow these steps −

• Define a method called manipulate2(), this will take matrix a and an array y
• x := last row of matrix a, then delete last row from a
• z := x
• x := y, z := y
• if z − z, then insert z into a
• if x − x, then insert x into a
• The main method will take matrix in and an array t
• define a matrix ans, and n := number of rows in matrix in
• for i in range 0 to n –
• insert in[i] into ans
• a := last row of a, b := t
• if a > b, then swap a and b
• if a and b are intersecting, then call manipulate2(ans, t)
• return ans

## Example

Let us see the following implementation to get better understanding −

class Solution {
public:
bool isIntersect(vector <int> a, vector <int> b){
return max(a, a) >= min(b, b);
}
void manipulate2(vector < vector <int> > &a, vector <int> y){
vector <int> x = a.back();
a.pop_back();
vector <int> z = x;
x = y;
z = y;
if(z < z)a.push_back(z);
if(x < x)a.push_back(x);
}
vector<vector<int>> removeInterval(vector<vector<int>>& in, vector<int>& t) {
vector < vector <int> > ans;
int n = in.size();
for(int i = 0; i < n; i++){
ans.push_back(in[i]);
vector <int> a;
vector <int> b;
a = ans.back();
b = t;
if(a>b)swap(a, b);
if(isIntersect(a, b)){
manipulate2(ans, t);
}
}
return ans;
}
};

## Input

[[0,2],[3,4],[5,7]]
[1,6]

## Output

[[0,1],[6,7]]