Open In App

Minimum removals required to make any interval equal to the union of the given Set

Last Updated : 30 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a set S of size N (1 ? N ? 1e5) consisting of intervals, the task is to find the minimum intervals required to be removed from the set such that any one of the remaining intervals becomes equal to the union of this set.

Examples: 

Input: S = {[1, 3], [4, 12], [5, 8], [13, 20]}
Output: 2
Explanation: Removing the intervals [1, 3] and [13, 20] modifies the set to { [4, 12], [5, 8]}. The interval [4, 12] is the union of the set.

Input : S = {[1, 2], [1, 10], [4, 8], [3, 7]}
Output: 0
Explanation: Union of the given set = {[1, 10]}, which is already present in the set. Therefore, no removals required.

Approach : The problem can be solved based on the following observation:

Observation: To make any interval equal to the union of the set, the set must contain an interval [L, R] such that all the remaining intervals have their left boundary greater than equal to L and right boundary less than equal to R.

Follow the steps below to solve the problem:

  1. Traverse the given set of intervals.
  2. For every interval in the Set, find all the intervals which have their left boundary greater than or equal to its left boundary as well as have their right boundary less than or equal to its right boundary. Store the count of such intervals in a variable, say Count.
  3. Find the minimum value of all N – Count (since N – Count would give the number of intervals deleted)values for each interval.
  4. Print the minimum value obtained as the required answer.

Below is the implementation of above approach :

C++




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


Java




// Java implementation of the above approach
import java.util.*;
class GFG{
 
// Function to count minimum number of removals
// required to make an interval equal to the
// union of the given Set
static int findMinDeletions(int [][]v,
                            int n)
{
   
    // Stores the minimum number of removals
    int minDel = Integer.MAX_VALUE;
 
    // Traverse the Set
    for (int i = 0; i < n; i++)
    {
 
        // Left Boundary
        int L = v[i][0];
 
        // Right Boundary
        int R = v[i][1];
 
        // Stores count of intervals
        // lying within current interval
        int Count = 0;
 
        // Traverse over all remaining intervals
        for (int j = 0; j < n; j++)
        {
 
            // Check if interval lies within
            // the current interval
            if (v[j][0] >= L && v[j][1] <= R)
            {
 
                // Increase count
                Count += 1;
            }
        }
 
        // Update minimum removals required
        minDel = Math.min(minDel, n - Count);
    }
    return minDel;
}
 
// Driver Code
public static void main(String[] args)
{
    int [][]v = {{ 1, 3 },
                 { 4, 12 },
                 { 5, 8 },
                 { 13, 20 }};
 
    int N = v.length;
 
    // Returns the minimum removals
    System.out.print(findMinDeletions(v, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 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
def findMinDeletions(v, n):
   
    # Stores the minimum number of removals
    minDel = 10**18
 
    # Traverse the Set
    for i in range(n):
 
        # Left Boundary
        L = v[i][0]
 
        # Right Boundary
        R = v[i][1]
 
        # Stores count of intervals
        # lying within current interval
        Count = 0
 
        # Traverse over all remaining intervals
        for j in range(n):
 
            # Check if interval lies within
            # the current interval
            if (v[j][1] >= L and v[j][0] <= R):
                 
                # Increase count
                Count += 1
 
        # Update minimum removals required
        minDel = min(minDel, n - Count)
    return minDel
 
# Driver Code
if __name__ == '__main__':
    v = []
    v.append([1, 3])
    v.append([4, 12])
    v.append([5, 8])
    v.append([13, 2])
 
    N = len(v)
 
    # Returns the minimum removals
    print (findMinDeletions(v, N))
 
# This code is contributed by mohit kumar 29


C#




// C# implementation of the above approach
using System;
 
public class GFG{
 
// Function to count minimum number of removals
// required to make an interval equal to the
// union of the given Set
static int findMinDeletions(int [,]v,
                            int n)
{
   
    // Stores the minimum number of removals
    int minDel = int.MaxValue;
 
    // Traverse the Set
    for (int i = 0; i < n; i++)
    {
 
        // Left Boundary
        int L = v[i,0];
 
        // Right Boundary
        int R = v[i,1];
 
        // Stores count of intervals
        // lying within current interval
        int Count = 0;
 
        // Traverse over all remaining intervals
        for (int j = 0; j < n; j++)
        {
 
            // Check if interval lies within
            // the current interval
            if (v[j,0] >= L && v[j,1] <= R)
            {
 
                // Increase count
                Count += 1;
            }
        }
 
        // Update minimum removals required
        minDel = Math.Min(minDel, n - Count);
    }
    return minDel;
}
 
// Driver Code
public static void Main(String[] args)
{
    int [,]v = {{ 1, 3 },
                 { 4, 12 },
                 { 5, 8 },
                 { 13, 20 }};
 
    int N = v.GetLength(0);
 
    // Returns the minimum removals
    Console.Write(findMinDeletions(v, N));
}
}
 
  
 
// This code is contributed by 29AjayKumar


Javascript




<script>
// Javascript program to implement
// the above approach
 
// Function to count minimum number of removals
// required to make an interval equal to the
// union of the given Set
function findMinDeletions(v, n)
{
    
    // Stores the minimum number of removals
    let minDel = Number.MAX_VALUE;
  
    // Traverse the Set
    for (let i = 0; i < n; i++)
    {
  
        // Left Boundary
        let L = v[i][0];
  
        // Right Boundary
        let R = v[i][1];
  
        // Stores count of intervals
        // lying within current interval
        let Count = 0;
  
        // Traverse over all remaining intervals
        for (let j = 0; j < n; j++)
        {
  
            // Check if interval lies within
            // the current interval
            if (v[j][0] >= L && v[j][1] <= R)
            {
  
                // Increase count
                Count += 1;
            }
        }
  
        // Update minimum removals required
        minDel = Math.min(minDel, n - Count);
    }
    return minDel;
}
 
// Driver Code
 
    let v = [[ 1, 3 ],
                 [ 4, 12 ],
                 [ 5, 8 ],
                 [ 13, 20 ]];
  
    let N = v.length;
  
    // Returns the minimum removals
    document.write(findMinDeletions(v, N));
  
 // This code is contributed by souravghosh0416.
</script>


  

Output: 

2

 

Time Complexity: O(N2), since two nested loops are used.
Auxiliary space: O(1), since no extra array is used so the space taken by the algorithm is constant



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads