
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find minimum number of intervals to be removed to remove overlaps in C++
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
- if start time of arr[i] >= end, then
- return n – count
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: static bool cmp(vector <int>& a, vector <int>& b){ return a[1] < b[1]; } 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[0][1]; for(int i = 1; i < n; i++){ if(arr[i][0] >= end){ end = arr[i][1]; 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
- Related Articles
- Minimum number of elements to be removed to make XOR maximum using C++.
- Find the number of boxes to be removed in C++
- Program to count minimum invalid parenthesis to be removed to make string correct in Python
- Program to find minimum number of heights to be increased to reach destination in Python
- C++ Program to count number of characters to be removed to get good string
- Program to find minimum number of characters to be added to make it palindrome in Python
- Minimum number of elements that should be removed to make the array good using C++.
- Minimum number of points to be removed to get remaining points on one side of axis using C++.
- Program to find shortest subarray to be removed to make array sorted in Python
- Program to find minimum number of people to teach in Python
- Program to find number of elements can be removed to make odd and even indexed elements sum equal in Python
- Program to find minimum remove required to make valid parentheses in Python
- Number of digits to be removed to make a number divisible by 3 in C++
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of lectures to attend to maintain 75% in C++

Advertisements