Max Points On A Line
To solve this coding challenge, we need to determine the maximum number of points that lie on the same straight line. The key to solving this problem is to use the concept of slopes between points. By comparing slopes, we can identify collinear points (points that lie on the same straight line). Here's how you can approach this problem:
# **Explanation**
- Initialization :
- First, if the number of points is less than or equal to 2, we return the number of points directly, as any two points are always collinear.
- Iterate through each point :
- For each point, treat that point as an anchor or reference point.
- Calculate Slopes :
- For each anchor point, calculate the slope it forms with every other point.
-
Slopes can be calculated using the formula
, but to avoid division and to handle vertical lines, use the difference in y-coordinates and x-coordinates directly. We can use a hash map (or dictionary) to store the frequency of each unique slope.
(y2 - y1) / (x2 - x1) - Handle Edge Cases :
-
Account for vertical lines where the x-coordinates are the same. These slopes can be marked distinctly (e.g., using a special value like
).
infinity - Handle duplicate points separately and count them only once for slope calculation but add them to the final count for the anchor point.
- Track Maximum Points :
- For every anchor point, determine the maximum number of points that share the same slope with that anchor.
- Update the global maximum count of collinear points.
- Return Result :
- After processing all points as possible anchor points, return the global maximum count of collinear points.
- Check edge case for small input size :
- If the length of the points array is less than or equal to 2, return the size of the array itself. This handles the scenario where there are one or two points, which are always collinear by default.
- Initialize a variable for the maximum number of points on a line :
-
is initialized to 1, since the minimum number of collinear points includes at least one point itself.
maxPointsOnLine - Loop through each point to use it as an anchor point :
-
For every
point, we consider it as a reference or anchor point.
i-th -
Initialize an empty dictionary (
) to store the frequency of slopes.
slopeCount -
Initialize a counter (
) to handle overlapping points.
samePoints -
Initialize
to track the maximum number of collinear points for the current anchor point.
localMax - Calculate slopes for the current anchor point :
-
For every
point other than the current
j-thpoint:i-th -
If the points are identical (i.e., have the same coordinates), increment the
counter.
samePoints -
Otherwise, compute the difference in x (
) and y (
dx) coordinates.dy -
Calculate the greatest common divisor (GCD) of
and
dxto normalize the slope and avoid precision issues.dy -
Normalize the slope (
) using the GCD.
normalizedSlope -
Increment the frequency of this slope in the
dictionary.
slopeCount -
Update
if the current slope's frequency is higher than the previous maximum for this anchor point.
localMax - Update the global maximum points on a line :
-
After processing all points for the current anchor point, update
by comparing it with
maxPointsOnLine.localMax + samePoints + 1 - Return the result :
- Finally, return the global maximum count of collinear points.
Pseudocode with Comments
// Function to find the maximum number of points on a line
function maxPoints(points):
// If there are less than or equal to 2 points, return the length of points
if length of points <= 2:
return length of points
// Initialize the variable to keep track of maximum points on the same line
maxPointsOnLine = 1
// Iterate through each point as anchor point
for i from 0 to length of points - 1:
// Use a dictionary to store frequency of slopes
slopeCount = empty dictionary
samePoints = 0 // Counter for overlapping points or same points
localMax = 0 // Maximum points for current anchor point
// Compare with every other point
for j from 0 to length of points - 1:
if i != j:
if points[i][0] == points[j][0] and points[i][1] == points[j][1]:
// Count the same points
samePoints += 1
else:
dx = points[j][0] - points[i][0]
dy = points[j][1] - points[i][1]
// Use gcd to avoid floating-point precision issues
gcdValue = gcd(dx, dy)
normalizedSlope = (dy / gcdValue, dx / gcdValue)
// Increment the slope frequency
if normalizedSlope exists in slopeCount:
slopeCount[normalizedSlope] += 1
else:
slopeCount[normalizedSlope] = 1
// Update the local maximum for this anchor point
localMax = max(localMax, slopeCount[normalizedSlope])
// Update the global maximum points on a line
maxPointsOnLine = max(maxPointsOnLine, localMax + samePoints + 1)
return maxPointsOnLine
// Helper function to compute the greatest common divisor
function gcd(a, b):
while b != 0:
temp = b
b = a % b
a = temp
return a