Related Articles
Minimum points to be selected such that removal of line segments passing through them empties given array
• Last Updated : 25 May, 2021

Given a 2D array arr[][], where each row is of the form {start, end} representing the start and endpoints of each line segment on the X-axis. In one step, select a point on the X-axis and delete all the line segments passing through that point. The task is to find the minimum number of such points that need to be selected to delete all the line segments of the array.

Examples:

Input: arr[][]= { {9, 15}, {3, 8}, {1, 6}, {7, 12}, {5,10} }
Output :
Explanation:
Select the point arr[2][1](= (6, 0) on the X-axis and delete the second(= arr[1]), third(= arr[2]), and fifth(= arr[4]) line segments.
Select the point arr[3][1](= (12, 0)) on the X-axis and delete the first(=arr[0]) and the fourth(=arr[3]) line segments.
Therefore, the required count is 2.

Input: arr[][]={ {1, 4}, {5, 7}, {9, 13} }
Output: 3

Approach: The problem can be solved using the Greedy technique. Follow the steps below to solve the problem:

• Initialize a variable, say cntSteps to count total number of steps required to delete all the line segments.
• Sort the array arr[][] based on the end points of the line segments.
• Initialize a variable, say Points = arr[0][1] to store the points of the X-axis.
• Traverse the array and check if the value of arr[i][0] greater than Points or not. If found to be true then increment the value cntSteps by 1 and update the value of Points = arr[i][1].
• Finally, print the value of cntSteps.

## C++

 `// C++ program to implement``// the above approach`  `#include ``using` `namespace` `std;`  `// Comparator function``bool` `comp(vector<``int``> &x, vector<``int``> y)``{``    ``return` `x[1] < y[1];``}`  `// Function to count the minimum number of``// steps required to delete all the segments``int` `cntMinSteps(vector > &arr,``                                   ``int` `N)``{``    `  `    ``// Stores count of steps required``    ``// to delete all the line segments``    ``int` `cntSteps = 1;``    `  `    ``// Sort the array based on end points``    ``// of line segments``    ``sort(arr.begin(), arr.end(), comp);``    `  `    ``// Stores point on X-axis``    ``int` `Points = arr[0][1];``    `  `    ``// Traverse the array``    ``for``(``int` `i = 0; i < N; i++) {``        `  `        ``// If arr[1][0] is``        ``// greater than Points``        ``if``(arr[i][0] > Points) {``            `  `            ``// Update cntSteps``            ``cntSteps++;``            `  `            ``// Update Points``            ``Points = arr[i][1];``        ``}``    ``}``    ` `    ``return` `cntSteps;``    ` `}`  `// Driver Code``int` `main() {``    ` `    ``vector > arr``       ``= { { 9, 15 }, { 3, 8 },``            ``{ 1, 6 }, { 7, 12 },``                      ``{ 5, 10 } };``                            ` `    ``int` `N = arr.size();``    ` `    ``cout<< cntMinSteps(arr, N);``    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;` `class` `GFG{` `// Function to sort by column``public` `static` `void` `sortbyColumn(``int` `arr[][],``                                ``int` `col)``{``    ` `    ``// Using built-in sort function Arrays.sort``    ``Arrays.sort(arr, ``new` `Comparator<``int``[]>()``    ``{``        ``@Override``        ` `        ``// Compare values according to columns``        ``public` `int` `compare(``final` `int``[] entry1, ``                           ``final` `int``[] entry2)``        ``{``            ` `            ``// To sort in descending order revert ``            ``// the '>' Operator``            ``if` `(entry1[col] > entry2[col])``                ``return` `1``;``            ``else``                ``return` `-``1``;``        ``}``    ``});  ``// End of function call sort().``}` `// Function to count the minimum number of``// steps required to delete all the segments``static` `int` `cntMinSteps(``int``[][] arr, ``int` `N)``{``    ` `    ``// Stores count of steps required``    ``// to delete all the line segments``    ``int` `cntSteps = ``1``;``    ` `    ``// Sort the array based on end points``    ``// of line segments``    ``sortbyColumn(arr, ``1``);``    ` `    ``// Stores point on X-axis``    ``int` `Points = arr[``0``][``1``];``    ` `    ``// Traverse the array``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ` `        ``// If arr[1][0] is``        ``// greater than Points``        ``if``(arr[i][``0``] > Points)``        ``{``            ` `            ``// Update cntSteps``            ``cntSteps++;``            ` `            ``// Update Points``            ``Points = arr[i][``1``];``        ``}``    ``}``    ``return` `cntSteps;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int``[][] arr = { { ``9``, ``15` `}, { ``3``, ``8` `},``                    ``{ ``1``, ``6` `}, { ``7``, ``12` `},``                    ``{ ``5``, ``10` `} };``                            ` `    ``int` `N = arr.length;``    ` `    ``System.out.print(cntMinSteps(arr, N));``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python3 program to implement``# the above approach` `# Comparator function``def` `comp(x, y):``    ``return` `x[``1``] < y[``1``]``  ` `# Function to count the``# minimum number of steps``# required to delete all``# the segments``def` `cntMinSteps(arr, N):``  ` `    ``# Stores count of steps``    ``# required to delete all``    ``# the line segments``    ``cntSteps ``=` `1` `    ``# Sort the array based``    ``# on end points of line``    ``# segments``    ``arr.sort(reverse ``=` `False``)   ` `    ``# Stores point on X-axis``    ``Points ``=` `arr[``0``][``1``]   ` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):``      ` `        ``# If arr[1][0] is``        ``# greater than Points``        ``if``(arr[i][``0``] > Points):``          ` `            ``# Update cntSteps``            ``cntSteps ``+``=` `1``            ` `            ``# Update Points``            ``Points ``=` `arr[i][``1``]``    ` `    ``return` `cntSteps` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``  ` `    ``arr ``=` `[[``9``, ``15``], [``3``, ``8``],``           ``[``1``, ``6``], [``7``, ``12``],``           ``[``5``, ``10``]]           ``    ``N ``=` `len``(arr)``    ``print``(cntMinSteps(arr, N))` `# This code is contributed by bgangwar59`
Output:
`2`

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.  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 industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up