Count of Right | Python3 program for the above approach ; Function to find the number of right angled triangle that are formed from given N points whose perpendicular or base is parallel to X or Y axis ; To store the number of points has same x or y coordinates ; Store the total count of triangle ; Iterate to check for total number of possible triangle ; Add the count of triangles formed ; Total possible triangle ; Driver Code ; Given N points ; Function call
from collections import defaultdict NEW_LINE def RightAngled ( a , n ) : NEW_LINE INDENT xpoints = defaultdict ( lambda : 0 ) NEW_LINE ypoints = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT xpoints [ a [ i ] [ 0 ] ] += 1 NEW_LINE ypoints [ a [ i ] [ 1 ] ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( xpoints [ a [ i ] [ 0 ] ] >= 1 and ypoints [ a [ i ] [ 1 ] ] >= 1 ) : NEW_LINE INDENT count += ( ( xpoints [ a [ i ] [ 0 ] ] - 1 ) * ( ypoints [ a [ i ] [ 1 ] ] - 1 ) ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 5 NEW_LINE arr = [ [ 1 , 2 ] , [ 2 , 1 ] , [ 2 , 2 ] , [ 2 , 3 ] , [ 3 , 2 ] ] NEW_LINE print ( RightAngled ( arr , N ) ) NEW_LINE