Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition

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++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the count of possible tripletsint countTriplets(int n, vector<pair<int, int> > points){Â Â Â Â set<pair<int, int> > pts;Â Â Â Â int ct = 0;Â
    // Insert all the points in a set    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 the mid point exists in the set            if (x % 2 == 0 && y % 2 == 0)                if (pts.find(make_pair(x / 2, y / 2))                    != pts.end())                    ct++;        }Â
    // Return the count of valid triplets    return ct;}Â
// Driver codeint main(){    vector<pair<int, int> > points        = { { 1, 1 }, { 2, 2 }, { 3, 3 } };    int n = points.size();    cout << countTriplets(n, points);} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{Â Â Â Â Â static class pair{Â Â Â Â int first,second;Â
    public pair(int first, int second)     {        this.first = first;        this.second = second;    }     }Â
// Function to return the count of possible tripletsstatic int countTriplets(int n, Vector<pair> points){Â Â Â Â Set<pair> pts = new HashSet<pair>();Â Â Â Â int ct = 0;Â
    // Insert all the points in a set    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 the mid point exists in the set            if (x % 2 == 0 && y % 2 == 0)                if (!pts.contains(new pair(x / 2, y / 2)))                    ct++;        }Â
    // Return the count of valid triplets    return ct;}Â
// Driver codepublic 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));}}Â
// This code is contributed by Princi Singh |
Python3
# Python3 implementation of the approach Â
# Function to return the count # of possible triplets def countTriplets(n, points) :Â Â Â Â Â Â Â Â Â pts = [] Â Â Â Â ct = 0; Â
    # Insert all the points in a set     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 the mid point exists in the set             if (x % 2 == 0 and y % 2 == 0) :                if [x // 2, y // 2] in pts :                    ct += 1                         # Return the count of valid triplets     return ct Â
# Driver code if __name__ == "__main__" :Â Â Â Â Â Â Â Â Â points = [[ 1, 1 ], [ 2, 2 ], [ 3, 3 ]]Â Â Â Â n = len(points) Â Â Â Â print(countTriplets(n, points))Â
# This code is contributed by Ryuga |
C#
// C# implementation of the approachusing 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;    }     }Â
// Function to return the count of possible tripletsstatic int countTriplets(int n, List<pair> points){Â Â Â Â HashSet<pair> pts = new HashSet<pair>();Â Â Â Â int ct = 0;Â
    // Insert all the points in a set    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 the mid point exists in the set            if (x % 2 == 0 && y % 2 == 0)                if (!pts.Contains(new pair(x / 2, y / 2)))                    ct++;        }Â
    // Return the count of valid triplets    return ct;}Â
// Driver codepublic 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));}}Â
// This code is contributed by 29AjayKumar |
PHP
<?php// PHP implementation of the approach Â
// Function to return the count // of possible triplets function countTriplets($n, $points){Â Â Â Â $pts = array();Â Â Â Â $ct = 0; Â
    // Insert all the points in a set     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 the mid point exists in the set             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 the count of valid triplets     return $ct; }Â
// Driver code $points = array(array( 1, 1 ), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â array( 2, 2 ), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â array( 3, 3 ));$n = count($points);print(countTriplets($n, $points));Â
// This code is contributed by chandan_jnu?> |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the count of possible tripletsfunction countTriplets(n, points){Â Â Â Â var pts = new Set();Â Â Â Â var ct = 0;Â
    // Insert all the points in a set    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 the mid point exists in the set            if (x % 2 == 0 && y % 2 == 0)                if (pts.has([(x / 2), (y / 2)].toString()))                    ct++;        }Â
    // Return the count of valid triplets    return ct;}Â
// Driver codevar points    = [ [ 1, 1 ], [ 2, 2 ], [ 3, 3 ] ];var n = points.length;document.write( countTriplets(n, points))Â
// This code is contributed by famously.</script> |
1
Â
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



