Given a list of ranges with starting and end value, the task is to find the minimum number of ranges that are required to be removed to make remaining ranges non-overlapping.

**Examples:**

Input :input = {{1, 2}, {4, 7}, {3, 8}}Output :1Explanation:Removal of {3, 8} makes {{1, 2} and {4, 7}} non-overlapping.

Input :input = {{ 10, 20 }, { 10, 20 } , { 10, 20 }}Output :2Explanation:Removal of [10, 20] makes the remaining ranges non-overlapping.

Input :input = {{1, 2}, {5, 10}, {18, 35}, {40, 45}}Output :0Explanation:All ranges are already non-overlapping.

**Approach:**

- Sort the ranges by their starting values.
- Traverse through the ranges and check if any range has a starting point less than the ending point of the previous (ie. there is an overlap).
- Remove the range with greater ending point.
Below code is the implementation of the above approach:

## C++

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`int`

`minRemovals(vector<vector<`

`int`

`> >& ranges)`

`{`

`int`

`size = ranges.size(), rem = 0;`

`if`

`(size <= 1)`

`return`

`0;`

`// Sort by minimum starting point`

`sort(ranges.begin(), ranges.end(),`

`[](`

`const`

`vector<`

`int`

`>& a,`

`const`

`vector<`

`int`

`>& b)`

`{`

`return`

`a[0] < b[0]; });`

`int`

`end = ranges[0][1];`

`for`

`(`

`int`

`i = 1; i < ranges.size(); i++) {`

`// If the current starting point is less than`

`// the previous interval's ending point`

`// (ie. there is an overlap)`

`if`

`(ranges[i][0] < end) {`

`// increase rem`

`rem++;`

`// Remove the interval`

`// with the higher ending point`

`end = min(ranges[i][1], end);`

`}`

`else`

`end = ranges[i][1];`

`}`

`return`

`rem;`

`}`

`// Driver code`

`int`

`main()`

`{`

`vector<vector<`

`int`

`> > input = { { 19, 25 },`

`{ 10, 20 }, { 16, 20 } };`

`cout << minRemovals(input) << endl;`

`}`

## Python3

`def`

`minRemovels (ranges):`

`size`

`=`

`len`

`(ranges)`

`rem`

`=`

`0`

`# Sort by minimum starting point`

`ranges.sort()`

`end`

`=`

`ranges[`

`0`

`][`

`1`

`]`

`for`

`i`

`in`

`range`

`(`

`1`

`, size):`

`# If the current starting point is less`

`# than the previous interval's ending`

`# point (ie. there is an overlap)`

`if`

`(ranges[i][`

`0`

`] < end):`

`# Increase rem`

`rem`

`+`

`=`

`1`

`# Remove the interval`

`# with the higher ending point`

`end`

`=`

`min`

`(ranges[i][`

`1`

`], end)`

`else`

`:`

`end`

`=`

`ranges[i][`

`1`

`]`

`return`

`rem`

`# Driver Code`

`if`

`__name__`

`=`

`=`

`'__main__'`

`:`

`Input`

`=`

`[ [`

`19`

`,`

`25`

`],`

`[`

`10`

`,`

`20`

`],`

`[`

`16`

`,`

`20`

`] ]`

`print`

`(minRemovels(`

`Input`

`))`

`# This code is contributed by himanshu77`

**Output:**2

**Time Complexity:**O(n log n)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

**DSA Self Paced Course**at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer**Complete Interview Preparation Course****.**In case you wish to attend live classes with industry experts, please refer

**Geeks Classes Live**