Minimum number of pairs required to be selected to cover a given range
Last Updated :
15 Feb, 2023
Given an array arr[] consisting of N pairs and a positive integer K, the task is to select the minimum number of pairs such that it covers the entire range [0, K]. If it is not possible to cover the range [0, K], then print -1.
Examples :
Input: arr[] = {{0, 3}, {2, 5}, {5, 6}, {6, 8}, {6, 10}}, K = 10
Output: 4
Explanation: One of the optimal ways is to select the ranges {{0, 3}, {2, 5}, {5, 6}, {6, 10}} which covers the entire range [0, 10].
Input: arr[] = {{0, 4}, {1, 5}, {5, 6}, {8, 10}}, N = 10
Output : -1
Naive Approach: The simplest approach to solve the problem is to generate all possible subsets and check for each subset, if it covers the entire range or not.
Time Complexity: O(2N × N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by sorting the array in increasing order on the basis of the left element and for pairs with equal left elements, sort the elements in decreasing order of their right element. Now, choose the pairs optimally.
Follow the steps below to solve the problem:
- Sort the vector of pairs arr[] in increasing order of left element and if left elements are equal, then sort the array in decreasing order of right element of pair.
- Check if arr[0][0] != 0 then print -1.
- Initialize a variable, say R as arr[0][1] which stores the right bound of the range and ans as 1 which stores number of ranges needed to cover the range [0, K].
- Iterate in the range [0, N-1] using the variable i and perform the following steps:
- If R == K, terminate the loop.
- Initialize a variable, say maxr as R which stores the maximum right bound where left bound ? R.
- Iterate in the range [i+1, N-1] using the variable j and perform the following steps:
- If arr[j][0] ? R, then modify the value of maxr as max(maxr, arr[j][1]).
- Modify the value of i as j-1 and the value of R as maxr and increment the value of ans by 1.
- If R is not equal to K, then print -1.
- Otherwise, print the value of ans.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
static bool cmp(pair< int , int > a, pair< int , int > b)
{
if (a.first == b.first) {
return a.second > b.second;
}
else {
return b.first > a.first;
}
}
int MinimumPairs(vector<pair< int , int > > arr, int K)
{
int N = arr.size();
sort(arr.begin(), arr.end(), cmp);
if (arr[0].first != 0) {
return -1;
}
int ans = 1;
int R = arr[0].second;
for ( int i = 0; i < N; i++) {
if (R == K) {
break ;
}
int maxr = R;
int j;
for (j = i + 1; j < N; j++) {
if (arr[j].first <= R) {
maxr = max(maxr, arr[j].second);
}
else {
break ;
}
}
i = j - 1;
R = maxr;
ans++;
}
if (R != K) {
return -1;
}
else {
return ans;
}
}
int main()
{
int K = 4;
vector<pair< int , int > > arr{ { 0, 6 } };
cout << MinimumPairs(arr, K);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Pair
{
int x;
int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
class cmp implements Comparator<Pair>
{
public int compare(Pair p1, Pair p2)
{
if (p1.x == p2.x)
{
return p1.y - p2.y;
}
else
{
return p2.x - p1.x;
}
}
}
class GFG
{
public static int MinimumPairs(Pair arr[], int K)
{
int N = arr.length;
Arrays.sort(arr, new cmp());
if (arr[ 0 ].x != 0 )
{
return - 1 ;
}
int ans = 1 ;
int R = arr[ 0 ].y;
for ( int i = 0 ; i < N; i++)
{
if (R == K)
{
break ;
}
int maxr = R;
int j;
for (j = i + 1 ; j < N; j++)
{
if (arr[j].x <= R)
{
maxr = Math.max(maxr, arr[j].y);
}
else
{
break ;
}
}
i = j - 1 ;
R = maxr;
ans++;
}
if (R != K)
{
return - 1 ;
}
else
{
return ans;
}
}
public static void main(String[] args)
{
int K = 4 ;
int n = 1 ;
Pair arr[] = new Pair[n];
arr[ 0 ] = new Pair( 0 , 6 );
System.out.println(MinimumPairs(arr, K));
}
}
|
Python3
def MinimumPairs(arr, K):
N = len (arr)
arr.sort()
if (arr[ 0 ][ 0 ] ! = 0 ):
return - 1
ans = 1
R = arr[ 0 ][ 1 ]
for i in range (N):
if (R = = K):
break
maxr = R
j = 0
for j in range (i + 1 , N, 1 ):
if (arr[j][ 0 ] < = R):
maxr = max (maxr, arr[j][ 1 ])
else :
break
i = j - 1
R = maxr
ans + = 1
if (R ! = K):
return - 1
else :
return ans
if __name__ = = '__main__' :
K = 4
arr = [ [ 0 , 6 ] ]
print (MinimumPairs(arr, K))
|
Javascript
<script>
function MinimumPairs(arr, K){
let N = arr.length
arr.sort((a,b)=>a-b)
if (arr[0][0] != 0)
return -1
let ans = 1
let R = arr[0][1]
for (let i=0;i<N;i++){
if (R == K)
break
let maxr = R
let j = 0
for (j=i+1;j<N;j++){
if (arr[j][0] <= R)
maxr = Math.max(maxr, arr[j][1])
else break
}
i = j - 1
R = maxr
ans += 1
}
if (R != K)
return -1
else
return ans
}
let K = 4
let arr = [ [ 0, 6 ] ]
document.write(MinimumPairs(arr, K), "</br>" )
</script>
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class Pair
{
public int x;
public int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
public class cmp : IComparer<Pair>
{
public int Compare(Pair p1, Pair p2)
{
if (p1.x == p2.x)
{
return p2.y - p1.y;
}
else
{
return p1.x - p2.x;
}
}
}
class GFG
{
public static int MinimumPairs(Pair[] arr, int K)
{
int N = arr.Length;
Array.Sort(arr, new cmp());
if (arr[0].x != 0)
{
return -1;
}
int ans = 1;
int R = arr[0].y;
for ( int i = 0; i < N; i++)
{
if (R == K)
{
break ;
}
int maxr = R;
int j;
for (j = i + 1; j < N; j++)
{
if (arr[j].x <= R)
{
maxr = Math.Max(maxr, arr[j].y);
}
else
{
break ;
}
}
i = j - 1;
R = maxr;
ans++;
}
if (R != K)
{
return -1;
}
else
{
return ans;
}
}
public static void Main( string [] args)
{
int K = 4;
int n = 1;
Pair[] arr = new Pair[n];
arr[0] = new Pair(0, 6);
Console.WriteLine(MinimumPairs(arr, K));
}
}
|
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...