# Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m

• Last Updated : 09 Jun, 2022

Given two integers nm. Find the number of rectangles of size 2*1 that can be placed inside a rectangle of size n*m.
Note:

1. No two small rectangles overlap.
2. Each small rectangle lies entirely inside the large rectangle. It is allowed to touch the edges of the large rectangle.

Examples

Input : n = 3, m =3
Output : 4

Input : n = 2, m = 4
Output : 4

Approach:

1. If N is even, then place M rows of N/2 small rectangles and cover the whole large rectangle.
2. If M is even, then place N rows of M/2 small rectangles and cover the whole large rectangle.
3. If both are odd then cover N – 1 row of the board with small rectangles and put floor(M/2) small rectangles to the last row. In the worst case (N and M are odd) one cell remains uncovered.

Below is the implementation of the above approach:

## C++

 // CPP program to Find the number of// rectangles of size 2*1 can be placed// inside a rectangle of size n*m#include using namespace std; // function to Find the number of// rectangles of size 2*1 can be placed// inside a rectangle of size n*mint NumberOfRectangles(int n, int m){    // if n is even    if (n % 2 == 0)        return (n / 2) * m;     // if m is even    else if (m % 2 == 0)        return (m / 2) * n;     // if both are odd    return (n * m - 1) / 2;} // Driver codeint main(){    int n = 3, m = 3;     // function call    cout << NumberOfRectangles(n, m);     return 0;}

## C

 // C program to Find the number of// rectangles of size 2*1 can be placed// inside a rectangle of size n*m#include  // function to Find the number of// rectangles of size 2*1 can be placed// inside a rectangle of size n*mint NumberOfRectangles(int n, int m){    // if n is even    if (n % 2 == 0)        return (n / 2) * m;     // if m is even    else if (m % 2 == 0)        return (m / 2) * n;     // if both are odd    return (n * m - 1) / 2;} // Driver codeint main(){    int n = 3, m = 3;     // function call    printf("%d",NumberOfRectangles(n, m));     return 0;} // This code is contributed by kothavvsaakash.

## Java

 // Java program to Find the number of// rectangles of size 2*1 can be placed// inside a rectangle of size n*m public class GFG {         // function to Find the number of    // rectangles of size 2*1 can be placed    // inside a rectangle of size n*m    static int NumberOfRectangles(int n, int m)    {        // if n is even        if (n % 2 == 0)            return (n / 2) * m;               // if m is even        else if (m % 2 == 0)            return (m / 2) * n;               // if both are odd        return (n * m - 1) / 2;    }    public static void main(String args[])    {         int n = 3, m = 3;                       // function call            System.out.println(NumberOfRectangles(n, m));               }    // This Code is contributed by ANKITRAI1}

## Python3

 # Python 3 program to Find the# number of rectangles of size# 2*1 can be placed inside a# rectangle of size n*m # function to Find the number# of rectangles of size 2*1# can be placed inside a# rectangle of size n*mdef NumberOfRectangles(n, m):     # if n is even    if (n % 2 == 0):        return (n / 2) * m     # if m is even    elif (m % 2 == 0):        return (m // 2) * n     # if both are odd    return (n * m - 1) // 2 # Driver codeif __name__ == "__main__":    n = 3    m = 3     # function call    print(NumberOfRectangles(n, m)) # This code is contributed# by ChitraNayal

## C#

 // C# program to Find the number of// rectangles of size 2*1 can be placed// inside a rectangle of size n*musing System; class GFG{     // function to Find the number of// rectangles of size 2*1 can be placed// inside a rectangle of size n*mstatic int NumberOfRectangles(int n, int m){    // if n is even    if (n % 2 == 0)        return (n / 2) * m;     // if m is even    else if (m % 2 == 0)        return (m / 2) * n;     // if both are odd    return (n * m - 1) / 2;} // Driver Codepublic static void Main(){    int n = 3, m = 3;     // function call    Console.WriteLine(NumberOfRectangles(n, m));     }// This code is contributed// by Akanksha Rai(Abby_akku)}



## Javascript



Output

4

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up