<p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= points.length &lt;= 300</code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>-10<sup>4</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
	<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>

class Solution {

public:
// Function to calculate the greatest common divisor (GCD)
int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

// Function to create a string representation of a line using slope and intercept
string getLineKey(pair<int, int> p1, pair<int, int> p2) {
    int dx = p2.first - p1.first;
    int dy = p2.second - p1.second;

    if (dx == 0) {  // Vertical line, slope is undefined
        return "inf_" + to_string(p1.first); // x = constant (vertical line)
    }

    int g = gcd(dx, dy);  // Simplify the slope
    dx /= g;
    dy /= g;

    // Normalize slope direction
    if (dx < 0) {
        dx = -dx;
        dy = -dy;
    }

    // Calculate intercept using y = mx + b -> b = y - m * x
    int intercept_numerator = p1.second * dx - p1.first * dy;
    int intercept_denominator = dx;

    // Simplify the intercept
    int g_intercept = gcd(intercept_numerator, intercept_denominator);
    intercept_numerator /= g_intercept;
    intercept_denominator /= g_intercept;

    return to_string(dy) + "/" + to_string(dx) + "_" + to_string(intercept_numerator) + "/" + to_string(intercept_denominator);
}

// Function to find the maximum number of points that lie on the same line
int maxPoints(vector<vector<int>>& points) {
    if (points.size() <= 2) return points.size();

    int max_points = 1;

    for (int i = 0; i < points.size(); i++) {
        unordered_map<string, int> line_count;
        int duplicates = 1;
        int cur_max = 0;

        for (int j = i + 1; j < points.size(); j++) {
            if (points[i] == points[j]) {
                duplicates++;  // Count duplicates
            } else {
                string line_key = getLineKey({points[i][0], points[i][1]}, {points[j][0], points[j][1]});
                line_count[line_key]++;
                cur_max = max(cur_max, line_count[line_key]);
            }
        }

        max_points = max(max_points, cur_max + duplicates);
    }

    return max_points;
}

};
25566