Count pairs of points having distance between them equal to integral values in a K | javascript program for the above approach Function to find pairs whose distance between the points of is an integer value . ; Stores count of pairs whose distance between points is an integer ; Traverse the array , points ; Stores distance between points ( i , j ) ; Traverse all the points of current pair ; Update temp ; Update dist ; If dist is a perfect square ; Update ans ; Given value of K ; Given points ; Given value of N ; Function Call
function cntPairs ( points , n , K ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var dist = 0 ; for ( k = 0 ; k < K ; k ++ ) { var temp = ( points [ i ] [ k ] - points [ j ] [ k ] ) ; dist += temp * temp ; } if ( Math . sqrt ( dist ) * Math . sqrt ( dist ) == dist ) { ans += 1 ; } } } document . write ( ans + " " ) ; } var K = 2 ; var points = [ [ 1 , 2 ] , [ 5 , 5 ] , [ - 2 , 8 ] ] ; var n = points . length ; cntPairs ( points , n , K ) ;