<p>Given an array of intervals <code>intervals</code> where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, return <em>the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> intervals = [[1,2],[2,3],[3,4],[1,3]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> [1,3] can be removed and the rest of the intervals are non-overlapping.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> intervals = [[1,2],[1,2],[1,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> You need to remove two [1,2] to make the rest of the intervals non-overlapping.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> intervals = [[1,2],[2,3]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> You don&#39;t need to remove any of the intervals since they&#39;re already non-overlapping.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= intervals.length &lt;= 10<sup>5</sup></code></li>
	<li><code>intervals[i].length == 2</code></li>
	<li><code>-5 * 10<sup>4</sup> &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 5 * 10<sup>4</sup></code></li>
</ul>


#include<bits/stdc++.h>

using namespace std;

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {

    auto customComparator = [] (const pair<int,pair<int,int>>& a, const pair<int,pair<int,int>>& b)
    {
        if(a.second.first == b.second.first)
        {
            return a.first < b.first;
        }

        return a.second.second < b.second.second;
    };

       vector<pair<int,pair<int,int>>>v;

       int n= intervals.size();

       for(int i=0;i<n;i++)
       {  

        int x = abs(intervals[i][1] - intervals[i][0]);

        v.push_back({x,{intervals[i][0],intervals[i][1]}});


       }

        sort(v.begin(),v.end(),customComparator);

        int check = v[0].second.first;
        int check1 = v[0].second.second;
        int ans=1;  

        for(int i=0;i<n;i++)
       {  

          cout <<v[i].second.first<<" "<<v[i].second.second<<endl;

       }

       for(int i=1;i<v.size();i++)
       {
          int y = v[i].second.first;

          if(y>=check1)
          {
            check1 = v[i].second.second;
            ans++;
          }

       }

       return n-ans;



    }
};
105565