Maximum Squares possible parallel to both axes from N distinct points | 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
def countSquares ( X , Y , N ) : NEW_LINE INDENT count = 0 ; NEW_LINE points = [ ] ; NEW_LINE vertical = dict . fromkeys ( X , None ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT points . append ( ( X [ i ] , Y [ i ] ) ) ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if vertical [ X [ i ] ] is None : NEW_LINE INDENT vertical [ X [ i ] ] = [ Y [ i ] ] ; NEW_LINE DEDENT else : NEW_LINE INDENT vertical [ X [ i ] ] . append ( Y [ i ] ) ; NEW_LINE DEDENT DEDENT for line in vertical : NEW_LINE INDENT X1 = line ; NEW_LINE yList = vertical [ line ] ; NEW_LINE for i in range ( len ( yList ) ) : NEW_LINE INDENT Y1 = yList [ i ] ; NEW_LINE for j in range ( i + 1 , len ( yList ) ) : NEW_LINE INDENT Y2 = yList [ j ] ; NEW_LINE side = abs ( Y1 - Y2 ) ; NEW_LINE X2 = X1 + side ; NEW_LINE if ( X2 , Y1 ) in points and ( X2 , Y2 ) in points : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = [ 0 , 2 , 0 , 2 ] ; Y = [ 0 , 2 , 2 , 0 ] ; NEW_LINE N = len ( X ) ; NEW_LINE print ( countSquares ( X , Y , N ) ) ; NEW_LINE DEDENT