Count pairs of points having distance between them equal to integral values in a K-dimensional space
Given an array points[] representing N points in a K-dimensional space, the task is to find the count of pairs of points in the space such that the distance between the points of each pair is an integer value.
Examples:
Input: points[] = { {1, 2}, {5, 5}, {2, 8} }, K = 2
Output: 1
Explanation:
Distance between points of the pair(points[0], points[1]) = 5
Distance between points of the pair(points[1], points[2]) = sqrt(58)
Distance between points of the pair(points[0], points[2]) = 3 * sqrt(5)
Therefore, the required output is 1.
Input: points[] = { {-3, 7, 8, 2}, {-12, 1, 10, 2}, {-2, 8, 9, 3} }, K = 4
Output: 2.
Approach: The idea is to generate all possible pairs of the given array and find the distance between the points of each pair and check if it is an integer value or not. If found to be true, then increment the count. Finally, print the total count obtained. Follow the steps below to solve the problem:
- Distance between the points of the pair({ a1, a2, …, aK }, { b1, b2, …, bK }) can be calculated using the below formula:
Distance(a, b) = sqrt(((a1 – b1)2 + (a2 – b2)2 + …. + (aK – bK)2 ))
- Traverse the array, and generate all possible pairs of the given array.
- For each pair of points, check if the distance between the points of the pair is an integer or not. If found to be true, then increment the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void cntPairs(vector<vector< 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 ( sqrt (dist) * sqrt (dist) == dist) {
ans += 1;
}
}
}
cout << ans << endl;
}
int main()
{
int K = 2;
vector<vector< int > > points
= { { 1, 2 }, { 5, 5 }, { -2, 8 } };
int n = points.size();
cntPairs(points, n, K);
return 0;
}
|
Java
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 + "\n" );
}
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);
}
}
|
Python3
def cntPairs(points, n, K):
ans = 0
for i in range ( 0 , n):
for j in range (i + 1 , n):
dist = 0
for k in range (K):
temp = (points[i][k] - points[j][k])
dist + = temp * temp
if (((dist) * * ( 1 / 2 )) * ((dist) * * ( 1 / 2 )) = = dist):
ans + = 1
print (ans)
K = 2
points = [ [ 1 , 2 ], [ 5 , 5 ], [ - 2 , 8 ]]
n = len (points)
cntPairs(points, n, K)
|
C#
using System;
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;
}
}
}
Console.WriteLine(ans);
}
public static void Main()
{
int K = 2;
int [, ] points = { { 1, 2 }, { 5, 5 }, { -2, 8 } };
int n = points.GetLength(0);
cntPairs(points, n, K);
}
}
|
Javascript
<script>
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 + "\n" );
}
var K = 2;
var points = [ [ 1, 2 ], [ 5, 5 ],
[ -2, 8 ] ];
var n = points.length;
cntPairs(points, n, K);
</script>
|
Time Complexity: O(N2 * K)
Auxiliary Space: O(1)
Last Updated :
30 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...