Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
Given a 2D array rectangle[][] representing vertices of a rectangle {(0, 0), (L, 0), (0, B), (L, B)} of dimensions L * B, and another 2D-array, points[][] of size N in a Cartesian coordinate system. Draw a horizontal line parallel to the X-axis and a vertical line parallel to the Y-axis from each point of the given array. The task is to count all possible rectangles present within the given rectangle.
Examples :
Input: Rectangle[][] = {{0, 0}, {5, 0}, {0, 5}, {5, 5}}, points[][] ={{1, 2}, {3, 4}}
Output: 9
Explanation:
Draw a horizontal line and a vertical line at points{1, 2} and points{3,4}.
_ _ _ _ _
5|_|_ _|_ _|
4| | |3,4|
3|_|_ _|_ _|
2| |1,2| |
1|_|_ _|_ _|
0 1 2 3 4 5
Therefore, the required output is 9.
Input: Rectangle[][] = {{0, 0}, {4, 0}, {0, 5}, {4, 5}}, points[][] = {{1, 3}, {2, 3}, {3, 3}}
Output: 12
Approach: The idea is to traverse the array and count all distinct horizontal and vertical lines passing through the given points[][] array. Finally, return the value of multiplication of (count of the distinct horizontal line – 1) * (count of vertical lines – 1). Follow the steps below to solve the problem:
- Create two set, say cntHor, cntVer to store all the distinct horizontal lines and vertical lines passing through points[][] array.
- Traverse the points[][] array.
- Count all distinct horizontal lines using the count of elements in cntHor.
- Count all distinct vertical lines using the count of elements in cntVer.
- Finally, print the value of (count of elements in cntHor – 1) * (count of elements in cntVer – 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntRect( int points[][2], int N,
int rectangle[][2])
{
unordered_set< int > cntHor;
unordered_set< int > cntVer;
cntHor.insert(0);
cntVer.insert(0);
cntHor.insert(rectangle[3][0]);
cntVer.insert(rectangle[3][1]);
for ( int i = 0; i < N; i++) {
cntHor.insert(points[i][0]);
cntVer.insert(points[i][1]);
}
return (cntHor.size() - 1) *
(cntVer.size() - 1);
}
int main()
{
int rectangle[][2] = {{0, 0}, {0, 5},
{5, 0}, {5, 5}};
int points[][2] = {{1, 2}, {3, 4}};
int N = sizeof (points) / sizeof (points[0]);
cout<<cntRect(points, N, rectangle);
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static int cntRect( int points[][], int N,
int rectangle[][])
{
HashSet<Integer> cntHor = new HashSet<>();
HashSet<Integer> cntVer = new HashSet<>();
cntHor.add( 0 );
cntVer.add( 0 );
cntHor.add(rectangle[ 3 ][ 0 ]);
cntVer.add(rectangle[ 3 ][ 1 ]);
for ( int i = 0 ; i < N; i++)
{
cntHor.add(points[i][ 0 ]);
cntVer.add(points[i][ 1 ]);
}
return (cntHor.size() - 1 ) *
(cntVer.size() - 1 );
}
public static void main(String args[])
{
int rectangle[][] = { { 0 , 0 }, { 0 , 5 },
{ 5 , 0 }, { 5 , 5 } };
int points[][] = { { 1 , 2 }, { 3 , 4 } };
int N = points.length;
System.out.println(cntRect(points, N,
rectangle));
}
}
|
Python3
def cntRect(points, N,
rectangle):
cntHor = set ([])
cntVer = set ([])
cntHor.add( 0 )
cntVer.add( 0 )
cntHor.add(rectangle[ 3 ][ 0 ])
cntVer.add(rectangle[ 3 ][ 1 ])
for i in range (N):
cntHor.add(points[i][ 0 ])
cntVer.add(points[i][ 1 ])
return (( len (cntHor) - 1 ) *
( len (cntVer) - 1 ))
if __name__ = = "__main__" :
rectangle = [[ 0 , 0 ], [ 0 , 5 ],
[ 5 , 0 ], [ 5 , 5 ]]
points = [[ 1 , 2 ], [ 3 , 4 ]]
N = len (points)
print (cntRect(points, N, rectangle))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int cntRect( int [,]points,
int N, int [,]rectangle)
{
HashSet< int > cntHor = new HashSet< int >();
HashSet< int > cntVer = new HashSet< int >();
cntHor.Add(0);
cntVer.Add(0);
cntHor.Add(rectangle[3, 0]);
cntVer.Add(rectangle[3, 1]);
for ( int i = 0; i < N; i++)
{
cntHor.Add(points[i, 0]);
cntVer.Add(points[i, 1]);
}
return (cntHor.Count - 1) *
(cntVer.Count - 1);
}
public static void Main(String []args)
{
int [,]rectangle = {{0, 0}, {0, 5},
{5, 0}, {5, 5}};
int [,]points = {{1, 2}, {3, 4}};
int N = points.GetLength(0);
Console.WriteLine(cntRect(points, N,
rectangle));
}
}
|
Javascript
<script>
function cntRect(points, N, rectangle)
{
var cntHor = new Set();
var cntVer = new Set();
cntHor.add(0);
cntVer.add(0);
cntHor.add(rectangle[3][0]);
cntVer.add(rectangle[3][1]);
for ( var i = 0; i < N; i++) {
cntHor.add(points[i][0]);
cntVer.add(points[i][1]);
}
return (cntHor.size - 1) *
(cntVer.size - 1);
}
var rectangle = [[0, 0], [0, 5],
[5, 0], [5, 5]];
var points = [[1, 2], [3, 4]];
var N = points.length;
document.write( cntRect(points, N, rectangle));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
27 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...