Minimum removals required to make any interval equal to the union of the given Set | C ++ implementation of the above approach ; Function to count minimum number of removals required to make an interval equal to the union of the given Set ; Stores the minimum number of removals ; Traverse the Set ; Left Boundary ; Right Boundary ; Stores count of intervals lying within current interval ; Traverse over all remaining intervals ; Check if interval lies within the current interval ; Increase count ; Update minimum removals required ; Driver Code ; Returns the minimum removals
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDeletions ( vector < pair < int , int > > & v , int n ) { int minDel = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int L = v [ i ] . first ; int R = v [ i ] . second ; int Count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j ] . first >= L && v [ j ] . second <= R ) { Count += 1 ; } } minDel = min ( minDel , n - Count ) ; } return minDel ; } int main ( ) { vector < pair < int , int > > v ; v . push_back ( { 1 , 3 } ) ; v . push_back ( { 4 , 12 } ) ; v . push_back ( { 5 , 8 } ) ; v . push_back ( { 13 , 20 } ) ; int N = v . size ( ) ; cout << findMinDeletions ( v , N ) ; return 0 ; }