Given two arrays **X[]** and **Y[]** consisting of **N** and **M** integers such that there are **N** lines parallel to the **y-axis** and **M** lines parallel to the **x-axis**, the task is to find the total number of squares having unique dimensions that can be generated from the given straight lines.

Each integer(say

a) in the arrayX[]denotes lines having equationx = a, parallel toy-axis.

Each integer(sayb) in the arrayY[]denotes lines having equationy = b, parallel tox-axis.

**Examples:**

Input:X[] = {1, 3, 7}, Y[] = {1, 2, 4, 6}Output:2Explanation:3 lines are parallel to y-axis for x = 1, x = 3 and x = 7.4 lines are parallel to x-axis for y = 2, y = 4, y = 6 and y = 1.

From the above figure, there are two possible squares of unique dimensions that are possible:

1) squareABDC(x = 1, x = 3, y = 4, y = 6), side = 2 units.2) squareBGHF(x = 3, x = 7, y = 2, y = 6), side = 4 units.

Input:X[] = {2, 6, 7, 8}, Y[] = {1, 3, 5, 7}Output:3

**Naive Approach:** The simplest approach is to check for every possible vertical dimension if there exists an equal horizontal dimension. The time complexity of this approach can be reduced with the help of the map. By using a map, we can get all the different vertical/horizontal dimensions.

i>**Time Complexity:** O((N^{2})*log N)**Auxiliary Space:** O(N)

**Efficient Approach:** To optimize the above approach, the idea is to generate all possible dimensions with the help of bitsets. Follow the steps below to solve this problem:

- Initialize bitsets for horizontal and vertical lines from the array
**X[]**and**Y[]**respectively. - Set positions of vertical and horizontal lines in the bitset.
- Maintain another bitset
**hdiff**and**vdiff**that store the different possible dimensions of squares. The dimensions can be obtained by shifting the set bits in the original bitset. - After the above steps, the unique count of squares is the count of the common element in
**hdiff**and**vdiff**which is calculated by**(hdiff&vdiff).count()**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `N = 100; ` ` ` `// Function to find the number of ` `// unique squares ` `int` `countSquares(` `int` `* hor, ` `int` `* ver, ` ` ` `int` `n, ` `int` `m) ` `{ ` ` ` `// Positions of the X[] and Y[] ` ` ` `// are set in the bitsets hpos ` ` ` `// and vpos respectively ` ` ` `bitset<N> hpos, vpos; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `hpos.set(hor[i]); ` ` ` `} ` ` ` `for` `(` `int` `i = 0; i < m; ++i) { ` ` ` `vpos.set(ver[i]); ` ` ` `} ` ` ` ` ` `// Stores all possible sides of the ` ` ` `// square are set in the bitsets ` ` ` `// having difference hdiff & vdiff ` ` ` `bitset<N> hdiff, vdiff; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `hdiff = hdiff | (hpos >> hor[i]); ` ` ` `} ` ` ` `for` `(` `int` `i = 0; i < m; ++i) { ` ` ` `vdiff = vdiff | (vpos >> ver[i]); ` ` ` `} ` ` ` ` ` `// Finding the number of square ` ` ` `// sides which are common to both ` ` ` `int` `common = (hdiff & vdiff).count(); ` ` ` ` ` `// Print the count of squares ` ` ` `cout << common - 1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given horizontal line segments ` ` ` `int` `X[] = { 1, 3, 7 }; ` ` ` ` ` `// Given vertical line segments ` ` ` `int` `Y[] = { 1, 2, 4, 6 }; ` ` ` ` ` `int` `N = (` `sizeof` `(X) / ` `sizeof` `(X[0])); ` ` ` `int` `M = (` `sizeof` `(Y) / ` `sizeof` `(Y[0])); ` ` ` ` ` `// Function Call ` ` ` `countSquares(X, Y, N, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N + M)**Auxiliary Space:** O(maxE), where maxE is the maximum element among both the arrays X[] and Y[].

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.

## Recommended Posts:

- Count squares possible from M and N straight lines parallel to X and Y axis respectively
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Maximum Squares possible parallel to both axes from N distinct points
- Maximum points of intersections possible among X circles and Y straight lines
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Count straight lines intersecting at a given point
- Count of different straight lines with total n points with m collinear
- Check if given two straight lines are identical or not
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Number of lines from given N points not parallel to X or Y axis
- Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points
- Check if three straight lines are concurrent or not
- Check whether two straight lines are orthogonal or not
- Puzzle | Connect 9 circles each arranged at center of a Matrix using 3 straight lines
- Area of triangle formed by the axes of co-ordinates and a given straight line
- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Find whether only two parallel lines contain all coordinates points or not
- Distance between two parallel lines
- Maximum number of region in which N non-parallel lines can divide a plane
- Find Four points such that they form a square whose sides are parallel to x and y axes

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.