# Minimum number of pairs required to be selected to cover a given range

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 :**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

Input:arr[] = {{0, 3}, {2, 5}, {5, 6}, {6, 8}, {6, 10}}, K = 10Output:4Explanation: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 = 10Output :-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(2^{N} × 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])**.

- If
- Modify the value of
**i**as**j-1**and the value of**R**as**maxr**and increment the value of**ans**by 1.

- If
- 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

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to sort the elements in` `// increasing order of left element and` `// sort pairs with equal left element in` `// decreasing order of right element` `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;` ` ` `}` `}` `// Function to select minimum number of pairs` `// such that it covers the entire range [0, K]` `int` `MinimumPairs(vector<pair<` `int` `, ` `int` `> > arr, ` `int` `K)` `{` ` ` `int` `N = arr.size();` ` ` `// Sort the array using comparator` ` ` `sort(arr.begin(), arr.end(), cmp);` ` ` `// If the start element` ` ` `// is not equal to 0` ` ` `if` `(arr[0].first != 0) {` ` ` `return` `-1;` ` ` `}` ` ` `// Stores the minimum` ` ` `// number of pairs required` ` ` `int` `ans = 1;` ` ` `// Stores the right bound of the range` ` ` `int` `R = arr[0].second;` ` ` `// Iterate in the range[0, N-1]` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `if` `(R == K) {` ` ` `break` `;` ` ` `}` ` ` `// Stores the maximum right bound` ` ` `// where left bound ≤ R` ` ` `int` `maxr = R;` ` ` `int` `j;` ` ` `// Iterate in the range [i+1, N-1]` ` ` `for` `(j = i + 1; j < N; j++) {` ` ` `// If the starting point of j-th` ` ` `// element is less than equal to R` ` ` `if` `(arr[j].first <= R) {` ` ` `maxr = max(maxr, arr[j].second);` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `i = j - 1;` ` ` `R = maxr;` ` ` `ans++;` ` ` `}` ` ` `// If the right bound` ` ` `// is not equal to K` ` ` `if` `(R != K) {` ` ` `return` `-1;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `return` `ans;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Input` ` ` `int` `K = 4;` ` ` `vector<pair<` `int` `, ` `int` `> > arr{ { 0, 6 } };` ` ` `// Function Call` ` ` `cout << MinimumPairs(arr, K);` `}` |

## Python3

`# Python3 program for the above approach` `# Function to select minimum number of pairs` `# such that it covers the entire range [0, K]` `def` `MinimumPairs(arr, K):` ` ` ` ` `N ` `=` `len` `(arr)` ` ` `# Sort the array using comparator` ` ` `arr.sort()` ` ` `# If the start element` ` ` `# is not equal to 0` ` ` `if` `(arr[` `0` `][` `0` `] !` `=` `0` `):` ` ` `return` `-` `1` ` ` `# Stores the minimum` ` ` `# number of pairs required` ` ` `ans ` `=` `1` ` ` `# Stores the right bound of the range` ` ` `R ` `=` `arr[` `0` `][` `1` `]` ` ` `# Iterate in the range[0, N-1]` ` ` `for` `i ` `in` `range` `(N):` ` ` `if` `(R ` `=` `=` `K):` ` ` `break` ` ` `# Stores the maximum right bound` ` ` `# where left bound ≤ R` ` ` `maxr ` `=` `R` ` ` `j ` `=` `0` ` ` `# Iterate in the range [i+1, N-1]` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, N, ` `1` `):` ` ` ` ` `# If the starting point of j-th` ` ` `# element is less than equal to R` ` ` `if` `(arr[j][` `0` `] <` `=` `R):` ` ` `maxr ` `=` `max` `(maxr, arr[j][` `1` `])` ` ` `# Otherwise` ` ` `else` `:` ` ` `break` ` ` `i ` `=` `j ` `-` `1` ` ` `R ` `=` `maxr` ` ` `ans ` `+` `=` `1` ` ` `# If the right bound` ` ` `# is not equal to K` ` ` `if` `(R !` `=` `K):` ` ` `return` `-` `1` ` ` `# Otherwise` ` ` `else` `:` ` ` `return` `ans` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given Input` ` ` `K ` `=` `4` ` ` `arr ` `=` `[ [ ` `0` `, ` `6` `] ]` ` ` `# Function Call` ` ` `print` `(MinimumPairs(arr, K))` `# This code is contributed by bgangwar59` |

**Output**

-1

**Time Complexity: **O(NlogN)**Auxiliary Space: **O(1)