Solution: Looking closely at the chessboard we can see that in addition to the 1 x 1 squares, there can be a combination of 2 x 2, 3 x 3, 4 x 4, 5 x 5, 6 x 6, 7 x 7, and 8 x 8 squares too. To get the total number of squares we need to find all the squares formed.
1 x 1: 8 * 8 = 64 squares. 2 x 2: 7 * 7 = 49 squares. 3 x 3: 6 * 6 = 36 squares. 4 x 4: 5 * 5 = 25 squares. 5 x 5: 4 * 4 = 16 squares. 6 x 6: 3 * 3 = 9 squares. 7 x 7: 2 * 2 = 4 squares. 8 x 8: 1 * 1 = 1 square.
Therefore, we have in all = 64 + 49 + 36 + 25 + 16 + 9 + 4 + 1 = 204 squares in a chessboard.
Given an n x n grid, count squares in it.
Input: n = 2 Output: 5 (4 squares of 1 unit + 1 square of 2 units) Input: n = 3 Output: 14 (9 squares of 1 unit + 4 square of 2 units + 1 square of 1 unit)
For a grid of size n*n the total number of squares formed are:
1^2 + 2^2 + 3^2 + ... + n^2 = n(n+1)(2n+1) / 6
Below is the implementation of the above formula. Since the value of n*(n+1)*(2n+1) can cause overflow for large values of n, below are some interesting tricks used in the program.
- long int is used in return.
- n * (n + 1) / 2 is evaluated first as the value n*(n+1) will always be a multiple of 2.
Note that overflow may still happen, but above tricks just reduce chances of overflow.
Count of squares is 30
This article is contributed by Rishabh. 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.
- Count squares with odd side length in Chessboard
- Puzzle | Dividing a Square into N smaller squares
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Count Distinct Rectangles in N*N Chessboard
- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Program to count number of distinct Squares and Cubes upto N
- Find the number of squares inside the given square grid
- 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 a point that lies inside exactly K given squares
- Find the side of the squares which are inclined diagonally and lined in a row
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Find K numbers with sum equal to N and sum of their squares maximized
- Find two numbers such that difference of their squares equal to N
- Count number of squares in a rectangle
- Number of perfect squares between two given numbers