Rectangle Mania Time : O(n^2) Space : O(n) 123456789101112131415161718192021222324252627282930313233343536#include <vector>using namespace std;int intersection(vector<int>& A, vector<int>& B) { int res = 0, i = 0, j = 0; while(i < A.size() and j < B.size()) { if(A[i] == B[j]) { res++,i++,j++; } else if(A[i] < B[j]) { i++; } else { j++; } } return res;}int rectangleMania(vector<vector<int>> coords) { unordered_map<int, vector<int>> mp; int res = 0; for(auto& coord: coords) { mp[coord[0]].push_back(coord[1]); } for(auto& [_, xaxis] : mp) { sort(begin(xaxis), end(xaxis)); } for(auto i = begin(mp); i != end(mp); i++) { for(auto j = next(i); j != end(mp); j++) { int intersectCount = intersection(i->second, j->second); res += (intersectCount) * (intersectCount - 1) / 2; } } return res;}