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++
#include <bits/stdc++.h>
using namespace std;
int squares( int l, int b, int a)
{
return ceil (l / ( double )a) * ceil (b / ( double )a);
}
int main()
{
int l = 11, b = 23, a = 14;
cout << squares(l, b, a) << endl;
return 0;
}
|
Java
class GFG
{
static int squares( int l, int b, int a)
{
return ( int )(Math.ceil(l / ( double )a) *
Math.ceil(b / ( double )a));
}
public static void main(String[] args)
{
int l = 11 , b = 23 , a = 14 ;
System.out.println(squares(l, b, a));
}
}
|
Python 3
import math
def squares(l, b, a):
return math.ceil(l / a) * math.ceil(b / a)
if __name__ = = "__main__" :
l = 11
b = 23
a = 14
print (squares(l, b, a))
|
C#
using System;
class GFG
{
static int squares( int l, int b, int a)
{
return ( int )(Math.Ceiling(l / ( double )a) *
Math.Ceiling(b / ( double )a));
}
public static void Main()
{
int l = 11, b = 23, a = 14;
Console.Write(squares(l, b, a));
}
}
|
Javascript
<script>
function squares(l , b , a)
{
return parseInt(Math.ceil(l / a) *
Math.ceil(b / a));
}
var l = 11, b = 23, a = 14;
document.write(squares(l, b, a));
</script>
|
PHP
<?php
function squares( $l , $b , $a )
{
return ceil ( $l / (double) $a ) *
ceil ( $b / (double) $a );
}
$l = 11;
$b = 23;
$a = 14;
echo squares( $l , $b , $a );
?>
|
Time complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
New Approach:- One alternative approach to solve this problem is to use the concept of the greatest common divisor (GCD) between the length and width of the rectangle.
Let’s say the length of the rectangle is l and the width is b. We can find their GCD using the Euclidean algorithm, which states that:
GCD(l, b) = GCD(b, l % b), where “%” denotes the modulo operation.
We can keep applying this formula until b becomes 0, at which point the GCD will be equal to l.
Now, let’s say that the GCD is equal to g. We can divide both the length and width by g, which will give us a new rectangle with dimensions (l/g) x (b/g).
The minimum number of squares needed to cover this new rectangle can be calculated as ceil((l/g) / a) * ceil((b/g) / a).
Finally, the minimum number of squares needed to cover the original rectangle is the product of the GCD and the number of squares needed for the new rectangle, i.e.,
minimum squares = g * ceil((l/g) / a) * ceil((b/g) / a)
Here’s the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int countSquares( int l, int b, int a, int g)
{
return ceil ((l / ( double )g) / a) * ceil ((b / ( double )g) / a);
}
int main()
{
int l = 11, b = 23, a = 14;
int g = gcd(l, b);
int squares = g * countSquares(l, b, a, g);
cout << squares << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int gcd( int a, int b) {
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static int countSquares( int l, int b, int a, int g) {
return ( int ) Math.ceil((l / ( double ) g) / a) * ( int ) Math.ceil((b / ( double ) g) / a);
}
public static void main(String[] args) {
int l = 11 , b = 23 , a = 14 ;
int g = gcd(l, b);
int squares = g * countSquares(l, b, a, g);
System.out.println(squares);
}
}
|
Python3
import math
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def count_squares(l, b, a, g):
return math.ceil((l / g) / a) * math.ceil((b / g) / a)
def main():
l = 11
b = 23
a = 14
g = gcd(l, b)
squares = g * count_squares(l, b, a, g)
print (squares)
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int countSquares( int l, int b, int a, int g)
{
return ( int )Math.Ceiling((l / ( double )g) / a) * ( int )Math.Ceiling((b / ( double )g) / a);
}
static void Main( string [] args)
{
int l = 11, b = 23, a = 14;
int g = gcd(l, b);
int squares = g * countSquares(l, b, a, g);
Console.WriteLine(squares);
}
}
|
Output:-
2
Time complexity: –The time complexity of the given algorithm is O(log(min(l, b))), where l and b are the length and breadth of the rectangle, respectively. This is because the time complexity of the Euclidean algorithm to compute the GCD of two numbers a and b is O(log(min(a, b))), and we are using it to find the GCD of l and b. The time complexity of the countSquares function is O(1) because it performs constant time operations.
Auxiliary Space:- The auxiliary space complexity of the given algorithm is O(1), as it uses a constant amount of extra space to store the variables used in the algorithm.