Number of Boomerangs

2017/12/13 posted in  leetcode

class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int res = 0;
        for (int i =0; i<points.size(); i++) {
            unordered_map<long, int> map(points.size());
            for (int j = 0; j<points.size(); j++) {
                if (j==i) continue;
                int dy = points[j].second - points[i].second;
                int dx = points[j].first - points[i].first;

                int dis = dy*dy + dx*dx;
                map[dis]++;
            }

            for (auto p : map) {
                if (p.second>1) {
                    /*
                     * for all the groups of points,
                     * number of ways to select 2 from n =
                     * nP2 = n!/(n - 2)! = n * (n - 1)
                     */
                    res+=p.second*(p.second-1);
                }
            }
        }
        return res;
    }
};