Given two arrays **start[]** and **end[]** consisting of positive integers denoting the starting and ending points of a segment respectively, the task is to find the minimum number of integers which lies in at least one of the given segments and each segment contains at least one of them.

**Examples:**

Input:start[] = {1, 2, 3}, end[] = { 3, 5, 6}

Output:3

Explanation:

All three ranges ([1, 3], [2, 5], [3, 6]) contains the integer 3.

Input:start[] = {4, 1, 2, 5}, end[] = {7, 3, 5, 6}

Output:3 6

Explanation:

Segments {1, 3} and {2, 5} are contains the integer 3.

Segments {4, 7} and {5, 6} contains the integer 6.

**Mathematical formulation:**

The mathematical way of describing the problem is to consider each given range of integers to be a line segment defined by two integer coordinates **[a _{i}, b_{i}] **on a line. Then the minimum number of integers required to cover each of the given range is the minimum number of points such that each segment contains at least one point.

The representation of

**Example 1**is shown below:

**Naive approach:**

The simplest way to solve the problem is to find the least value of all the starting points and maximum value of all ending points of all segments. Iterate over this range, and for each point in this range keep track of the number of segments which can be covered using this point. Use an array to store the number of segments as:

**arr[point] = number of segments that can be covered using this point**

- Find the maximum value in the array
**arr[]**. - If this maximum value is equal to N, the index corresponding to this value is the point which covers all segments.
- If this maximum value is less than
**N**, then the index corresponding to this value is a point which covers some segments. - Repeat the steps 1 to 3 for array arr[] excluding this maximum value until the sum of all the maximum values found is equal to N.

**Time Complexity:** *O((A-B+1)*N)*, where A is maximum of ending points of segments and B is the minimum of the starting points of the segments.

**Auxiliary Space:** *O(1)*

**Efficient Approach:**

The problem can be solved efficiently by using the Greedy Technique. Follow the steps given below to solve the problem:

**Sort**the segments by their end points.- Select the point(or coordinate) corresponding to
**minimum**end point of all segments. - Now, All the segments whose starting point are less than
**this selected point**and whose ending points are greater than this selected point can be**covered**by this point. - Then print the minimum number of points.

Below is the implementation of the above approach:

## Python3

`# python program for the above approach ` ` ` `# Function to compute minimum number ` `# of points which cover all segments ` `def` `minPoints(points): ` ` ` ` ` `# Sort the list of tuples by ` ` ` `# their second element. ` ` ` `points.sort(key ` `=` `lambda` `x: x[` `1` `]) ` ` ` ` ` `# To store the solution ` ` ` `cordinates ` `=` `[] ` ` ` `i ` `=` `0` ` ` ` ` `# Iterate over all the segments ` ` ` `while` `i < n: ` ` ` ` ` `seg ` `=` `points[i][` `1` `] ` ` ` `cordinates.append(seg) ` ` ` `p ` `=` `i ` `+` `1` ` ` ` ` `if` `p >` `=` `n: ` ` ` `break` ` ` ` ` `# Get the start point of next segment ` ` ` `arrived ` `=` `points[p][` `0` `] ` ` ` ` ` `# Loop over all those segements whose ` ` ` `# start point is less than the end ` ` ` `# point of current segment ` ` ` `while` `seg >` `=` `arrived: ` ` ` ` ` `p ` `+` `=` `1` ` ` `if` `p >` `=` `n: ` ` ` `break` ` ` `arrived ` `=` `points[p][` `0` `] ` ` ` `i ` `=` `p ` ` ` `# Print the possibles values of M ` ` ` `for` `point ` `in` `cordinates: ` ` ` `print` `(point, end ` `=` `" "` `) ` ` ` ` ` `# Driver Code ` `n ` `=` `4` ` ` `# Starting points of segments ` `start ` `=` `[` `4` `, ` `1` `, ` `2` `, ` `5` `] ` ` ` `# Ending points of segments ` `end ` `=` `[` `7` `, ` `3` `, ` `5` `, ` `6` `] ` ` ` `points ` `=` `[] ` ` ` `# Insert ranges in points[] ` `for` `i ` `in` `range` `(n): ` ` ` `tu ` `=` `(start[i], end[i]) ` ` ` `points.append(tu) ` ` ` `# Function Call ` `minPoints(points) ` |

*chevron_right*

*filter_none*

**Output:**

3 6

**Time Complexity:** *O(N*log N)*

**Auxiliary Space:** *O(N)*

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.

## Recommended Posts:

- Minimum number of towers required such that every house is in the range of at least one tower
- Convert a number of length N such that it contains any one digit at least 'K' times
- Find two numbers with sum N such that neither of them contains digit K
- Count possible splits of sum N into K integers such that the minimum is at least P
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Minimum flips required in a binary string such that all K-size substring contains 1
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Find the largest interval that contains exactly one of the given N integers.
- Modify the string such that it contains all vowels at least once
- Sub-strings that start and end with one character and have at least one other
- Split N into two integers whose addition to A and B makes them equal
- Sort the strings based on the numbers of matchsticks required to represent them
- Sort numbers based on count of letters required to represent them in words
- Minimum number of integers required to fill the NxM grid
- Count pairs in an array such that frequency of one is at least value of other
- Ways to choose balls such that at least one ball is chosen
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Minimum increments by index value required to obtain at least two equal Array elements
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.