Given two integers M and N, the task is to find the minimum number of tiles of size 2 * 1 that can be placed on an M * N grid such that the following conditions are satisfied:
- Each tile must completely cover 2 squares of the board.
- No pair of tiles may overlap.
- Each tile lies must be placed entirely inside the board. It is allowed to touch the edges of the board.
If it is not possible to cover the entire board, print -1
Input: N = 2, M = 4
Explanation: 4 tiles of dimension 2 * 1. Place each tile in one column.
Input: N = 3, M = 3
Approach: Follow the steps below to solve the problem
- If N is even, (N / 2) * M tiles can be placed to cover the entire board.
- If N is odd, tiles of 2 * 1 tiles, since the length is odd which can not be expressed as a multiple of 2
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.