VOOZH about

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

⇱ Find Maximum Number of Intersections on the Chart - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find Maximum Number of Intersections on the Chart

Last Updated : 11 May, 2024

Given a line chart with n points connected by line segments and a 1-indexed integer array y[], where the ith point has coordinates (i, y[i]). There are no horizontal lines, meaning no two consecutive points have the same y-coordinate. The task is to find the maximum number of points of intersection of an infinitely long horizontal line with the chart.

Example:


👁 drdrawio

Input: y = [1,2,1,2,1,3,2]
Output: 5
Explanation: As you can see in the image above, the line y = 1.5 has 5 intersections with the chart (in red crosses). You can also see the line y = 2 which intersects the chart in 4 points (in red crosses). It can be shown that there is no horizontal line intersecting the chart at more than 5 points. So the answer would be 5.

Input: y = [2,1,3,4,5]
Output: 2
Explanation: As you can see in the image above, the line y = 1.5 has 2 intersections with the chart (in red crosses). You can also see the line y = 2 which intersects the chart in 2 points (in red crosses). It can be shown that there is no horizontal line intersecting the chart at more than 2 points. So the answer would be 2.

Approach:

Create and ordered map of + and - with the coordinates. Then we can sweep through the map from least to greatest while keeping a running sum which is equal to the number of intersections of a horizontal line at the current y coordinate.

Trick is to multiply each number by 2 to create space after each number; we need this if we want to avoid using floating points, because for example, if we have test case {2,1,3}, we need to have a -1 between 2 and 3.

Steps to solve this problem:

  • Multiply each element in vector 'y' by 2 to create spaces between numbers.
  • Create a map to store the count of each y-coordinate.
  • Initialize the previous slope to 0.
  • Add the first y-coordinate to the map with a count of 1, and the next y-coordinate with a count of -1.
  • Iterate over the remaining y-coordinates:
    • Calculate the slope between the current and previous y-coordinates.
    • If the slope has changed and the previous slope was not 0:
      • If the slope is positive, increment the count of the y-coordinate at the bottom of the valley and decrement the count of the y-coordinate at the top of the valley.
      • If the slope is negative, decrement the count of the y-coordinate at the bottom of the hill and increment the count of the y-coordinate at the top of the hill.
    • If the slope remains the same:
      • If the slope is negative, decrement the count of the y-coordinate at the top of the line and increment the count of the y-coordinate at the bottom of the line.
      • If the slope is positive, increment the count of the y-coordinate at the top of the line and decrement the count of the y-coordinate at the bottom of the line.
  • Update the previous slope.
  • Initialize the answer to -1 and current count to 0.
  • Iterate over the map and update the answer to the maximum of the current answer and the current count.
  • Return the answer.

Below is the Implementation of above approach:


Output
5

Time complexity: O(nlog⁡(n))
Space complexity: O(n)

Comment
Article Tags: