Minimum squares to cover a rectangle

13 Jun, 2022
Given a rectangle with length l and breadth b, we need to find the minimum number of squares that can cover the surface of the rectangle, given that each square has a side of length a. It is allowed to cover the surface larger than the rectangle, but the rectangle has to be covered. It is not allowed to break the square.Examples:  
Input : 1 2 3
Output :1
We have a 3x3 square and we need
to make a rectangle of size 1x2.
So we need only 1 square to cover the
rectangle.

Input : 11 23 14
Output :2
 
The only way to actually fill the rectangle optimally is to arrange each square such that it is parallel to the sides of the rectangle. So we just need to find the number of squares to fully cover the length and breadth of the rectangle. The length of the rectangle is l, and if the side length of the square is a divided l, then there must be l/a squares to cover the full length of l. If l isn’t divisible by a, we need to add 1 to l/a, to round it down. For this, we can use the ceil function, as ceil(x) returns the least integer which is above or equal to x. We can do the same with the rectangle width and take the number of squares across the width to be ceil(b/a). So, total number of squares=ceil(m/a) * ceil(n/a). 
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to find the minimum number// of squares to cover the surface of the// rectangle with given dimensions#include <bits/stdc++.h>using namespace std;int squares(int l, int b, int a){    // function to count    // the number of squares that can    // cover the surface of the rectangle    return ceil(l / (double)a) * ceil(b / (double)a);} // Driver codeint main(){    int l = 11, b = 23, a = 14;    cout << squares(l, b, a) << endl;    return 0;}
// Java program to find the minimum number// of squares to cover the surface of the// rectangle with given dimensionsclass GFG{static int squares(int l, int b, int a){     // function to count// the number of squares that can// cover the surface of the rectanglereturn (int)(Math.ceil(l / (double)a) *             Math.ceil(b / (double)a));} // Driver codepublic static void main(String[] args){    int l = 11, b = 23, a = 14;    System.out.println(squares(l, b, a));}} // This code is contributed by ChitraNayal
# Python3 program to find the minimum number# of squares to cover the surface of the# rectangle with given dimensionsimport math def squares(l, b, a):         # function to count    # the number of squares that can    # cover the surface of the rectangle    return math.ceil(l / a) * math.ceil(b / a) # Driver codeif __name__ == "__main__":    l = 11    b = 23    a = 14    print(squares(l, b, a)) # This code is contributed# by ChitraNayal
// C# program to find the minimum number// of squares to cover the surface of the// rectangle with given dimensionsusing System; class GFG{static int squares(int l, int b, int a){     // function to count// the number of squares that can// cover the surface of the rectanglereturn (int)(Math.Ceiling(l / (double)a) *             Math.Ceiling(b / (double)a));} // Driver codepublic static void Main(){    int l = 11, b = 23, a = 14;    Console.Write(squares(l, b, a));}} // This code is contributed by ChitraNayal
<?php// PHP program to find the minimum number// of squares to cover the surface of the// rectangle with given dimensions function squares($l, $b, $a){    // function to count    // the number of squares that can    // cover the surface of the rectangle    return ceil($l / (double)$a) *           ceil($b / (double)$a);} // Driver code$l = 11;$b = 23;$a = 14;echo squares($l, $b, $a); // This code is contributed// by ChitraNayal?>
<script> // javascript program to find the minimum number// of squares to cover the surface of the// rectangle with given dimensions function squares(l , b , a){         // function to count    // the number of squares that can    // cover the surface of the rectangle    return parseInt(Math.ceil(l / a) *                 Math.ceil(b / a));} // Driver code var l = 11, b = 23, a = 14;document.write(squares(l, b, a)); // This code is contributed by Amit Katiyar </script>
2
 
Time complexity: O(1)
Auxiliary Space: O(1)
YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=5M08zWeC6x4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
ukasp
amit143katiyar
everyonemaurya7
hasani
square-rectangle
Geometric
Mathematical
School Programming
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimum location of point to minimize total distance
Check whether a given point lies inside a triangle or not
Program for Point of Intersection of Two Lines
Given n line segments, find if any two segments intersect
Window to Viewport Transformation in Computer Graphics with Implementation
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays