Minimum number of square tiles required to fill the rectangular floor
Given a rectangular floor of (M X N) meters is to be paved with square tiles of (s X s). The task is to find the minimum number of tiles required to pave the rectangular floor.
Constraints:
- It’s allowed to cover the surface larger than the floor, but the floor has to be covered.
- It’s not allowed to break the tiles.
- The sides of tiles should be parallel to the sides of the floor.
Examples:
Input: 2 1 2
Output: 1
Explanation:
length of floor = 2
breadth of floor = 1
length of side of tile = 2
No of tiles required for paving is 2
Input: 222 332 5
Output: 3015
Approach:
It is given that the edges of each tile must be parallel to the edges of the tiles allows us to analyze X and Y axes separately, that is, how many segments of length ‘s’ are needed to cover a segment of length’ and ‘N’ — and take the product of these two quantities.
ceil(M/s) * ceil(N/s)
, where ceil(x) is the least integer that is above or equal to x. Using integers only, it is usually written as
((M + s - 1) / s)*((N + s - 1) / s)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int M, int N, int s)
{
if (N % s == 0) {
N = N / s;
}
else {
N = (N / s) + 1;
}
if (M % s == 0) {
M = M / s;
}
else {
M = (M / s) + 1;
}
return M * N;
}
int main()
{
int N = 12, M = 13, s = 4;
cout << solve(M, N, s);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int solve( int M, int N, int s)
{
if (N % s == 0 )
{
N = N / s;
}
else
{
N = (N / s) + 1 ;
}
if (M % s == 0 )
{
M = M / s;
}
else
{
M = (M / s) + 1 ;
}
return M * N;
}
public static void main(String args[])
{
int N = 12 , M = 13 , s = 4 ;
System.out.println(solve(M, N, s));
}
}
|
Python3
def solve(M, N, s) :
if (N % s = = 0 ) :
N = N / / s
else :
N = (N / / s) + 1
if (M % s = = 0 ) :
M = M / / s
else :
M = (M / / s) + 1
return M * N
if __name__ = = "__main__" :
N, M, s = 12 , 13 , 4
print (solve(M, N, s))
|
C#
using System;
class GFG
{
static int solve( int M, int N, int s)
{
if (N % s == 0)
{
N = N / s;
}
else
{
N = (N / s) + 1;
}
if (M % s == 0)
{
M = M / s;
}
else
{
M = (M / s) + 1;
}
return M * N;
}
static void Main()
{
int N = 12, M = 13, s = 4;
Console.WriteLine(solve(M, N, s));
}
}
|
PHP
<?php
function solve( $M , $N , $s )
{
if ( $N % $s == 0)
{
$N = $N / $s ;
}
else
{
$N = ( $N / $s ) + 1;
}
if ( $M % $s == 0)
{
$M = $M / $s ;
}
else
{
$M = ( $M / $s ) + 1;
}
return (int) $M * $N ;
}
$N = 12;
$M = 13;
$s = 4;
echo solve( $M , $N , $s );
?>
|
Javascript
<script>
function solve(M, N, s)
{
if (N % s == 0)
{
N = N / s;
}
else
{
N = (N / s) + 1;
}
if (M % s == 0)
{
M = M / s;
}
else
{
M = (M / s) + 1;
}
return parseInt(M * N);
}
var N = 12, M = 13, s = 4;
document.write(solve(M, N, s));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Using ceil function:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( double M, double N, double s)
{
int ans = (( int )( ceil (M / s)) * ( int )( ceil (N / s)));
return ans;
}
int main()
{
double N = 12, M = 13, s = 4;
cout << solve(M, N, s);
return 0;
}
|
Java
class GFG
{
static int solve( double M,
double N, double s)
{
int ans = (( int )(Math.ceil(M / s)) *
( int )(Math.ceil(N / s)));
return ans;
}
public static void main(String[] args)
{
double N = 12 , M = 13 , s = 4 ;
System.out.println(solve(M, N, s));
}
}
|
Python3
import math
def solve(M, N, s):
ans = ((math.ceil(M / s)) *
(math.ceil(N / s)));
return ans
if __name__ = = "__main__" :
N = 12
M = 13
s = 4
print (solve(M, N, s))
|
C#
using System;
class GFG
{
static int solve( double M,
double N, double s)
{
int ans = (( int )(Math.Ceiling(M / s)) *
( int )(Math.Ceiling(N / s)));
return ans;
}
public static void Main()
{
double N = 12, M = 13, s = 4;
Console.WriteLine(solve(M, N, s));
}
}
|
PHP
<?php
function solve( $M , $N , $s )
{
$ans = ((int)( ceil ( $M / $s )) *
(int)( ceil ( $N / $s )));
return $ans ;
}
$N = 12;
$M = 13;
$s = 4;
echo solve( $M , $N , $s );
?>
|
Javascript
<script>
function solve(M, N, s)
{
let ans = Math.floor(((Math.ceil(M / s)) *
(Math.ceil(N / s))));
return ans;
}
let N = 12, M = 13, s = 4;
document.write(solve(M, N, s));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
25 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...