<p>You are given an array of points in the <strong>X-Y</strong> plane <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>

<p>Return <em>the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes</em>. If there is not any such rectangle, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/rec1.JPG" style="width: 500px; height: 447px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/rec2.JPG" style="width: 500px; height: 477px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
<strong>Output:</strong> 2
</pre>

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

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

class Solution {
    map<int, vector<int>> xAxis;
    // map<int, vector<int>> yAxis;
public:
    int minAreaRect(vector<vector<int>>& points) {
        xAxis.clear();
        // yAxis.clear();

        int sz = points.size();

        for(int i = 0; i < sz; i++){
            xAxis[points[i][0]].push_back(points[i][1]);
            // yAxis[points[i][1]].push_back(points[i][0]);
        }
        vector<int> temp;

        for(auto x: xAxis){
            temp = x.second;
            sort(temp.begin(), temp.end());
            xAxis[x.first]= temp;
        }
        vector<int> y1Arr;
        vector<int> y2Arr;

        int x1, x2, y1, y2, i, j, sz1, sz2, yd, xd, area;

        area = 2147483647;        
        vector<int> yc;

        for(auto x1ent: xAxis) {
            x1 = x1ent.first;
            y1Arr = x1ent.second;
            for(auto x2ent: xAxis){
                x2 = x2ent.first;
                if(x1 == x2) continue;
                y2Arr = x2ent.second;
                
                yc.clear();
                i = j = 0;
                sz1 = y1Arr.size();
                sz2 = y2Arr.size();
                for(; i < sz1 && j < sz2; ){
                    // cout<<" - "<< y1Arr[i] <<" "<< y2Arr[j]<<"\n";
                    if(y1Arr[i] == y2Arr[j]){
                        yc.push_back(y1Arr[i]);
                        i++;
                        j++;
                    } else if(y1Arr[i] < y2Arr[j]) {
                        i++;
                    } else {
                        j++;
                    }
                }
                // cout<<x1<<" * "<<x2<<" : ";
                for(int y1 = 0; y1<yc.size(); y1++)
                    // cout<<yc[y1]<<" ";
                
                if(yc.size() > 1) {
                    xd = abs(x1-x2);
                    yd = 2147483647;
                    // cout<<" ++ ";
                    for(int y1 = 1; y1 < yc.size(); y1++){

                        yd = min(yd, (yc[y1] - yc[y1-1]));
                        // cout<<yd<<" "<<xd;
                    }
                    area = min(area, yd*xd);
                    // cout<<" => "<< area;
                }
            // cout<<"\n";
            }
        }
        if(area == 2147483647) return 0;
        return area;
    }
};
156465