The Wayback Machine - https://web.archive.org/web/20210720113845/https://www.geeksforgeeks.org/check-if-n-given-lines-can-be-intersected-by-k-vertical-lines/
Skip to content
Related Articles

Related Articles

Improve Article

Check if N given lines can be intersected by K vertical lines

  • Last Updated : 20 Jul, 2021

Given N horizontal lines represented by an array position[][] of size N, where position[i] represents the ith horizontal line which has x-coordinates from position[i][0] to position[i][1] and an integer K, which represents the maximum number of vertical lines that can be drawn, the task is to check if N given lines can be intersected by at most K vertical lines.

Examples:

Input: N = 4, K = 2, position[][] = [[1, 4], [6, 8], [7, 9], [10, 15]]
Output: NO
Explanation: In the given example, draw lines at [1, 7, 10]. The line drawn at 1 will intersect the first line, the line at position 7 will intersect both the second and third lines, and the line drawn at 10 will intersect the fourth line. Hence, a minimum of 3 rods is required. Hence, it is not possible

Input: N = 5, K = 3, position[][] = [[1, 6], [3, 5], [2, 4], [8, 12], [10, 24]]
Output : YES

Approach : The idea is to use greedy approach to solve this problem by sorting the positions[][] array and, with 1 line, intersect as many lines as possible and so on. Follow the steps below to solve the problem:



  • Sort the array position[][] in ascending order.
  • Initialize the variables ans as 1 to store the answer and r as position[0][1] to store the end point till a particular point other horizontal lines can be for intersection with the given considered vertical line.
  • Iterate over the range [1, N] using the variable, say I, and perform the following steps:
    • If position[i][0] is less than r, then set the value of r to the minimum of r or position[i][1].
    • Otherwise, add the value of ans by 1 and set the value of r as position[i][1].
  • If k is greater than equal to ans, then print “YES” and print “NO” otherwise.

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 check if it is possible
// to intersect n lines using k vertical lines
void findIfPossible(int n, int k,
                    vector<vector<int> > position)
{
  
    sort(position.begin(), position.end());
  
    int ans = 1;
  
    // Track the point till a particular
    // vertical line can intersect
    int r = position[0][1];
  
    // Iterate over the range
    for (int i = 1; i < n; i++) {
  
        if (position[i][0] <= r) {
  
            r = min(r, position[i][1]);
        }
  
        else {
  
            ans++;
  
            r = position[i][1];
        }
    }
    if (k >= ans)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
  
// Driver Code
int main()
{
  
    int n = 5;
    int k = 2;
    vector<vector<int> > position = {
        { 2, 5 }, { 4, 6 }, { 7, 16 }, { 9, 10 }, { 10, 17 }
    };
  
    findIfPossible(n, k, position);
  
    return 0;
}
Output:
YES

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

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :