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

Given two integers n, m. Find the number of rectangles of size 2*1 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
#include <bits/stdc++.h>
using namespace std;
  
// function to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
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 code
int main()
{
    int n = 3, m = 3;
  
    // function call
    cout << NumberOfRectangles(n, m);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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*m
def 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 code
if __name__ == "__main__":
    n = 3
    m = 3
  
    # function call
    print(NumberOfRectangles(n, m))
  
# This code is contributed
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Find the number of 
// rectangles of size 2*1 can be placed 
// inside a rectangle of size n*m 
using System;
  
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; 
  
// Driver Code
public static void Main()
{
    int n = 3, m = 3; 
  
    // function call 
    Console.WriteLine(NumberOfRectangles(n, m)); 
      
}
// This code is contributed 
// by Akanksha Rai(Abby_akku)
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP 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*m
function NumberOfRectangles($n, $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 code
$n = 3;
$m = 3;
  
// function call
echo NumberOfRectangles($n, $m);
  
// This code is contributed 
// by Shivi_Aggarwal 
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

pawanasipugmailcom

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.