Find number of rectangles that can be formed from a given set of coordinates
Given an array arr[][] consisting of pair of integers denoting coordinates. The task is to count the total number of rectangles that can be formed using given coordinates.
Examples:
Input: arr[][] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1}, {11, 11}}
Output: 3
Explanation: Following are the rectangles formed using given coordinates.
First Rectangle (0, 0), (0, 1), (1, 0), (1, 1)
Second Rectangle (0, 0), (0, 1), (2, 0), (2, 1)
Third Rectangle (1, 0), (1, 1), (2, 0), (2, 1)
Input: arr[][] = {{10, 0}, {0, 10}, {11, 11}, {0, 11}, {12, 10}}
Output: 0
Explanation: No Rectangles can be formed using these co-ordinates
Approach: This problem can be solved by using the property of the rectangle and Hash maps. If two coordinates of a rectangle are known then the other two remaining coordinates can be easily determined. For every pair of coordinates find the other two coordinates that can form a rectangle.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countRectangles(vector<pair< int , int > >& ob)
{
set<pair< int , int > > it;
for ( int i = 0; i < ob.size(); ++i) {
it.insert(ob[i]);
}
int ans = 0;
for ( int i = 0; i < ob.size(); ++i)
{
for ( int j = 0; j < ob.size(); ++j)
{
if (ob[i].first != ob[j].first
&& ob[i].second != ob[j].second)
{
if (it.count({ ob[i].first, ob[j].second })
&& it.count(
{ ob[j].first, ob[i].second }))
{
++ans;
}
}
}
}
return ans / 4;
}
int main()
{
int N = 7;
vector<pair< int , int > > ob(N);
ob[0] = { 0, 0 };
ob[1] = { 1, 0 };
ob[2] = { 1, 1 };
ob[3] = { 0, 1 };
ob[4] = { 2, 0 };
ob[5] = { 2, 1 };
ob[6] = { 11, 23 };
cout << countRectangles(ob);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Main {
static class Pair implements Comparable<Pair> {
int first;
int second;
Pair( int first, int second)
{
this .first = first;
this .second = second;
}
@Override
public int compareTo(Pair o)
{
if ( this .first == o.first) {
return this .second - o.second;
}
return this .first - o.first;
}
}
static int countRectangles(Pair ob[])
{
TreeSet<Pair> it = new TreeSet<>();
for ( int i = 0 ; i < ob.length; ++i) {
it.add(ob[i]);
}
int ans = 0 ;
for ( int i = 0 ; i < ob.length; ++i) {
for ( int j = 0 ; j < ob.length; ++j) {
if (ob[i].first != ob[j].first
&& ob[i].second != ob[j].second) {
if (it.contains( new Pair(ob[i].first,
ob[j].second))
&& it.contains( new Pair(
ob[j].first, ob[i].second))) {
++ans;
}
}
}
}
return ans / 4 ;
}
public static void main(String[] args)
{
int N = 7 ;
Pair ob[] = new Pair[N];
ob[ 0 ] = new Pair( 0 , 0 );
ob[ 1 ] = new Pair( 1 , 0 );
ob[ 2 ] = new Pair( 1 , 1 );
ob[ 3 ] = new Pair( 0 , 1 );
ob[ 4 ] = new Pair( 2 , 0 );
ob[ 5 ] = new Pair( 2 , 1 );
ob[ 6 ] = new Pair( 11 , 23 );
System.out.print(countRectangles(ob));
}
}
|
Python3
def countRectangles(ob):
it = set ()
for i in range ( len (ob)):
it.add(f "{ob[i]}" );
ans = 0 ;
for i in range ( len (ob)):
for j in range ( len (ob)):
if (ob[i][ 0 ] ! = ob[j][ 0 ] and ob[i][ 1 ] ! = ob[j][ 1 ]):
if (f "{[ob[i][0], ob[j][1]]}" in it and f "{[ob[j][0], ob[i][1]]}" in it):
ans + = 1
return int (ans / 4 );
N = 7 ;
ob = [ 0 ] * N
ob[ 0 ] = [ 0 , 0 ];
ob[ 1 ] = [ 1 , 0 ];
ob[ 2 ] = [ 1 , 1 ];
ob[ 3 ] = [ 0 , 1 ];
ob[ 4 ] = [ 2 , 0 ];
ob[ 5 ] = [ 2 , 1 ];
ob[ 6 ] = [ 11 , 23 ];
print (countRectangles(ob));
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int CountRectangles( int [,] ob) {
HashSet<KeyValuePair< int , int >> it
= new HashSet<KeyValuePair< int , int >>();
for ( int i = 0; i < ob.GetLength(0); ++i) {
it.Add( new KeyValuePair< int , int >(ob[i, 0], ob[i, 1]));
}
int ans = 0;
for ( int i = 0; i < ob.GetLength(0); ++i) {
for ( int j = 0; j < ob.GetLength(0); ++j) {
if (ob[i, 0] != ob[j, 0] && ob[i, 1] != ob[j, 1]) {
if (it.Contains( new KeyValuePair< int , int >(ob[i, 0], ob[j, 1]))
&& it.Contains( new KeyValuePair< int , int >(ob[j, 0], ob[i, 1]))) {
ans = ans + 1;
}
}
}
}
return ans / 4;
}
static void Main() {
int [,] ob = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { 2, 0 }, { 2, 1 }, { 11, 23 } };
Console.WriteLine(CountRectangles(ob));
}
}
|
Javascript
<script>
function countRectangles(ob) {
let it = new Set();
for (let i = 0; i < ob.length; ++i) {
it.add(`${ob[i]}`);
}
let ans = 0;
for (let i = 0; i < ob.length; ++i) {
for (let j = 0; j < ob.length; ++j) {
if (ob[i][0] != ob[j][0]
&& ob[i][1] != ob[j][1]) {
if (it.has(`${[ob[i][0], ob[j][1]]}`)
&& it.has(`${[ob[j][0], ob[i][1]]}`)) {
++ans;
}
}
}
}
return ans / 4;
}
let N = 7;
let ob = new Array(N).fill(0);
ob[0] = [0, 0];
ob[1] = [1, 0];
ob[2] = [1, 1];
ob[3] = [0, 1];
ob[4] = [2, 0];
ob[5] = [2, 1];
ob[6] = [11, 23];
document.write(countRectangles(ob));
</script>
|
Time Complexity: O(N2), Where N is the size of the array.
Auxiliary Space: O(N), Where N is the size of the array.
Last Updated :
22 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...