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 : 2
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 <bits/stdc++.h>using namespace std;// Comparator functionbool 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 segmentsint cntMinSteps(vector<vector<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 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 Codeint main() { vector<vector<int> > 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 approachimport java.util.*;class GFG{// Function to sort by columnpublic 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 segmentsstatic 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 Codepublic 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 functiondef comp(x, y): return x[1] < y[1] # Function to count the# minimum number of steps# required to delete all# the segmentsdef 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 Codeif __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 |
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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


