Total number of triplets ( A , B , C ) in which the points B and C are Equidistant to A | Function to size ( ) such triplets ; Iterate over all the points ; Iterate over all points other than the current point ; Compute squared euclidean distance for the current point ; Compute nP2 that is n * ( n - 1 ) ; Return the final result ; Driver code
function numTrip ( points ) { let res = 0 ; for ( let i = 0 ; i < points . length ; ++ i ) { let map = new Map ( ) ; for ( let j = 0 ; j < points . length ; ++ j ) { if ( j == i ) continue ; let dy = points [ i ] [ 1 ] - points [ j ] [ 1 ] ; let dx = points [ i ] [ 0 ] - points [ j ] [ 0 ] ; let key = dy * dy ; key += dx * dx ; if ( map . has ( key ) ) { map . set ( key , map . get ( key ) + 1 ) ; } else { map . set ( key , 1 ) ; } } for ( let [ key , value ] of map . entries ( ) ) res += value * ( value - 1 ) ; } return res ; } let mat = [ ] ; mat . push ( [ 0 , 0 ] ) ; mat . push ( [ 1 , 0 ] ) ; mat . push ( [ 2 , 0 ] ) ; document . write ( numTrip ( mat ) ) ;