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

 `// C++ program to implement` `// the above approach`   `#include ` `using` `namespace` `std;`   `// Function to get the count ` `// of ractangles ` `int` `cntRect(``int` `points[], ``int` `N,` `             ``int` `rectangle[])` `{` `    ``// Store distinct` `    ``// horizontal lines` `    ``unordered_set<``int``> cntHor;   ` `    `  `    ``// Store distinct` `    ``// Vertical lines` `    ``unordered_set<``int``> cntVer;   ` `    `  `    ``// Insert horizontal line` `    ``// passing through 0` `    ``cntHor.insert(0);` `    `  `    ``// Insert vertical line` `    ``// passing through 0.` `    ``cntVer.insert(0);` `    `  `    ``// Insert horizontal line` `    ``// passing through rectangle` `    ``cntHor.insert(rectangle);` `    `  `    ``// Insert vertical line` `    ``// passing through rectangle` `    ``cntVer.insert(rectangle);` `    `  `    ``// Insert all horizontal and ` `    ``// vertical lines passing through` `    ``// the given array` `    ``for` `(``int` `i = 0; i < N; i++) {` `        `  `        ``// Insert all horizontal lines` `        ``cntHor.insert(points[i]);` `        `  `        ``// Insert all vertical lines` `        ``cntVer.insert(points[i]);` `    ``}` `    `  `    ``return` `(cntHor.size() - 1) * ` `              ``(cntVer.size() - 1);` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `rectangle[] = {{0, 0}, {0, 5},` `                          ``{5, 0}, {5, 5}};` `    ``int` `points[] = {{1, 2}, {3, 4}};` `    `  `    ``int` `N = ``sizeof``(points) / ``sizeof``(points);` `    ``cout<

## Java

 `// Java program to implement` `// the above approach` `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG{` `    `  `// Function to get the count` `// of ractangles` `public` `static` `int` `cntRect(``int` `points[][], ``int` `N,` `                          ``int` `rectangle[][])` `{` `    `  `    ``// Store distinct` `    ``// horizontal lines` `    ``HashSet cntHor = ``new` `HashSet<>();`   `    ``// Store distinct` `    ``// Vertical lines` `    ``HashSet cntVer = ``new` `HashSet<>();`   `    ``// Insert horizontal line` `    ``// passing through 0` `    ``cntHor.add(``0``);`   `    ``// Insert vertical line` `    ``// passing through 0.` `    ``cntVer.add(``0``);`   `    ``// Insert horizontal line` `    ``// passing through rectangle` `    ``cntHor.add(rectangle[``3``][``0``]);`   `    ``// Insert vertical line` `    ``// passing through rectangle` `    ``cntVer.add(rectangle[``3``][``1``]);`   `    ``// Insert all horizontal and` `    ``// vertical lines passing through` `    ``// the given array` `    ``for``(``int` `i = ``0``; i < N; i++)` `    ``{` `        `  `        ``// Insert all horizontal lines` `        ``cntHor.add(points[i][``0``]);`   `        ``// Insert all vertical lines` `        ``cntVer.add(points[i][``1``]);` `    ``}` `    ``return` `(cntHor.size() - ``1``) *` `           ``(cntVer.size() - ``1``);` `}`   `// Driver Code` `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));` `}` `}`   `// This code is contributed by hemanth gadarla`

## Python3

 `# Python3 program to implement` `# the above approach`   `# Function to get the count ` `# of ractangles ` `def` `cntRect(points, N,` `            ``rectangle):`   `    ``# Store distinct` `    ``# horizontal lines` `    ``cntHor ``=` `set``([])  ` `    `  `    ``# Store distinct` `    ``# Vertical lines` `    ``cntVer ``=` `set``([])` `    `  `    ``# Insert horizontal line` `    ``# passing through 0` `    ``cntHor.add(``0``)` `    `  `    ``# Insert vertical line` `    ``# passing through 0.` `    ``cntVer.add(``0``)` `    `  `    ``# Insert horizontal line` `    ``# passing through rectangle` `    ``cntHor.add(rectangle[``3``][``0``])` `    `  `    ``# Insert vertical line` `    ``# passing through rectangle` `    ``cntVer.add(rectangle[``3``][``1``])` `    `  `    ``# Insert all horizontal and ` `    ``# vertical lines passing through` `    ``# the given array` `    ``for` `i ``in` `range` `(N):` `        `  `        ``# Insert all horizontal lines` `        ``cntHor.add(points[i][``0``])` `        `  `        ``# Insert all vertical lines` `        ``cntVer.add(points[i][``1``])` `   `  `    ``return` `((``len``(cntHor) ``-` `1``) ``*` `            ``(``len``(cntVer) ``-` `1``))`   `# Driver Code` `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))`   `# This code is contributed by Chitranayal`

## C#

 `// C# program to implement` `// the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` `    `  `// Function to get the count` `// of ractangles` `public` `static` `int` `cntRect(``int` `[,]points, ` `                          ``int` `N, ``int` `[,]rectangle)` `{` `  ``// Store distinct` `  ``// horizontal lines` `  ``HashSet<``int``> cntHor = ``new` `HashSet<``int``>();`   `  ``// Store distinct` `  ``// Vertical lines` `  ``HashSet<``int``> cntVer = ``new` `HashSet<``int``>();`   `  ``// Insert horizontal line` `  ``// passing through 0` `  ``cntHor.Add(0);`   `  ``// Insert vertical line` `  ``// passing through 0.` `  ``cntVer.Add(0);`   `  ``// Insert horizontal line` `  ``// passing through rectangle[3,0]` `  ``cntHor.Add(rectangle[3, 0]);`   `  ``// Insert vertical line` `  ``// passing through rectangle[3,1]` `  ``cntVer.Add(rectangle[3, 1]);`   `  ``// Insert all horizontal and` `  ``// vertical lines passing through` `  ``// the given array` `  ``for``(``int` `i = 0; i < N; i++)` `  ``{` `    ``// Insert all horizontal lines` `    ``cntHor.Add(points[i, 0]);`   `    ``// Insert all vertical lines` `    ``cntVer.Add(points[i, 1]);` `  ``}` `  ``return` `(cntHor.Count - 1) *` `         ``(cntVer.Count - 1);` `}`   `// Driver Code` `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));` `}` `}`   `// This code is contributed by 29AjayKumar`

Output:

```9

```

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.