Maximum Squares possible parallel to both axes from N distinct points | C ++ implementation of the approach ; Function that returns the count of squares parallel to both X and Y - axis from a given set of points ; Initialize result ; Initialize a set to store points ; Initialize a map to store the points in the same vertical line ; Store the points in a set ; Store the points in the same vertical line i . e . with same X co - ordinates ; Check for every two points in the same vertical line ; Check if other two point are present or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSquares ( int * X , int * Y , int N ) { int count = 0 ; set < pair < int , int > > points ; map < int , vector < int > > vertical ; for ( int i = 0 ; i < N ; i ++ ) { points . insert ( { X [ i ] , Y [ i ] } ) ; } for ( int i = 0 ; i < N ; i ++ ) { vertical [ X [ i ] ] . push_back ( Y [ i ] ) ; } for ( auto line : vertical ) { int X1 = line . first ; vector < int > yList = line . second ; for ( int i = 0 ; i < yList . size ( ) ; i ++ ) { int Y1 = yList [ i ] ; for ( int j = i + 1 ; j < yList . size ( ) ; j ++ ) { int Y2 = yList [ j ] ; int side = abs ( Y1 - Y2 ) ; int X2 = X1 + side ; if ( points . find ( { X2 , Y1 } ) != points . end ( ) and points . find ( { X2 , Y2 } ) != points . end ( ) ) count ++ ; } } } return count ; } int main ( ) { int X [ ] = { 0 , 2 , 0 , 2 } , Y [ ] = { 0 , 2 , 2 , 0 } ; int N = sizeof ( X ) / sizeof ( X [ 0 ] ) ; cout << countSquares ( X , Y , N ) ; return 0 ; }