# Count of Squares that are parallel to the coordinate axis from the given set of N points

Given an array of points points[] in a cartesian coordinate system, the task is to find the count of the squares that are parallel to the coordinate axis.

Examples:

Input:points[] = {(0, 0), (0, 2), (2, 0), (2, 2), (1, 1)}
Output: 1
Explanation:
As the points (0, 0), (0, 2), (2, 0), (2, 2) forms square which is parallel to the X-axis and Y-axis, Hence the count of such squares is 1.

Input:points[] = {(2, 0), (0, 2), (2, 2), (0, 0), (-2, 2), (-2, 0)}
Output: 2
Explanation:
As the points (0, 0), (0, 2), (2, 0), (2, 2) forms one square, whereas points (0, 0), (0, 2), (-2, 0), (-2, 2) forms other square which is parallel to the X-axis and Y-axis,
Hence the count of such squares is 2.

Approach: The idea is to choose two points from the array of points such that these two points are parallel to co-ordinate axis and then find other two points of the square with the help of the distance between the points. If those points exist in the array then, there is one such possible square.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find count of Squares ` `// that are parallel to the coordinate axis ` `// from the given set of N points ` ` `  `#include ` `using` `namespace` `std; ` ` `  `#define sz(x) int(x.size()) ` ` `  `// Function to get distance ` `// between two points ` `int` `get_dis(pair<``int``, ``int``> p1, ` `            ``pair<``int``, ``int``> p2) ` `{ ` `    ``int` `a = ``abs``(p1.first - p2.first); ` `    ``int` `b = ``abs``(p1.second - p2.second); ` `    ``return` `((a * a) + (b * b)); ` `} ` ` `  `// Function to check that points ` `// forms a square and parallel to ` `// the co-ordinate axis ` `bool` `check(pair<``int``, ``int``> p1, ` `           ``pair<``int``, ``int``> p2, ` `           ``pair<``int``, ``int``> p3, ` `           ``pair<``int``, ``int``> p4) ` `{ ` ` `  `    ``int` `d2 = get_dis(p1, p2); ` `    ``int` `d3 = get_dis(p1, p3); ` `    ``int` `d4 = get_dis(p1, p4); ` `    ``if` `(d2 == d3 ` `        ``&& 2 * d2 == d4 ` `        ``&& 2 * get_dis(p2, p4) == get_dis(p2, p3)) { ` `        ``return` `true``; ` `    ``} ` `    ``if` `(d3 == d4 ` `        ``&& 2 * d3 == d2 ` `        ``&& 2 * get_dis(p3, p2) == get_dis(p3, p4)) { ` `        ``return` `true``; ` `    ``} ` `    ``if` `(d2 == d4 ` `        ``&& 2 * d2 == d3 ` `        ``&& 2 * get_dis(p2, p3) == get_dis(p2, p4)) { ` `        ``return` `true``; ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Function to find all the squares which is ` `// parallel to co-ordinate axis ` `int` `count(map, ``int``> hash, ` `          ``vector > v, ``int` `n) ` `{ ` `    ``int` `ans = 0; ` `    ``map, ``int``> vis; ` ` `  `    ``// Loop to choose two points ` `    ``// from the array of points ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``for` `(``int` `j = 0; j < n; j++) { ` `            ``if` `(i == j) ` `                ``continue``; ` `            ``pair<``int``, ``int``> p1 ` `                ``= make_pair(v[i].first, ` `                            ``v[j].second); ` `            ``pair<``int``, ``int``> p2 ` `                ``= make_pair(v[j].first, ` `                            ``v[i].second); ` `            ``set > s; ` `            ``s.insert(v[i]); ` `            ``s.insert(v[j]); ` `            ``s.insert(p1); ` `            ``s.insert(p2); ` `            ``if` `(sz(s) != 4) ` `                ``continue``; ` ` `  `            ``// Condition to check if the ` `            ``// other points are present in the map ` `            ``if` `(hash.find(p1) != hash.end() ` `                ``&& hash.find(p2) != hash.end()) { ` `                ``if` `((!vis[v[i]] || !vis[v[j]] ` `                     ``|| !vis[p1] || !vis[p2]) ` `                    ``&& (check(v[i], v[j], p1, p2))) { ` ` `  `                    ``vis[v[i]] = 1; ` `                    ``vis[v[j]] = 1; ` `                    ``vis[p1] = 1; ` `                    ``vis[p2] = 1; ` `                    ``ans++; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` `    ``cout << ans; ` `    ``return` `ans; ` `} ` ` `  `// Function to Count the number of squares ` `void` `countOfSquares(vector > v, ``int` `n) ` `{ ` `    ``ios_base::sync_with_stdio(0); ` `    ``cin.tie(0); ` ` `  `    ``map, ``int``> hash; ` ` `  `    ``// Declaring iterator to a vector ` `    ``vector >::iterator ptr; ` ` `  `    ``// Adding the points to hash ` `    ``for` `(ptr = v.begin(); ptr < v.end(); ptr++) ` `        ``hash[*ptr] = 1; ` ` `  `    ``// Count the number of squares ` `    ``count(hash, v, n); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``int` `n = 5; ` `    ``vector > v; ` `    ``v.push_back(make_pair(0, 0)); ` `    ``v.push_back(make_pair(0, 2)); ` `    ``v.push_back(make_pair(2, 0)); ` `    ``v.push_back(make_pair(2, 2)); ` `    ``v.push_back(make_pair(0, 1)); ` ` `  `    ``// Function call ` `    ``countOfSquares(v, n); ` `    ``return` `0; ` `} `

Output:

```1
```

Performance Analysis:

• Time Complexity: As in the above approach, there are two loops which takes O(N2) time, Hence the Time Complexity will be O(N2).
• Auxiliary Space Complexity: As in the above approach, there is extra space used, Hence the auxiliary space complexity will be O(N).

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.