Minimum tiles of sizes in powers of two to cover whole area
Last Updated :
25 May, 2022
Given an area of N X M. You have the infinite number of tiles of size 2i X 2i, where i = 0, 1, 2,… so on. The task is to find the minimum number of tiles required to fill the given area with tiles.
Examples:
Input : N = 5, M = 6.
Output : 9
Area of 5 X 6 can be covered with minimum 9 tiles.
6 tiles of 1 X 1, 2 tiles of 2 X 2, 1 tile of 4 X 4.
Input : N = 10, M = 5.
Output : 14
The idea is to divide the given area into the nearest 2i X 2i.
Let’s divide the problem into cases:
Case 1: if N is odd and M is even, fill the row or column with M number of 1 X 1 tiles. Then count the minimum number of tiles for N/2 X M/2 size of the area. Similarly, if M is odd and N is even, add N to our answer and find a minimum number of tiles for the N/2 X M/2 area.
Case 2: If N and M both are odd, fill one row and one column, so add N + M – 1 to the answer and find the minimum number of tiles required to fill the N/2 X M/2 area.
Case 3: If N and M both are even, calculate the minimum number of tiles required to fill the area with N/2 X M/2 area. Because halving both the dimensions doesn’t change the number of tiles required.
Below is the implementation of this approach:
C++
< div id= "highlighter_190951" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" >< div class = "container" >< div class = "line number1 index0 alt2" ><code class = "preprocessor" >#include<bits/stdc++.h></code></ div >< div class = "line number2 index1 alt1" ><code class = "keyword bold" > using </code> <code class = "keyword bold" > namespace </code> <code class = "plain" >std;</code></ div >< div class = "line number3 index2 alt2" > </ div >< div class = "line number4 index3 alt1" ><code class = "color1 bold" > int </code> <code class = "plain" >minTiles(</code><code class = "color1 bold" > int </code> <code class = "plain" >n, </code><code class = "color1 bold" > int </code> <code class = "plain" >m)</code></ div >< div class = "line number5 index4 alt2" ><code class = "plain" >{</code></ div >< div class = "line number6 index5 alt1" ><code class = "undefined spaces" > </code><code class = "comments" >
|
Java
class GFG {
static int minTiles( int n, int m)
{
if (n == 0 || m == 0 )
return 0 ;
else if (n % 2 == 0 && m % 2 == 0 )
return minTiles(n / 2 , m / 2 );
else if (n % 2 == 0 && m % 2 == 1 )
return (n + minTiles(n / 2 , m / 2 ));
else if (n % 2 == 1 && m % 2 == 0 )
return (m + minTiles(n / 2 , m / 2 ));
else
return (n + m - 1 + minTiles(n / 2 , m / 2 ));
}
public static void main (String[] args)
{
int n = 5 , m = 6 ;
System.out.println(minTiles(n, m));
}
}
|
Python3
def minTiles(n, m):
if n = = 0 or m = = 0 :
return 0
elif n % 2 = = 0 and m % 2 = = 0 :
return minTiles( int (n / 2 ), int (m / 2 ))
elif n % 2 = = 0 and m % 2 = = 1 :
return (n + minTiles( int (n / 2 ), int (m / 2 )))
elif n % 2 = = 1 and m % 2 = = 0 :
return (m + minTiles( int (n / 2 ), int (m / 2 )))
else :
return (n + m - 1 + minTiles( int (n / 2 ), int (m / 2 )))
n = 5
m = 6
print (minTiles(n, m))
|
C#
<div id= "highlighter_324618" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" ><div class = "container" ><div class = "line number1 index0 alt2" ><code class = "comments" >
|
PHP
<div id= "highlighter_613659" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" ><div class = "container" ><div class = "line number1 index0 alt2" ><code class = "plain" ><?php</code></div><div class = "line number2 index1 alt1" ><code class = "comments" >
|
Javascript
<script>
function minTiles(n, m)
{
if (n == 0 || m == 0)
return 0;
else if (n % 2 == 0 && m % 2 == 0)
return minTiles((n / 2),(m / 2));
else if (n % 2 == 0 && m % 2 == 1)
return (n + minTiles(Math.floor(n / 2), Math.floor(m / 2)));
else if (n % 2 == 1 && m % 2 == 0)
return (m + minTiles(Math.floor(n / 2), Math.floor(m / 2)));
else
return (n + m - 1 + minTiles(Math.floor(n / 2), Math.floor(m / 2)));
}
let n = 5, m = 6;
document.write(Math.floor(minTiles(n, m)));
</script>
|
Output:
9
Complexity Analysis:
Time Complexity: O(log(min(n,m))/log(2)), where n and m are dimensions of tiles.
Auxiliary Space: O(log(min(n,m))/log(2)), because we are adding an element to the stack during each recursion call.
Share your thoughts in the comments
Please Login to comment...