# Program to find minimum number of intervals to be removed to remove overlaps in C++

C++Server Side ProgrammingProgramming

Suppose we have a set of intervals; we have to find the minimum number of intervals that should be removed to make the rest of the intervals non-overlapping. So if the intervals are [[8,10],[3,5],[6,9]], then the output will be 1, as we have to remove [6,9] to make all others are non-overlapping.

To solve this, we will follow these steps −

• n := size of array
• if n is 0, then return 0
• count := 1
• sort the array based on the end time of the intervals
• end := end date of the first interval
• for i in range 1 to n – 1
• if start time of arr[i] >= end, then
• end := end time of arr[i]
• increase count by 1
• return n – count

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
static bool cmp(vector <int>& a, vector <int>& b){
return a < b;
}
int eraseOverlapIntervals(vector<vector<int>>& arr) {
int n = arr.size();
if(!n)return 0;
int cnt = 1;
sort(arr.begin(), arr.end(), cmp);
int end = arr;
for(int i = 1; i < n; i++){
if(arr[i] >= end){
end = arr[i];
cnt++;
}
}
return n - cnt;
}
};
main(){
vector<vector<int>>
v = {{8,10},{3,5},{6,9}};
Solution ob;
cout << (ob.eraseOverlapIntervals(v));
}

## Input

{{8,10},{3,5},{6,9}}

## Output

1