Maximum given sized rectangles that can be cut out of a sheet of paper
Given the length L and breadth B of a sheet of paper, the task is to find the maximum number of rectangles with given length l and breadth b that can be cut from this sheet of paper.
Examples:
Input: L = 5, B = 2, l = 14, b = 3
Output: 0
The sheet is smaller than the required rectangle. So, no rectangle of the given dimension can be cut from the sheet.
Input: L = 10, B = 7, l = 4, b = 3
Output: 4
Approach:
- Try to cut the rectangles horizontally i.e. length of the rectangle is aligned with the length of the sheet and breadth of the rectangle is aligned with the breadth of the sheet and store the count of rectangles possible in horizontal.
- Repeat the same with vertical alignment i.e. when length of the rectangle is aligned with the breadth of the sheet and breadth of the rectangle is aligned with the length of the sheet and store the result in vertical. Print max(horizontal, vertical) as the result.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int maxRectangles( int L, int B, int l, int b)
{
int horizontal = 0, vertical = 0;
if (l <= L && b <= B)
{
int columns = B / b;
int rows = L / l;
horizontal = rows * columns;
}
if (l <= B && b <= L)
{
int columns = L / b;
int rows = B / l;
vertical = rows * columns;
}
return max(horizontal, vertical);
}
int main()
{
int L = 10, B = 7, l = 4, b = 3;
cout << (maxRectangles(L, B, l, b)) << endl;
}
|
Java
class GFG {
static int maxRectangles( int L, int B, int l, int b)
{
int horizontal = 0 , vertical = 0 ;
if (l <= L && b <= B) {
int columns = B / b;
int rows = L / l;
horizontal = rows * columns;
}
if (l <= B && b <= L) {
int columns = L / b;
int rows = B / l;
vertical = rows * columns;
}
return Math.max(horizontal, vertical);
}
public static void main(String[] args)
{
int L = 10 , B = 7 , l = 4 , b = 3 ;
System.out.print(maxRectangles(L, B, l, b));
}
}
|
Python3
def maxRectangles(L, B, l, b):
horizontal, vertical = 0 , 0
if l < = L and b < = B:
columns = B / / b
rows = L / / l
horizontal = rows * columns
if l < = B and b < = L:
columns = L / / b
rows = B / / l
vertical = rows * columns
return max (horizontal, vertical)
if __name__ = = "__main__" :
L, B, l, b = 10 , 7 , 4 , 3
print (maxRectangles(L, B, l, b))
|
C#
using System;
class GFG
{
static int maxRectangles( int L, int B,
int l, int b)
{
int horizontal = 0, vertical = 0;
if (l <= L && b <= B)
{
int columns = B / b;
int rows = L / l;
horizontal = rows * columns;
}
if (l <= B && b <= L)
{
int columns = L / b;
int rows = B / l;
vertical = rows * columns;
}
return Math.Max(horizontal, vertical);
}
public static void Main()
{
int L = 10, B = 7, l = 4, b = 3;
Console.WriteLine(maxRectangles(L, B, l, b));
}
}
|
PHP
<?php
function maxRectangles( $L , $B , $l , $b )
{
$horizontal = 0;
$vertical = 0;
if ( $l <= $L && $b <= $B )
{
$columns = (int)( $B / $b );
$rows = (int)( $L / $l );
$horizontal = $rows * $columns ;
}
if ( $l <= $B && $b <= $L )
{
$columns = (int)( $L / $b );
$rows = (int)( $B / $l );
$vertical = $rows * $columns ;
}
return max( $horizontal , $vertical );
}
$L = 10;
$B = 7;
$l = 4;
$b = 3;
print (maxRectangles( $L , $B , $l , $b ));
?>
|
Javascript
<script>
function maxRectangles(L, B, l, b)
{
var horizontal = 0, vertical = 0;
if (l <= L && b <= B)
{
var columns = parseInt(B / b);
var rows = parseInt(L / l);
horizontal = rows * columns;
}
if (l <= B && b <= L)
{
var columns = parseInt(L / b);
var rows = parseInt(B / l);
vertical = rows * columns;
}
return Math.max(horizontal, vertical);
}
var L = 10, B = 7, l = 4, b = 3;
document.write(maxRectangles(L, B, l, b));
</script>
|
Time Complexity: O(1), since there is only a basic arithmetic operation that takes constant time.
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...