VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-maximum-number-of-intersections-lines/

⇱ Maximum intersection of a horizontal lines with a vertical - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum intersection of a horizontal lines with a vertical

Last Updated : 23 Jul, 2025

Given n horizontal line segments are arranged on the X-axis of a 2D plane. The start and end point of each line segment is given in an nx2 matrix lines[ ][ ], the task is to find the maximum number of intersections possible of any vertical line with the given n line segments.

Examples:

Input: n = 4, lines[][] = [ [ 1, 3 ], [ 2, 3], [ 1, 2 ], [ 4, 4 ] ]
Output: 3
Explanation: A vertical line at X = 2 passes through {1, 3}, {2, 3}, {1, 2}, i.e. three of the given horizontal lines.

Input: n= 3, lines[][] = [ [ 1, 3 ], [ 5, 6 ], [ 3, 4 ] ]
Output: 2
Explanation: A vertical line at X = 3 passes through two of the given horizontal lines which are the maximum possible.

This problem is mainly a variation of popular problem called Minimum Platforms.

[Naive Approach] Using Nested Loop – O(n*m) Time and O(1) Space

This approach is based on independently checking the count of every x-axis point that it lies across how many line segments.

  • Identify the Range of Vertical Lines: Determine the minimum and maximum x-coordinates from the start and end points of all line segments to define the valid range for vertical lines.
  • Check Intersections for Each Vertical Line: For each vertical line within the valid range, check how many line segments intersect with it by comparing the x-coordinate with the start and end points of the segments.
  • Track Maximum Intersections: Keep track of the maximum number of line segments that intersect with any vertical line and return this value as the result.

Output
2

Time complexity is O(n*m) where n is the size of list and m is the difference between the max and min x points.

[Better Approach] Using Sorting and Two Pointer – O(n log n) Time and O(n) Space

In this approach, we first sort the start and end points of the line segments. We then use two pointers: one for the start points and one for the end points. If the start point is less than or equal to the end point, it means a new line is starting, so we increase the count of intersections. If the start point is greater than the end point, it means a line has ended, so we decrease the intersection count. At each step, we check and update the maximum number of intersections. This method efficiently tracks the intersections without having to check every single point on the x-axis.

  • Sort Start and End Points: The start and end points of all the line segments are sorted to efficiently process the intersections.
  • Two Pointer Technique: Traverse through the sorted start and end points using two pointers. Increment the intersection count when a new line starts and decrement when a line ends.
  • Track Maximum Intersections: Continuously update the maximum number of active intersections during the traversal and return the result.

Output
3


[Expected Approach] Using Hash Map – O(n) Time and O(n) Space

Instead of checking all x-axis points, we focus only on the start and end points of the line segments. We use a map to track these points: incrementing the count when a line starts and decrementing it when a line ends. After processing all points, we simply check the map to find the point with the highest count, which gives the maximum number of intersections. This method is efficient as we only consider the relevant points.

  • Create Events: Treat the start of a line as an increment and the end (adjusted by +1) as a decrement to track active line segments.
  • Use of Map: Store events in a sorted map where keys are x-coordinates and values are the count of active lines.
  • Process Events: Traverse the events, updating the active count at each x-coordinate and track the maximum intersections.

Output
2
Comment
Article Tags:
Article Tags: