Given two integers L and B denoting the length and breadth of a rectangle respectively. The task is to calculate the sum of the area of all possible squares that comes into the rectangle.
Input: L = 4, B = 3 Output: 54 Input: L = 2, B = 5 Output: 26
The idea is to observe the count of number of squares in a rectangle.
Now, the number of squares of side 1 will be 12 as there will be two cases one as squares of 1-unit sides along the horizontal(3) and second case as squares of 1-unit sides along the vertical(4). That gives us 3*4 = 12 squares.
When the side is 2 units, one case will be as squares of side of 2 units along only one place horizontally and second case as two places vertically. So the number of squares = 6
So we can deduce that,
Number of squares of size 1*1 will be L*B
Number of squares of size 2*2 will be (L-1)(B-1)
Therefore, the number of squares with size will be:
Number of square of size K = (L-K+1)*(B-K+1)
Therefore, area of total number of squares of size K will be:
Area of total number of square of size K = (L-K+1)*(B-K+1)*K*K
Below is the implementation of above idea:
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.
- Ratio of area of a rectangle with the rectangle inscribed in it
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Count the number of rhombi possible inside a rectangle of given size
- Area of a leaf inside a square
- Area of largest semicircle that can be drawn inside a square
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Generate all integral points lying inside a rectangle
- Area of the biggest possible rhombus that can be inscribed in a rectangle
- Maximum area of rectangle possible with given perimeter
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Check whether a given point lies inside a rectangle or not
- Coordinates of rectangle with given points lie inside
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies on or inside the rectangle | Set 3
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Find minimum area of rectangle with given set of coordinates
- Number of squares of maximum area in a rectangle
- Area and Perimeter of Rectangle in PL/SQL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : chitranayal