You are given N unit squares (squares with side length 1 unit), and you are asked to make rectangles using these squares. You have to count the number of rotationally unique rectangles than you can make. What does rotationally unique mean? Well, two rectangles are rotationally unique if one can’t be rotated to become equivalent to the other one.
Example – The 4×2 rectangle can be rotated 90 degrees clockwise to make it the exact same as the 2×4 rectangle and so these are not rotationally unique.
Input : N = 4 Output : 5 We can make following five rectangles 1 x 1, 1 x 2, 2 x 2, 1 x 3 and 1 x 4 Input : N = 5 Output : 6 Input : 6 Output : 8
So how do we solve this problem?
Every rectangle is uniquely determined by its length and its height.
A rectangle of length = l and height = h then l * h <= n is considered equivalent to a rectangle with length = h and height = l provided l is not equal to h. If we can have some sort of "ordering" in these pairs then we can avoid counting (l, h) and (h, l) as different rectangles. One way to define such an ordering is:
Assume that length <= height and count for all such pairs such that length*height <= n.
We have, length <= height
or, length*length <= length*height
or, length*length <= n
or, length <= sqrt(n)
This article is contributed by Hemang Sarkar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Total number of unit cells covered by all given Rectangles
- Determine the number of squares of unit area that a given line will pass through.
- Smallest square formed with given rectangles
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Number of rectangles in N*M grid
- Number of rectangles in a circle of radius R
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Count the number of rectangles such that ratio of sides lies in the range [a,b]
- Find the minimum number of rectangles left after inserting one into another
- Find maximum number that can be formed using digits of a given number
- Find if two rectangles overlap
- Create a matrix with alternating rectangles of O and X
- Find all rectangles filled with 0
- Sum of Areas of Rectangles possible for an array
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Count of distinct rectangles inscribed in an equilateral triangle
- Count Distinct Rectangles in N*N Chessboard
Improved By : jit_t