Number of possible squares
Last Updated :
11 Dec, 2025
Given an N × N chessboard. Find the total number of distinct squares that can be formed on this board.
Note: A square can be of any size: 1×1, 2×2, 3×3, …, up to N×N.
Examples:
Input: N = 3
Output: 14
Explanation: A 3×3 chessboard contains:
9 squares of size 1×1
4 squares of size 2×2
1 square of size 3×3
Total = 9 + 4 + 1 = 14
Input: N = 5
Output: 55
Explanation: A 5×5 chessboard contains:
25 squares of size 1×1
16 squares of size 2×2
9 squares of size 3×3
4 squares of size 4×4
1 square of size 5×5
Total = 25 + 16 + 9 + 4 + 1 = 55
[Naive Approach] Brute Force - O(N^3) Time and O(1) Space
The naive idea is to count all possible squares by checking every size from 1×1 up to N×N. For each square size k, we slide a k×k window across the board and count how many such squares fit, which is (N − k + 1)². We repeat this for all k and add them up to get the total number of squares. This method is simple and intuitive but runs slower for large N because it explicitly checks all square sizes.
C++
#include <iostream>
using namespace std;
long long totNumOfSquares(long long N) {
long long total = 0;
// try every top-left corner
for (long long i = 0; i < N; i++) {
for (long long j = 0; j < N; j++) {
// try every square size k
for (long long k = 1; k <= N; k++) {
// check if k×k fits starting at (i,j)
if (i + k <= N && j + k <= N)
total++;
}
}
}
return total;
}
int main() {
long long N = 3;
cout << totNumOfSquares(N);
return 0;
}
Java
class GFG {
static long totNumOfSquares(long N) {
long total = 0;
for (long i = 0; i < N; i++) {
for (long j = 0; j < N; j++) {
for (long k = 1; k <= N; k++) {
if (i + k <= N && j + k <= N)
total++;
}
}
}
return total;
}
public static void main(String[] args) {
long N = 3;
System.out.println(totNumOfSquares(N));
}
}
Python
def totNumOfSquares(N):
total = 0
for i in range(N):
for j in range(N):
for k in range(1, N + 1):
if i + k <= N and j + k <= N:
total += 1
return total
if __name__ == "__main__":
N = 3
print(totNumOfSquares(N))
C#
using System;
class GFG {
static long totNumOfSquares(long N) {
long total = 0;
for (long i = 0; i < N; i++) {
for (long j = 0; j < N; j++) {
for (long k = 1; k <= N; k++) {
if (i + k <= N && j + k <= N)
total++;
}
}
}
return total;
}
static void Main() {
long N = 3;
Console.WriteLine(totNumOfSquares(N));
}
}
JavaScript
function totNumOfSquares(N) {
let total = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
for (let k = 1; k <= N; k++) {
if (i + k <= N && j + k <= N)
total++;
}
}
}
return total;
}
// Driver code
let N = 3;
console.log(totNumOfSquares(N));
[Better Approach] Linear Solution - O(N) Time and O(1) Space
The idea is to count squares by size, not by position. A chessboard has 1×1 squares, 2×2 squares, …, N×N squares. A k×k square can fit only in (N − k + 1) rows and columns, so there are (N − k + 1)² such squares. Adding this for all k gives the total number of squares.
C++
#include <iostream>
using namespace std;
long long totNumOfSquares(long long N) {
long long total = 0;
// add k^2 for each square size
for (long long k = 1; k <= N; k++)
total += k * k;
return total;
}
int main() {
long long N = 3;
cout << totNumOfSquares(N);
return 0;
}
Java
class GFG {
static long totNumOfSquares(long N) {
long total = 0;
// add k^2 for each square size
for (long k = 1; k <= N; k++)
total += k * k;
return total;
}
public static void main(String[] args) {
long N = 3;
System.out.println(totNumOfSquares(N));
}
}
Python
def totNumOfSquares(N):
total = 0
# add k^2 for each square size
for k in range(1, N + 1):
total += k * k
return total
if __name__ == "__main__":
N = 3
print(totNumOfSquares(N))
C#
using System;
class GFG {
static long totNumOfSquares(long N) {
long total = 0;
// add k^2 for each square size
for (long k = 1; k <= N; k++)
total += k * k;
return total;
}
static void Main() {
long N = 3;
Console.WriteLine(totNumOfSquares(N));
}
}
JavaScript
function totNumOfSquares(N) {
let total = 0;
// add k^2 for each square size
for (let k = 1; k <= N; k++)
total += k * k;
return total;
}
// Driver code
let N = 3;
console.log(totNumOfSquares(N));
[Expected Approach] Optimal solution - O(1) Time and O(1) Space
A chessboard contains squares of many sizes. A 1×1 square can appear everywhere, a 2×2 square has fewer possible positions, a 3×3 square even fewer, and so on. For a k×k square, we can slide it horizontally and vertically, and it fits in exactly (N−k+1)×(N−k+1) places.
So the total number of squares is the sum of all these counts: 1² + 2² + 3² + … + N².
This sum has a known closed-form formula: (N * (N+1) * (2N+1)) / 6.
C++
#include <iostream>
using namespace std;
// compute total squares using formula
long long totNumOfSquares(long long N) {
return N * (N + 1) * (2 * N + 1) / 6;
}
int main() {
long long N = 3;
cout << totNumOfSquares(N);
return 0;
}
Java
class GFG {
// compute total squares using formula
static long totNumOfSquares(long N) {
return N * (N + 1) * (2 * N + 1) / 6;
}
public static void main(String[] args) {
long N = 3;
System.out.println(totNumOfSquares(N));
}
}
Python
# compute total squares using formula
def totNumOfSquares(N):
return N * (N + 1) * (2 * N + 1) // 6
if __name__ == "__main__":
N = 3
print(totNumOfSquares(N))
C#
using System;
class GFG {
// compute total squares using formula
static long totNumOfSquares(long N) {
return N * (N + 1) * (2 * N + 1) / 6;
}
static void Main() {
long N = 3;
Console.WriteLine(totNumOfSquares(N));
}
}
JavaScript
// compute total squares using formula
function totNumOfSquares(N) {
N = BigInt(N);
return (N * (N + 1n) * (2n * N + 1n)) / 6n;
}
// driver code
let N = 3;
console.log(String(totNumOfSquares(N)));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem