Number of squares of side length required to cover an N*M rectangle - GeeksforGeeks

20 May, 2021
Given three numbers , , . Find Number of squares of dimension required to cover rectangle. Note:  
It’s allowed to cover the surface larger than the rectangle, but the rectangle has to be covered.
It’s not allowed to break a square.
The sides of squares should be parallel to the sides of the rectangle.
Examples:  
Input: N = 6, M = 6, a = 4
Output: 4

Input: N = 2, M = 3, a = 1
Output: 6
 
Approach: An efficient approach is to make an observation and find a formula. The constraint that edges of each square must be parallel to the edges of the rectangle allows to analyze X and Y axes separately, that is, how many squares of length ‘a’ are needed to cover squares of length ‘m’ and ‘n’ and take the product of these two quantities. The number of small squares of side length ‘a’ required to cover ‘m’ sized square are ceil(m/a). Similarly, number of ‘a’ sized squares required to cover ‘n’ sized square are ceil(n/a). So, the answer will be ceil(m/a)*ceil(n/a).Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// CPP program to find number of squares// of a*a required to cover n*m rectangle#include <bits/stdc++.h>using namespace std; // function to find number of squares// of a*a required to cover n*m rectangleint Squares(int n, int m, int a){    return ((m + a - 1) / a) * ((n + a - 1) / a);} // Driver codeint main(){    int n = 6, m = 6, a = 4;     // function call    cout << Squares(n, m, a);     return 0;}
// Java program to find number of squares// of a*a required to cover n*m rectangleimport java.util.*; class solution{ // function to find a number of squares// of a*a required to cover n*m rectanglestatic int Squares(int n, int m, int a){     return ((m + a - 1) / a) * ((n + a - 1) / a); } // Driver codepublic static void main(String arr[]){    int n = 6, m = 6, a = 4;     // function call    System.out.println(Squares(n, m, a)); } }//This code is contributed by Surendra_Gangwar
# Python 3 program to find number# of squares of a*a required to# cover n*m rectangle # function to find number of# squares of a*a required to# cover n*m rectangledef Squares(n, m, a):    return (((m + a - 1) // a) *            ((n + a - 1) // a)) # Driver codeif __name__ == "__main__":    n = 6    m = 6    a = 4     # function call    print(Squares(n, m, a)) # This code is contributed# by ChitraNayal
// CSHARP program to find number of squares// of a*a required to cover n*m rectangle using System; class GFG{    // function to find a number of squares    // of a*a required to cover n*m rectangle    static int Squares(int n, int m, int a)    {             return ((m + a - 1) / a) * ((n + a - 1) / a);         }     static void Main()    {          int n = 6, m = 6, a = 4;          // function call         Console.WriteLine(Squares(n, m, a));    }    // This code is contributed by ANKITRAI1}
<?php// PHP program to find number of squares// of a*a required to cover n*m rectangle // function to find number of squares// of a*a required to cover n*m rectanglefunction Squares($n, $m, $a){    return ((int)(($m + $a - 1) / $a)) *           ((int)(($n + $a - 1) / $a));} // Driver code$n = 6; $m = 6; $a = 4; // function callecho Squares($n, $m, $a); // This code is contributed// by Akanksha Rai?>
<script> // JavaScript program to find number of squares// of a*a required to cover n*m rectangle // function to find a number of squares// of a*a required to cover n*m rectanglefunction Squares(n, m, a){    return parseInt(((m + a - 1) / a)) *           parseInt(((n + a - 1) / a));} // Driver codevar n = 6, m = 6, a = 4; // Function calldocument.write(Squares(n, m, a));       // This code is contributed by Ankita saini </script>
4
 
SURENDRA_GANGWAR
ankthon
ukasp
Akanksha_Rai
ankita_saini
square-rectangle
Competitive Programming
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Prefix Sum Array - Implementation and Applications in Competitive Programming
Ordered Set and GNU C++ PBDS
Modulo 10^9+7 (1000000007)
Bits manipulation (Important tactics)
What is Competitive Programming and How to Prepare for It?
Closest Pair of Points using Divide and Conquer algorithm
How to check if a given point lies inside or outside a polygon?
Program for distance between two points on earth
How to check if two given line segments intersect?
Find if two rectangles overlap