Detect Squares
From LeetCode. Count axis-aligned squares. Solve the geometry problem "Detect Squares".
Examples
Input: [1,2,3]
Output: 0
Input: [2,3,4]
Output: 0
Hints
For add(point), store it in a hash map keyed by x-coordinate, each mapping to a nested map of y → count. This lets you quickly find points sharing the same x.
For count(query point (qx, qy)), iterate over all points (px, py) that share the same x as the query. For each, the side length s = |qy - py|. If s > 0, the other two corners of the square are (qx + s, qy) & (qx + s, py), and also (qx - s, qy) & (qx - s, py).
Multiply the counts at the two opposite corners to get the number of squares for that diagonal pair. Sum over all candidates. Each point counts independently, even if multiple points share the same coordinate.
Detect Squares
**From LeetCode.** Count axis-aligned squares. Solve the geometry problem "Detect Squares".