Given some points on a plane, which are distinct and no three of them lie on the same line. We need to find number of Parallelograms with the vertices as the given points. Examples:
Input : points[] = {(0, 0), (0, 2), (2, 2), (4, 2),
(1, 4), (3, 4)}
Output : 2
Two Parallelograms are possible by choosing above
given point as vertices, which are shown in below
diagram.
We can solve this problem by using a special property of parallelograms that diagonals of a parallelogram intersect each other in the middle. So if we get such a middle point which is middle point of more than one line segment, then we can conclude that a parallelogram exists, more accurately if a middle point occurs x times, then diagonals of possible parallelograms can be chosen in xC2 ways, i.e. there will be x*(x-1)/2 parallelograms corresponding to this particular middle point with a frequency x. So we iterate over all pair of points and we calculate their middle point and increase frequency of middle point by 1. At the end, we count number of parallelograms according to the frequency of each distinct middle point as explained above. As we just need frequency of middle point, division by 2 is ignored while calculating middle point for simplicity.
CPP
#include <bits/stdc++.h>
using namespace std;
int countOfParallelograms( int x[], int y[], int N)
{
map<pair< int , int >, int > cnt;
for ( int i=0; i<N; i++)
{
for ( int j=i+1; j<N; j++)
{
int midX = x[i] + x[j];
int midY = y[i] + y[j];
cnt[make_pair(midX, midY)]++;
}
}
int res = 0;
for ( auto it = cnt.begin(); it != cnt.end(); it++)
{
int freq = it->second;
res += freq*(freq - 1)/2;
}
return res;
}
int main()
{
int x[] = {0, 0, 2, 4, 1, 3};
int y[] = {0, 2, 2, 2, 4, 4};
int N = sizeof (x) / sizeof ( int );
cout << countOfParallelograms(x, y, N) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static int countOfParallelograms( int [] x, int [] y, int N)
{
HashMap<String, Integer> cnt = new HashMap<>();
for ( int i= 0 ; i<N; i++)
{
for ( int j=i+ 1 ; j<N; j++)
{
int midX = x[i] + x[j];
int midY = y[i] + y[j];
String temp = String.join( " " , String.valueOf(midX), String.valueOf(midY));
if (cnt.containsKey(temp)){
cnt.put(temp, cnt.get(temp) + 1 );
}
else {
cnt.put(temp, 1 );
}
}
}
int res = 0 ;
for (Map.Entry<String, Integer> it : cnt.entrySet()) {
int freq = it.getValue();
res = res + freq*(freq - 1 )/ 2 ;
}
return res;
}
public static void main(String[] args) {
int [] x = { 0 , 0 , 2 , 4 , 1 , 3 };
int [] y = { 0 , 2 , 2 , 2 , 4 , 4 };
int N = x.length;
System.out.println(countOfParallelograms(x, y, N));
}
}
|
Python3
def countOfParallelograms(x, y, N):
cnt = {}
for i in range (N):
for j in range (i + 1 , N):
midX = x[i] + x[j];
midY = y[i] + y[j];
if ((midX, midY) in cnt):
cnt[(midX, midY)] + = 1
else :
cnt[(midX, midY)] = 1
res = 0
for key in cnt:
freq = cnt[key]
res + = freq * (freq - 1 ) / 2
return res
x = [ 0 , 0 , 2 , 4 , 1 , 3 ]
y = [ 0 , 2 , 2 , 2 , 4 , 4 ]
N = len (x);
print ( int (countOfParallelograms(x, y, N)))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int CountOfParallelograms( int [] x, int [] y, int N)
{
Dictionary< string , int > cnt = new Dictionary< string , int >();
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
int midX = x[i] + x[j];
int midY = y[i] + y[j];
string temp = string .Join( " " , midX.ToString(), midY.ToString());
if (cnt.ContainsKey(temp))
{
cnt[temp]++;
}
else
{
cnt.Add(temp, 1);
}
}
}
int res = 0;
foreach (KeyValuePair< string , int > it in cnt)
{
int freq = it.Value;
res += freq * (freq - 1) / 2;
}
return res;
}
public static void Main( string [] args)
{
int [] x = { 0, 0, 2, 4, 1, 3 };
int [] y = { 0, 2, 2, 2, 4, 4 };
int N = x.Length;
Console.WriteLine(CountOfParallelograms(x, y, N));
}
}
|
Javascript
function countOfParallelograms(x, y, N)
{
let cnt = new Map();
for (let i=0; i<N; i++)
{
for (let j=i+1; j<N; j++)
{
let midX = x[i] + x[j];
let midY = y[i] + y[j];
let make_pair = [midX, midY];
if (cnt.has(make_pair.join( '' ))){
cnt.set(make_pair.join( '' ), cnt.get(make_pair.join( '' )) + 1);
}
else {
cnt.set(make_pair.join( '' ), 1);
}
}
}
let res = 0;
for (const [key, value] of cnt)
{
let freq = value;
res = res + Math.floor(freq*(freq - 1)/2);
}
return res;
}
let x = [0, 0, 2, 4, 1, 3];
let y = [0, 2, 2, 2, 4, 4];
let N = x.length;
console.log(countOfParallelograms(x, y, N));
|
Time Complexity: O(n2logn), as we are iterating through two loops up to n and using a map as well which takes logn.
Auxiliary Space: O(n)
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.