# Minimum tiles of sizes in powers of two to cover whole area

Given an area of N X M. You have infinite number of tiles of size 2i X 2i, where i = 0, 1, 2,… so on. The task is to find 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to divide the given area into nearest 2i X 2i.
Lets divide the problem into cases:
Case 1: if N is odd and M is even, fill the a 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 area. Similarly, if M is odd and N is even, add N to our answer and find minimum number of tiles for 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 minimum number of tiles required to fill N/2 X M/2 area.

Case 3: If N and M both are even, calculate the minimum number of tiles required to fill area of 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++

 `#include ` `using` `namespace` `std; ` ` `  `int` `minTiles(``int` `n, ``int` `m) ` `{ ` `  ``// base case, when area is 0. ` `  ``if` `(n == 0 || m == 0) ` `    ``return` `0; ` ` `  `  ``// If n and m both are even, calculate tiles for n/2 x m/2 ` `  ``// Halving both dimensions doesn't change the number of tiles ` `  ``else` `if` `(n%2 == 0 && m%2 == 0) ` `    ``return` `minTiles(n/2, m/2); ` `   `  `  ``// If n is even and m is odd ` `  ``// Use a row of 1x1 tiles ` `  ``else` `if` `(n%2 == 0 && m%2 == 1) ` `    ``return` `(n + minTiles(n/2, m/2)); ` ` `  `  ``// If n is odd and m is even ` `  ``// Use a column of 1x1 tiles ` `  ``else` `if` `(n%2 == 1 && m%2 == 0) ` `    ``return` `(m + minTiles(n/2, m/2)); ` ` `  `  ``// If n and m are odd ` `  ``// add row + column number of tiles ` `  ``else` `    ``return` `(n + m - 1 + minTiles(n/2, m/2));  ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `  ``int` `n = 5, m = 6; ` ` `  `  ``cout << minTiles(n, m) << endl; ` `  ``return` `0; ` `} `

## Java

 `// Java code for Minimum tiles of  ` `// sizes in powers of two to cover  ` `// whole area ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `minTiles(``int` `n, ``int` `m) ` `    ``{ ` `    ``// base case, when area is 0. ` `    ``if` `(n == ``0` `|| m == ``0``) ` `        ``return` `0``; ` `     `  `    ``// If n and m both are even,  ` `    ``// calculate tiles for n/2 x m/2 ` `    ``// Halving both dimensions doesn't  ` `    ``// change the number of tiles ` `    ``else` `if` `(n % ``2`  `== ``0` `&& m % ``2` `== ``0``) ` `        ``return` `minTiles(n / ``2``, m / ``2``); ` `         `  `    ``// If n is even and m is odd ` `    ``// Use a row of 1x1 tiles ` `    ``else` `if` `(n % ``2` `== ``0` `&& m % ``2` `== ``1``) ` `        ``return` `(n + minTiles(n / ``2``, m / ``2``)); ` `     `  `    ``// If n is odd and m is even ` `    ``// Use a column of 1x1 tiles ` `    ``else` `if` `(n % ``2` `== ``1` `&& m % ``2` `== ``0``) ` `        ``return` `(m + minTiles(n / ``2``, m / ``2``)); ` `     `  `    ``// If n and m are odd ` `    ``// add row + column number of tiles ` `    ``else` `        ``return` `(n + m - ``1` `+ minTiles(n / ``2``, m / ``2``));  ` `    ``} ` `         `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `            ``int` `n = ``5``, m = ``6``; ` `            ``System.out.println(minTiles(n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `def` `minTiles(n, m): ` `     `  `    ``# base case, when area is 0. ` `    ``if` `n ``=``=` `0` `or` `m ``=``=` `0``: ` `        ``return` `0` ` `  `    ``# If n and m both are even, calculate ` `    ``# tiles for n/2 x m/2 ` `    ``# Halfing both dimensions doesn't ` `    ``# change the number of tiles ` `    ``elif` `n``%``2` `=``=` `0` `and` `m``%``2` `=``=` `0``: ` `        ``return` `minTiles(``int``(n``/``2``), ``int``(m``/``2``)) ` ` `  `    ``# If n is even and m is odd ` `    ``# Use a row of 1x1 tiles ` `    ``elif` `n ``%` `2` `=``=` `0` `and` `m ``%` `2` `=``=` `1``: ` `        ``return` `(n ``+` `minTiles(``int``(n``/``2``), ``int``(m``/``2``))) ` ` `  `    ``# If n is odd and m is even ` `    ``# Use a column of 1x1 tiles ` `    ``elif` `n ``%` `2` `=``=` `1` `and` `m ``%` `2` `=``=` `0``: ` `        ``return` `(m ``+` `minTiles(``int``(n``/``2``), ``int``(m``/``2``))) ` ` `  `    ``# If n and m are odd add ` `    ``# row + column number of tiles ` `    ``else``: ` `        ``return` `(n ``+` `m ``-` `1` `+` `minTiles(``int``(n``/``2``), ``int``(m``/``2``)))  ` ` `  `# Driven Program ` `n ``=` `5` `m ``=` `6` `print` `(minTiles(n, m)) ` ` `  `# This code is contributed ` `# by Shreyanshi Arun. `

## C#

 `// C# code for Minimum tiles of ` `// sizes in powers of two to cover ` `// whole area ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `int` `minTiles(``int` `n, ``int` `m) ` `    ``{ ` `         `  `        ``// base case, when area is 0. ` `        ``if` `(n == 0 || m == 0) ` `            ``return` `0; ` ` `  `        ``// If n and m both are even, ` `        ``// calculate tiles for n/2 x m/2 ` `        ``// Halving both dimensions doesn't ` `        ``// change the number of tiles ` `        ``else` `if` `(n % 2 == 0 && m % 2 == 0) ` `            ``return` `minTiles(n / 2, m / 2); ` ` `  `        ``// If n is even and m is odd ` `        ``// Use a row of 1x1 tiles ` `        ``else` `if` `(n % 2 == 0 && m % 2 == 1) ` `            ``return` `(n + minTiles(n / 2, m / 2)); ` ` `  `        ``// If n is odd and m is even ` `        ``// Use a column of 1x1 tiles ` `        ``else` `if` `(n % 2 == 1 && m % 2 == 0) ` `            ``return` `(m + minTiles(n / 2, m / 2)); ` ` `  `        ``// If n and m are odd ` `        ``// add row + column number of tiles ` `        ``else` `            ``return` `(n + m - 1 + minTiles(n / 2, m / 2)); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 5, m = 6; ` `         `  `        ``Console.WriteLine(minTiles(n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```9
```

This article is contributed by Anuj Chauhan (anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.