Count pairs of points having distance between them equal to integral values in a K | Java 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 ; Driver Code ; Given value of K ; Given points ; Given value of N ; Function Call
class GFG { static void cntPairs ( int [ ] [ ] points , int n , int K ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int dist = 0 ; for ( int k = 0 ; k < K ; k ++ ) { int temp = ( points [ i ] [ k ] - points [ j ] [ k ] ) ; dist += temp * temp ; } if ( Math . sqrt ( dist ) * Math . sqrt ( dist ) == dist ) { ans += 1 ; } } } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int K = 2 ; int [ ] [ ] points = { { 1 , 2 } , { 5 , 5 } , { - 2 , 8 } } ; int n = points . length ; cntPairs ( points , n , K ) ; } }