Count triplet pairs ( A , B , C ) of points in 2 | Function to return the count of possible triplets ; Insert all the points in a set ; If the mid point exists in the set ; Return the count of valid triplets ; Driver code
function countTriplets ( n , points ) { var pts = new Set ( ) ; var ct = 0 ; for ( var i = 0 ; i < n ; i ++ ) pts . add ( points [ i ] . toString ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) { var x = points [ i ] [ 0 ] + points [ j ] [ 0 ] ; var y = points [ i ] [ 1 ] + points [ j ] [ 1 ] ; if ( x % 2 == 0 && y % 2 == 0 ) if ( pts . has ( [ ( x / 2 ) , ( y / 2 ) ] . toString ( ) ) ) ct ++ ; } return ct ; } var points = [ [ 1 , 1 ] , [ 2 , 2 ] , [ 3 , 3 ] ] ; var n = points . length ; document . write ( countTriplets ( n , points ) )