Given a floor of size MxN size and tiles of size 2×1, the task is to find the maximum number of tiles required to cover the floor as much as possible of size MxN size.
Note: A tile can either be placed horizontally or vertically and no two tiles should overlap.
Examples:
Input: M = 2, N = 4
Output: 4
Explanation:
4 tiles are needed to cover the floor.
Input: M = 3, N = 3
Output: 4
Explanation:
4 tiles are needed to cover the floor.
Approach:
- If N is even, then the task is to place m rows of (N/2) number of tiles to cover the whole floor.
- Else if N is odd, then cover M rows till N – 1 (even) columns in the same way as discussed in the above point and put (M/2) number of tiles in the last column. If both M and N are odd, then one cell of the floor remains uncovered.
- Therefore, the maximum number of tiles is floor((M * N) / 2).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximumTiles( int n, int m)
{
cout << (m * n) / 2 << endl;
}
int main()
{
int M = 3;
int N = 4;
maximumTiles(N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maximumTiles( int n, int m)
{
System.out.println((m * n) / 2 );
}
public static void main (String[] args)
{
int M = 3 ;
int N = 4 ;
maximumTiles(N, M);
}
}
|
Python3
def maximumTiles(n, m):
print ( int ((m * n) / 2 ));
if __name__ = = '__main__' :
M = 3 ;
N = 4 ;
maximumTiles(N, M);
|
C#
using System;
class GFG{
static void maximumTiles( int n, int m)
{
Console.WriteLine((m * n) / 2);
}
public static void Main(String[] args)
{
int M = 3;
int N = 4;
maximumTiles(N, M);
}
}
|
Javascript
<script>
function maximumTiles(n, m)
{
document.write((m * n) / 2);
}
let M = 3;
let N = 4;
maximumTiles(N, M);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)