Given a m x n rectangle, how many squares are there in it?
Input: m = 2, n = 2 Output: 5 There are 4 squares of size 1x1 + 1 square of size 2x2. Input: m = 4, n = 3 Output: 20 There are 12 squares of size 1x1 + 6 squares of size 2x2 + 2 squares of size 3x3.
Let us first solve this problem for m = n, i.e., for a square:
For m = n = 1, output: 1
For m = n = 2, output: 4 + 1 [4 of size 1×1 + 1 of size 2×2]
For m = n = 3, output: 9 + 4 + 1 [9 of size 1×1 + 4 of size 2×2 + 1 of size 3×3]
For m = n = 4, output 16 + 9 + 4 + 1 [16 of size 1×1 + 9 of size 2×2 + 4 of size 3×3 + 1 of size 4×4]
In general, it seems to be n^2 + (n-1)^2 + … 1 = n(n+1)(2n+1)/6
Let us solve this problem when m may not be equal to n:
Let us assume that m <= n
From above explanation, we know that number of squares in a m x m matrix is m(m+1)(2m+1)/6
What happens when we add a column, i.e., what is the number of squares in m x (m+1) matrix?
When we add a column, number of squares increased is m + (m-1) + … + 3 + 2 + 1
[m squares of size 1×1 + (m-1) squares of size 2×2 + … + 1 square of size m x m]
Which is equal to m(m+1)/2
So when we add (n-m) columns, total number of squares increased is (n-m)*m(m+1)/2.
So total number of squares is m(m+1)(2m+1)/6 + (n-m)*m(m+1)/2.
Using same logic we can prove when n <= m.
So, in general,
Total number of squares = m x (m+1) x (2m+1)/6 + (n-m) x m x (m+1)/2 when n is larger dimension
Using above logic for rectangle, we can also prove that number of squares in a square is n(n+1)(2n+1)/6
Below is the implementation of above formula.
Count of Squares is 20
Alternate Solution :
Let us take m = 2, n = 3;
The number of squares of side 1 will be 6 as there will be two cases one as squares of 1-unit sides along the horizontal(2) and second case as squares of 1-unit sides along the vertical(3). that give us 2*3 = 6 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 number of squares=2
So we can deduce that
Number of squares of size 1*1 will be m*n
Number of squares of size 2*2 will be (n-1)(m-1)
So like this the number of squares of size n will be 1*(m-n+1)
The final formula for total number of squares will be n*(n+1)(3m-n+1)/6
Count of Squares is 20
Thanks to Pranav for providing this alternate solution.
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.
- Number of squares of maximum area in a rectangle
- Number of squares of side length required to cover an N*M rectangle
- Ratio of area of a rectangle with the rectangle inscribed in it
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Minimum squares to cover a rectangle
- Minimum squares to evenly cut a rectangle
- Count the number of rhombi possible inside a rectangle of given size
- Program to count number of distinct Squares and Cubes upto N
- Count number less than N which are product of perfect squares
- Count the total number of squares that can be visited by Bishop in one move
- Sum of the count of number of adjacent squares in an M X N grid
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Minimum number of squares whose sum equals to given number N | set 2
- Minimum number of squares whose sum equals to a given number N | Set-3
- Minimum number of squares whose sum equals to given number n
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Number of ways N can be divided into four parts to construct a rectangle
- Count all subarrays whose sum can be split as difference of squares of two Integers