Count squares of side A that fit in an N by M rectangle

Updated

Given a rectangle of size N*M, find the number of squares of size A that can be placed in the rectangle such that no two squares overlaps each other.

Note : It is okay for squares to share a common side.

Examples :

Input : N = 4, M = 6, A = 3
Output : 2
Explanation :
A square with diagonal having end points at (0, 0) and (3, 3), and
another square with diagonal having end points at (3, 0) and (6, 3)

Input : N = 4, M = 3, A = 3
Output : 1
Explanation :
Only one square can be placed.

Approach :

The number of squares that can be placed is equal to the number of groups of rows of size A * number of groups of columns of size A.

That is, number of squares = N/A * M/A

Implementation :

// Java Program for
// the above approach
class Main {
    public static void findCount(int N, int M, int A)
    {
        int count = (N / A) * (M / A);
        System.out.println(count);
    }
    public static void main(String[] args)
    {
        int N = 4, M = 6, A = 3;
        findCount(N, M, A);
    }
}
Output

2

Time Complexity : O(1)

Auxiliary Space : O(1) 

Comment
Next tutorial
Mithlesh Upadhyay Published 27 Nov 2026 · 1 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials