Minimum removals required to make any interval equal to the union of the given Set
Last Updated :
30 Aug, 2022
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:
- Traverse the given set of intervals.
- 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.
- Find the minimum value of all N – Count (since N – Count would give the number of intervals deleted)values for each interval.
- Print the minimum value obtained as the required answer.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG{
static int findMinDeletions( int [][]v,
int n)
{
int minDel = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
{
int L = v[i][ 0 ];
int R = v[i][ 1 ];
int Count = 0 ;
for ( int j = 0 ; j < n; j++)
{
if (v[j][ 0 ] >= L && v[j][ 1 ] <= R)
{
Count += 1 ;
}
}
minDel = Math.min(minDel, n - Count);
}
return minDel;
}
public static void main(String[] args)
{
int [][]v = {{ 1 , 3 },
{ 4 , 12 },
{ 5 , 8 },
{ 13 , 20 }};
int N = v.length;
System.out.print(findMinDeletions(v, N));
}
}
|
Python3
def findMinDeletions(v, n):
minDel = 10 * * 18
for i in range (n):
L = v[i][ 0 ]
R = v[i][ 1 ]
Count = 0
for j in range (n):
if (v[j][ 1 ] > = L and v[j][ 0 ] < = R):
Count + = 1
minDel = min (minDel, n - Count)
return minDel
if __name__ = = '__main__' :
v = []
v.append([ 1 , 3 ])
v.append([ 4 , 12 ])
v.append([ 5 , 8 ])
v.append([ 13 , 2 ])
N = len (v)
print (findMinDeletions(v, N))
|
C#
using System;
public class GFG{
static int findMinDeletions( int [,]v,
int n)
{
int minDel = int .MaxValue;
for ( int i = 0; i < n; i++)
{
int L = v[i,0];
int R = v[i,1];
int Count = 0;
for ( int j = 0; j < n; j++)
{
if (v[j,0] >= L && v[j,1] <= R)
{
Count += 1;
}
}
minDel = Math.Min(minDel, n - Count);
}
return minDel;
}
public static void Main(String[] args)
{
int [,]v = {{ 1, 3 },
{ 4, 12 },
{ 5, 8 },
{ 13, 20 }};
int N = v.GetLength(0);
Console.Write(findMinDeletions(v, N));
}
}
|
Javascript
<script>
function findMinDeletions(v, n)
{
let minDel = Number.MAX_VALUE;
for (let i = 0; i < n; i++)
{
let L = v[i][0];
let R = v[i][1];
let Count = 0;
for (let j = 0; j < n; j++)
{
if (v[j][0] >= L && v[j][1] <= R)
{
Count += 1;
}
}
minDel = Math.min(minDel, n - Count);
}
return minDel;
}
let v = [[ 1, 3 ],
[ 4, 12 ],
[ 5, 8 ],
[ 13, 20 ]];
let N = v.length;
document.write(findMinDeletions(v, N));
</script>
|
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
Please Login to comment...