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);
}
}
Time Complexity : O(1)
Auxiliary Space : O(1)