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++
#include <bits/stdc++.h>
using namespace std;
int Squares(int n, int m, int a)
{
return ((m + a - 1) / a) * ((n + a - 1) / a);
}
int main()
{
int n = 6, m = 6, a = 4;
cout << Squares(n, m, a);
return 0;
}
|
Java
import java.util.*;
class solution
{
static int Squares(int n, int m, int a)
{
return ((m + a - 1) / a) * ((n + a - 1) / a);
}
public static void main(String arr[])
{
int n = 6, m = 6, a = 4;
System.out.println(Squares(n, m, a));
}
}
|
Python 3
def Squares(n, m, a):
return (((m + a - 1) // a) *
((n + a - 1) // a))
if __name__ == "__main__":
n = 6
m = 6
a = 4
print(Squares(n, m, a))
|
C#
using System;
class GFG
{
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;
Console.WriteLine(Squares(n, m, a));
}
}
|
PHP
<?php
function Squares($n, $m, $a)
{
return ((int)(($m + $a - 1) / $a)) *
((int)(($n + $a - 1) / $a));
}
$n = 6; $m = 6; $a = 4;
echo Squares($n, $m, $a);
?>
|
Javascript
<script>
function Squares(n, m, a)
{
return parseInt(((m + a - 1) / a)) *
parseInt(((n + a - 1) / a));
}
var n = 6, m = 6, a = 4;
document.write(Squares(n, m, a));
</script>
|