Number of unique rectangles formed using N unit squares
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.
Examples :
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)
C++
Java
<div id= "highlighter_899625" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" ><div class = "container" ><div class = "line number1 index0 alt2" ><code class = "comments" >
|
Python3
C#
PHP
Javascript
Output :
6
Time Complexity: O(n?n)
Auxiliary Space: O(1)
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...