Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
Last Updated :
24 May, 2022
Given N points in 2 dimensional space. The task is to count the number of triplets pairs (A, B, C) such that point B is the midpoint of line segment formed by joining points A and C.
Examples:
Input: points = {{1, 1}, {2, 2}, {3, 3}}
Output: 1
The point (2, 2) is the midpoint of the line segment joining points (1, 1) and (3, 3).
Input: points = {{1, 1}, {1, 2}, {1, 5}}
Output: 0
Approach: Consider a pair of points A and C. The midpoint of the line segment joining these points will be ((A * X + C * X) / 2, (A * Y + C * Y) / 2)). If the point is present in the given list of points, we have found a triplet. To quickly check if a point is in our list of points we can use a set. Doing this for all pairs of points will give us the required count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int n, vector<pair< int , int > > points)
{
set<pair< int , int > > pts;
int ct = 0;
for ( int i = 0; i < n; i++)
pts.insert(points[i]);
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++) {
int x = points[i].first + points[j].first;
int y = points[i].second + points[j].second;
if (x % 2 == 0 && y % 2 == 0)
if (pts.find(make_pair(x / 2, y / 2))
!= pts.end())
ct++;
}
return ct;
}
int main()
{
vector<pair< int , int > > points
= { { 1, 1 }, { 2, 2 }, { 3, 3 } };
int n = points.size();
cout << countTriplets(n, points);
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first,second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int countTriplets( int n, Vector<pair> points)
{
Set<pair> pts = new HashSet<pair>();
int ct = 0 ;
for ( int i = 0 ; i < n; i++)
pts.add(points.get(i));
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
{
int x = points.get(i).first + points.get(j).first;
int y = points.get(i).second + points.get(j).second;
if (x % 2 == 0 && y % 2 == 0 )
if (!pts.contains( new pair(x / 2 , y / 2 )))
ct++;
}
return ct;
}
public static void main(String args[])
{
Vector<pair> points = new Vector<>();
points.add( new pair( 1 , 1 ));
points.add( new pair( 2 , 2 ));
points.add( new pair( 3 , 3 ));
int n = points.size();
System.out.println(countTriplets(n, points));
}
}
|
Python3
def countTriplets(n, points) :
pts = []
ct = 0 ;
for i in range (n) :
pts.append(points[i]);
for i in range (n) :
for j in range (i + 1 , n) :
x = points[i][ 0 ] + points[j][ 0 ];
y = points[i][ 1 ] + points[j][ 1 ];
if (x % 2 = = 0 and y % 2 = = 0 ) :
if [x / / 2 , y / / 2 ] in pts :
ct + = 1
return ct
if __name__ = = "__main__" :
points = [[ 1 , 1 ], [ 2 , 2 ], [ 3 , 3 ]]
n = len (points)
print (countTriplets(n, points))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class pair
{
public int first,second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int countTriplets( int n, List<pair> points)
{
HashSet<pair> pts = new HashSet<pair>();
int ct = 0;
for ( int i = 0; i < n; i++)
pts.Add(points[i]);
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
{
int x = points[i].first + points[j].first;
int y = points[i].second + points[j].second;
if (x % 2 == 0 && y % 2 == 0)
if (!pts.Contains( new pair(x / 2, y / 2)))
ct++;
}
return ct;
}
public static void Main(String []args)
{
List<pair> points = new List<pair>();
points.Add( new pair(1, 1));
points.Add( new pair(2, 2));
points.Add( new pair(3, 3));
int n = points.Count;
Console.WriteLine(countTriplets(n, points));
}
}
|
PHP
<?php
function countTriplets( $n , $points )
{
$pts = array ();
$ct = 0;
for ( $i = 0; $i < count ( $points ); $i ++)
{
for ( $j = 0;
$j < count ( $points [ $i ]); $j ++)
{
$pts [] = $points [ $i ][ $j ];
}
}
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
{
$x = $points [ $i ][0] + $points [ $j ][0];
$y = $points [ $i ][1] + $points [ $j ][1];
if ( $x % 2 == 0 and $y % 2 == 0)
if (in_array((int)( $x / 2), $pts ) and
in_array((int)( $y / 2), $pts ))
$ct += 1;
}
return $ct ;
}
$points = array ( array ( 1, 1 ),
array ( 2, 2 ),
array ( 3, 3 ));
$n = count ( $points );
print (countTriplets( $n , $points ));
?>
|
Javascript
<script>
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))
</script>
|
Time Complexity: O(N2 logN), where N represents the size of the given vector.
Auxiliary Space: O(N), where N represents the size of the given vector.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...