Fill The Grid CodeChef Solution

Problem – Fill The Grid CodeChef Solution

You have a grid with N rows and M columns. You have two types of tiles — one of dimensions 2×2 and the other of dimensions 1×1. You want to cover the grid using these two types of tiles in such a way that:

  • Each cell of the grid is covered by exactly one tile; and
  • The number of 1×1 tiles used is minimized.

Find the minimum number of 1×1 tiles you have to use to fill the grid.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of a single line containing two space-separated integers N,M.

Output Format

For each test case, print on a new line the minimum number of 1×1 tiles needed to fill the grid.

Constraints

  • 1≤T≤10^4
  • 1≤N,M≤10^4

Sample 1:

Input:
4
1 1
4 5
6 8
3 2
Output:
1
4
0
2

Explanation:

Test case 1: There is only one square in the grid, and it must be filled with a single 1×1 tile.

Test case 2: One way of tiling the grid using 1×1 tiles exactly 4 times is as follows:

sample 2

Test case 3: One way of tiling the grid using no 1×1 tiles is as follows:

sample 3

Test case 4: One way of tiling the grid using 1×1 tiles exactly twice is:

sample 4

Fill The Grid CodeChef Solution in Pyth 3

# cook your dish here
for _ in range(int(input())):
    x,y=map(int,input().split())
    if x%2==0 and y%2==0:
        print(0)
    elif x%2!=0 and y%2!=0:
        print(x+y-1)
    else:
        if x%2==0:
            print(x)
        else:
            print(y)

Fill The Grid CodeChef Solution in C++17

#include <iostream>
using namespace std;

int main() {
	int t,n,m;
	cin>>t;
	
	while(t--)
	{
	    cin>>n>>m;
	    int tile2=(n/2)*(m/2);
	    int remaining=(n*m)-(tile2*4);
	    
	    cout<<remaining<<endl;
	}
	return 0;
}

Fill The Grid CodeChef Solution in Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0)
		{
		    int n=sc.nextInt();
		    int m=sc.nextInt();
		    int n1=n%2;
		    int m1=m%2;
		    n1*=m;
		    m1*=n;
		    int c=n1+m1;
		    if(n1!=0 && m1!=0)
		    {
		        c=c-1;
		    }
		    System.out.println(c);
		    t--;
		}
		
	}
}
Fill The Grid CodeChef Solution Review:

In our experience, we suggest you solve this Fill The Grid CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Fill The Grid CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Fill The Grid CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *