Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find number of rectangles that can be formed from a given set of coordinates

  • Difficulty Level : Hard
  • Last Updated : 17 Dec, 2021

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++




// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of possible rectangles
int countRectangles(vector<pair<int, int> >& ob)
{
 
    // Creating TreeSet containing elements
    set<pair<int, int> > it;
 
    // Inserting the pairs in the set
    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)
            {
               
                // Searching the pairs in the set
                if (it.count({ ob[i].first, ob[j].second })
                    && it.count(
                        { ob[j].first, ob[i].second }))
                {
                   
                    // Increase the answer
                    ++ans;
                }
            }
        }
    }
 
    // Return the final answer
    return ans / 4;
}
 
// Driver Code
int main()
{
 
    int N = 7;
    vector<pair<int, int> > ob(N);
 
    // Inserting the pairs
    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;
}
 
    // This code is contributed by rakeshsahni

Java




// Java program for above approach
import java.io.*;
import java.util.*;
 
class Main {
 
    // Creataing pair class and
    // implements comparable interface
    static class Pair implements Comparable<Pair> {
        int first;
        int second;
        Pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
        // Changing sorting order of the pair class
        @Override
        public int compareTo(Pair o)
        {
            // Checking the x axis
            if (this.first == o.first) {
                return this.second - o.second;
            }
 
            return this.first - o.first;
        }
    }
 
    // Function to find number of possible rectangles
    static int countRectangles(Pair ob[])
    {
 
        // Creating TreeSet containing elements
        TreeSet<Pair> it = new TreeSet<>();
 
        // Inserting the pairs in the set
        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) {
                    // Searching the pairs in the set
                    if (it.contains(new Pair(ob[i].first,
                                             ob[j].second))
                        && it.contains(new Pair(
                               ob[j].first, ob[i].second))) {
                        // Increase the answer
                        ++ans;
                    }
                }
            }
        }
 
        // Return the final answer
        return ans / 4;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int N = 7;
        Pair ob[] = new Pair[N];
 
        // Inserting the pairs
        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




# Python program for above approach
 
# Function to find number of possible rectangles
def countRectangles(ob):
 
    # Creating TreeSet containing elements
    it = set()
 
    # Inserting the pairs in the 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]):
               
                # Searching the pairs in the set
                if (f"{[ob[i][0], ob[j][1]]}" in it and f"{[ob[j][0], ob[i][1]]}" in it):
 
                    # Increase the answer
                    ans += 1
 
    # Return the final answer
    return int(ans / 4);
 
# Driver Code
N = 7;
ob = [0] * N
 
# Inserting the pairs
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));
 
# This code is contributed by Saurabh Jaiswal

Javascript




<script>
// Javascript program for above approach
 
// Function to find number of possible rectangles
function countRectangles(ob) {
 
    // Creating TreeSet containing elements
    let it = new Set();
 
    // Inserting the pairs in the 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]) {
                // Searching the pairs in the set
                if (it.has(`${[ob[i][0], ob[j][1]]}`)
                    && it.has(`${[ob[j][0], ob[i][1]]}`)) {
 
                    // Increase the answer
                    ++ans;
                }
            }
        }
    }
 
    // Return the final answer
    return ans / 4;
}
 
// Driver Code
 
let N = 7;
let ob = new Array(N).fill(0);
 
// Inserting the pairs
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));
 
// This code is contributed by Saurabh Jaiswal
</script>
Output: 
3

 

Time Complexity: O(N2), Where N is the size of the array. 
Auxiliary Space: O(N), Where N is the size of the array.

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!