Count squares possible from M and N straight lines parallel to X and Y axis respectively | Function to count all the possible squares with given lines parallel to both the X and Y axis ; Stores the count of all possible distances in X [ ] & Y [ ] respectively ; Find distance between all pairs in the array X [ ] ; Add the count to m1 ; Find distance between all pairs in the array Y [ ] ; Add the count to m2 ; Find sum of m1 [ i ] * m2 [ i ] for same distance ; Find current count in m2 ; Add to the total count ; Return the final count ; Driver Code ; Given lines ; Function call
def numberOfSquares ( X , Y , N , M ) : NEW_LINE INDENT m1 = { } NEW_LINE m2 = { } NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT dist = abs ( X [ i ] - X [ j ] ) NEW_LINE if dist in m1 : NEW_LINE INDENT m1 [ dist ] = m1 [ dist ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT m1 [ dist ] = 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , M ) : NEW_LINE INDENT for j in range ( i + 1 , M ) : NEW_LINE INDENT dist = abs ( Y [ i ] - Y [ j ] ) NEW_LINE if dist in m2 : NEW_LINE INDENT m2 [ dist ] = m2 [ dist ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT m2 [ dist ] = 1 NEW_LINE DEDENT DEDENT DEDENT for key in m1 : NEW_LINE INDENT if key in m2 : NEW_LINE INDENT ans = ans + ( m1 [ key ] * m2 [ key ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = [ 1 , 3 , 7 ] NEW_LINE Y = [ 2 , 4 , 6 , 1 ] NEW_LINE N = len ( X ) NEW_LINE M = len ( Y ) NEW_LINE print ( numberOfSquares ( X , Y , N , M ) ) NEW_LINE DEDENT