Open In App

Maximum given sized rectangles that can be cut out of a sheet of paper

Last Updated : 23 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:
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:
 

 

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++




// CPP implementation of the approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to return the maximum rectangles possible
int maxRectangles(int L, int B, int l, int b)
{
    int horizontal = 0, vertical = 0;
 
    // Cut rectangles horizontally if possible
    if (l <= L && b <= B)
    {
 
        // One rectangle is a single cell
        int columns = B / b;
        int rows = L / l;
 
        // Total rectangles = total cells
        horizontal = rows * columns;
    }
 
    // Cut rectangles vertically if possible
    if (l <= B && b <= L)
    {
        int columns = L / b;
        int rows = B / l;
 
        vertical = rows * columns;
    }
 
    // Return the maximum possible rectangles
    return max(horizontal, vertical);
}
 
// Driver code
int main()
{
    int L = 10, B = 7, l = 4, b = 3;
    cout << (maxRectangles(L, B, l, b)) << endl;
}
 
// This code is contributed by
// Sanjit_Prasad


Java




// Java implementation of the approach
class GFG {
 
    // Function to return the maximum rectangles possible
    static int maxRectangles(int L, int B, int l, int b)
    {
        int horizontal = 0, vertical = 0;
 
        // Cut rectangles horizontally if possible
        if (l <= L && b <= B) {
 
            // One rectangle is a single cell
            int columns = B / b;
            int rows = L / l;
 
            // Total rectangles = total cells
            horizontal = rows * columns;
        }
 
        // Cut rectangles vertically if possible
        if (l <= B && b <= L) {
            int columns = L / b;
            int rows = B / l;
 
            vertical = rows * columns;
        }
 
        // Return the maximum possible rectangles
        return Math.max(horizontal, vertical);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int L = 10, B = 7, l = 4, b = 3;
        System.out.print(maxRectangles(L, B, l, b));
    }
}


Python3




# Python3 implementation of the approach
 
# Function to return the maximum
# rectangles possible
def maxRectangles(L, B, l, b):
 
    horizontal, vertical = 0, 0
 
    # Cut rectangles horizontally if possible
    if l <= L and b <= B:
 
        # One rectangle is a single cell
        columns = B // b
        rows = L // l
 
        # Total rectangles = total cells
        horizontal = rows * columns
 
    # Cut rectangles vertically if possible
    if l <= B and b <= L:
     
        columns = L // b
        rows = B // l
 
        vertical = rows * columns
 
    # Return the maximum possible rectangles
    return max(horizontal, vertical)
 
# Driver code
if __name__ == "__main__":
 
    L, B, l, b = 10, 7, 4, 3
    print(maxRectangles(L, B, l, b))
 
# This code is contributed by Rituraj Jain


C#




// C# implementation of the above approach
using System;
 
class GFG
{
 
    // Function to return the
    // maximum rectangles possible
    static int maxRectangles(int L, int B,
                                int l, int b)
    {
        int horizontal = 0, vertical = 0;
 
        // Cut rectangles horizontally if possible
        if (l <= L && b <= B)
        {
 
            // One rectangle is a single cell
            int columns = B / b;
            int rows = L / l;
 
            // Total rectangles = total cells
            horizontal = rows * columns;
        }
 
        // Cut rectangles vertically if possible
        if (l <= B && b <= L)
        {
            int columns = L / b;
            int rows = B / l;
            vertical = rows * columns;
        }
 
        // Return the maximum possible rectangles
        return Math.Max(horizontal, vertical);
    }
 
    // Driver code
    public static void Main()
    {
        int L = 10, B = 7, l = 4, b = 3;
        Console.WriteLine(maxRectangles(L, B, l, b));
    }
}
 
// This code is contributed by Ryuga


PHP




<?php
// PHP implementation of the approach
 
// Function to return the maximum
// rectangles possible
function maxRectangles($L, $B, $l, $b)
{
    $horizontal = 0;
    $vertical = 0;
 
    // Cut rectangles horizontally if possible
    if ($l <= $L && $b <= $B)
    {
 
        // One rectangle is a single cell
        $columns = (int)($B / $b);
        $rows = (int)($L / $l);
 
        // Total rectangles = total cells
        $horizontal = $rows * $columns;
    }
 
    // Cut rectangles vertically if possible
    if ($l <= $B && $b <= $L)
    {
        $columns = (int)($L / $b);
        $rows = (int)($B / $l);
 
        $vertical = $rows * $columns;
    }
 
    // Return the maximum possible rectangles
    return max($horizontal, $vertical);
}
 
// Driver code
$L = 10;
$B = 7;
$l = 4;
$b = 3;
print(maxRectangles($L, $B, $l, $b));
 
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the maximum
// rectangles possible
function maxRectangles(L, B, l, b)
{
    var horizontal = 0, vertical = 0;
 
    // Cut rectangles horizontally
    // if possible
    if (l <= L && b <= B)
    {
         
        // One rectangle is a single cell
        var columns = parseInt(B / b);
        var rows = parseInt(L / l);
 
        // Total rectangles = total cells
        horizontal = rows * columns;
    }
 
    // Cut rectangles vertically if possible
    if (l <= B && b <= L)
    {
        var columns = parseInt(L / b);
        var rows = parseInt(B / l);
 
        vertical = rows * columns;
    }
 
    // Return the maximum possible rectangles
    return Math.max(horizontal, vertical);
}
 
// Driver Code
var L = 10, B = 7, l = 4, b = 3;
 
document.write(maxRectangles(L, B, l, b));
 
// This code is contributed by kirti
 
</script>


Output: 

4

 

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.



Similar Reads

Construct N sized Array such that every K sized Subarray has MEX value D
Given three integers N, K and D, the task is to construct an array of size N such that every subarray of size K has a MEX value of D. If there is no possible answer, output -1. MEX of an array is the first non-negative integer that is not present in the array. Examples: Input: N = 4, K = 3, D = 4Output: -1Explanation: As D exceeds K, it is impossib
5 min read
Cut all the rods with some length such that the sum of cut-off length is maximized
Given N rods of different lengths. The task is to cut all the rods with some maximum integer height 'h' such that the sum of cut-off lengths of the rod is maximized and must be greater than M. Print -1 if no such cut is possible. Note: A rod cannot be cut also. Examples: Input: N = 7, M = 8, a[] = {1, 2, 3, 5, 4, 7, 6} Output: 3 Rod 1 and 2 are unt
9 min read
Paper Cut into Minimum Number of Squares | Set 2
Given a paper of size A x B. Task is to cut the paper into squares of any size. Find the minimum number of squares that can be cut from the paper.  Examples:  Input : 36 x 30Output : 5Explanation : 3 (squares of size 12x12) + 2 (squares of size 18x18)Input : 4 x 5Output : 5Explanation : 1 (squares of size 4x4) + 4 (squares of size 1x1)Asked in : Go
10 min read
Paper Cut into Minimum Number of Squares
Given a paper of size A x B, the task is to cut the entire paper into squares of any size. Find the minimum number of squares that can be cut from the paper. Examples:  Input: A = 5, B = 8 Output: 5Explanation: We can cut the paper into 5 squares: 1 square of size 5 X 5, 1 square of size 3 X 3, 1 square of size 2 X 2 and 2 squares of size 1 X 1. In
15 min read
Find the number of corner rectangles that can be formed from given Matrix
Given a binary matrix mat[][] of dimensions N*M, the task is to find the number of corner rectangles that can be formed. A corner rectangle is defined as the submatrix having 1s on the corners of it and each 1s must belong to a unique cell in that submatrix. Examples: Input: mat[][] = {{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}Output: 1Explanation:There exis
7 min read
Find number of rectangles that can be formed from a given set of coordinates
Given an array arr[][] consisting of pair of integers denoting coordinates. The task is to count the total number of rectangles that can be formed using given coordinates. Examples: Input: arr[][] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1}, {11, 11}}Output: 3Explanation: Following are the rectangles formed using given coordinates. First Rect
6 min read
Maximum count of distinct sized subarrays with given sum
Given a binary array arr[] of N integers, the task is to find the maximum count of distinct sized subarrays such that the sum of each subarray is K. Example: Input: arr[] = {0, 1, 1 , 0}, K = 2Output: 3Explanation: The subset {{0, 1, 1, 0}, {0, 1, 1}, {1, 1}} is the subset of 3 subarrays such that the sum of each subarray is 2 and the size of each
7 min read
Maximum multiple of D from K-sized subset sums from given Array
Given an integer array A[] of size N and two integers K, D. A list or superset of all possible subset sums of size K is made from the given array, the task is to find the largest multiple of D from this list or superset. If there exists no multiple, return -1 as the answer. Examples: Input: N = 4, K = 2, D = 2, A[] = [1, 2, 3, 4]Output: 6Explanatio
15+ min read
Count of maximum distinct Rectangles possible with given Perimeter
Given an integer N denoting the perimeter of a rectangle. The task is to find the number of distinct rectangles possible with a given perimeter. Examples Input: N = 10Output: 4Explanation: All the rectangles with perimeter 10 are following in the form of (length, breadth):(1, 4), (4, 1), (2, 3), (3, 2) Input: N = 8Output: 3 Approach: This problem c
3 min read
Maximum of sum of length of rectangles and squares formed by given sticks
Given an array arr[] consisting of N integers, representing the length of the sticks, the task is to find the maximum sum possible of all lengths of the squares and rectangles constructed using these sticks. Note A single side can be represented using only a single stick. Examples: Input: arr[] = {5, 3, 2, 3, 6, 3, 3} Output: 12 Sum of length of Sq
12 min read
Check if L sized Subarray of first N numbers can have sum S with one deletion allowed
Consider an integer sequence A = {1, 2, 3, ...., N} i.e. the first N natural numbers in order and two integers, L and S. Check whether there exists a subarray of length L and sum S after removing at most one element from A. Examples: Input: N = 5, L = 3, S = 11Output: YESExplanation: We can remove 3 from A to obtain A = {1, 2, 4, 5} where {2, 4, 5}
6 min read
Maximise number of cuts in a rod if it can be cut only in given 3 sizes
Given a rod of length N meters, and the rod can be cut in only 3 sizes A , B and C . The task is to maximizes the number of cuts in rod. If it is impossible to make cut then print -1 . Examples: Input: N = 17, A = 10, B = 11, C = 3 Output: 3 Explanation: The maximum cut can be obtain after making 2 cut of length 3 and one cut of length 11. Input: N
7 min read
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 that can be placed inside a rectangle of size n*m. Note: No two small rectangles overlap.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 A
6 min read
Check if N rectangles of equal area can be formed from (4 * N) integers
Given an integer N and an array arr[] of size 4 * N, the task is to check whether N rectangles of equal area can be formed from this array if each element can be used only once. Examples: Input: arr[] = {1, 8, 2, 1, 2, 4, 4, 8}, N = 2 Output: Yes Two rectangles with sides (1, 8, 1, 8) and (2, 4, 2, 4) can be formed. Both of these rectangles have th
6 min read
Count of smaller rectangles that can be placed inside a bigger rectangle
Given four integers L, B, l, and b, where L and B denote the dimensions of a bigger rectangle and l and b denotes the dimension of a smaller rectangle, the task is to count the number of smaller rectangles that can be drawn inside a bigger rectangle. Note: Smaller rectangles can overlap partially. Examples: Input: L = 5, B = 3, l = 4, b = 1Output:
5 min read
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
Given a 2D array rectangle[][] representing vertices of a rectangle {(0, 0), (L, 0), (0, B), (L, B)} of dimensions L * B, and another 2D-array, points[][] of size N in a Cartesian coordinate system. Draw a horizontal line parallel to the X-axis and a vertical line parallel to the Y-axis from each point of the given array. The task is to count all p
6 min read
Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
Given N rows and M columns of a matrix, the task is to fill the matrix cells using first K integers such that: Each group of cells is denoted by a single integer.The difference between the group containing the maximum and the minimum number of cells should be minimum.All the cells of the same group should be contiguous i.e., for any group, two adja
11 min read
Maximum sum of even indexed elements obtained by right shift on an even sized subarray
Given an array arr[], we need to find the maximum sum of the even indexed elements that can be obtained by performing right shift operation on any sub-array of even length by 1. Examples: Input: arr[] = {5, 1, 3, 4, 5, 6} Output: 15 Explanation: We can perform a right shift on index 2 to 5 then resulting array is: arr[] = {5, 1, 6, 3, 4, 5} Sum of
14 min read
Maximize the sum of maximum elements of at least K-sized subarrays
Given an integer array arr[] of length N and an integer K, partition the array in some non-overlapping subarrays such that each subarray has size at least K and each element of the array should be part of a subarray. The task is to maximize the sum of maximum elements across all the subarrays. Examples: Input: N = 4 , K = 1, arr[] = {1, 2, 3, 4}Out
7 min read
Maximum number of overlapping rectangles with at least one common point
Given four arrays x1[], x2[], y1[], y2[] of size N where (x1[i], y1[i]) denotes the left-bottom corner and (x2[i], y2[i]) right-top corner of a rectangle, the task is to find the maximum number of overlapping rectangles with at least one common point. Examples: Input: N = 2 x1[] = {0, 50} y1[] = {0, 50} x2[] = {100, 60} y2[] = {100, 60}Output: 2Exp
8 min read
Maximize the difference of sum of elements at even indices and odd indices by shifting an odd sized subarray to end of given Array.
Given an array arr[] of size N, the task is to maximize the difference of the sum of elements at even indices and elements at odd indices by shifting any subarray of odd length to the end of the array. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}Output: 3Explanation: Initially sum of elements at even indices = 1 + 3 + 5 = 9Sum of elements at odd ind
13 min read
Check if N sized String with given number of 00, 01, 10, 11 Substrings exists
Given an integer N and four values a, b, c, d (where a ≥ 0, b ≥ 0, c ≥ 0, d ≥ 0), the task is to check if it is possible to create a binary string of length N such that: There are exactly a substrings of "00" There are exactly b substrings of "01"There are exactly c substrings of "10"There are exactly d substrings of "11" Examples: Input: N = 11, a
11 min read
Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
Given coordinates of 4 points, bottom-left and top-right corners of two rectangles. The task is to find the coordinates of the intersecting rectangle formed by the given two rectangles. Examples: Input: rec1: bottom-left(0, 0), top-right(10, 8), rec2: bottom-left(2, 3), top-right(7, 9) Output: (2, 3) (7, 8) (2, 8) (7, 3) Input: rec1: bottom-left(0,
10 min read
Smallest square formed with given rectangles
Given a rectangle of length l and breadth b, we need to find the area of the smallest square which can be formed with the rectangles of these given dimensions. Examples: Input : 1 2 Output : 4 We can form a 2 x 2 square using two rectangles of size 1 x 2. Input : 7 10 Output :4900 Let's say we want to make a square of side length a from rectangles
6 min read
Count of Rectangles with area K made up of only 1s from given Binary Arrays
Given two binary arrays A[] and B[], of length N and M respectively, the task is to find the number of rectangles of area K consisting of 1's in the matrix C[][] generated by multiplying the two arrays such that, C[i][j] = A[i] * B[j] (1< i < n, 1< j < m). Examples: Input: N= 3, M = 3, A[] = {1, 1, 0}, B[] = {0, 1, 1}, K = 2 Output: 4 E
11 min read
Number of rectangles with given area in an N*M grid
Given three positive integers N, M, and A, the task is to count the number of rectangles with area equal to A present in an M * N grid. Examples: Input: N = 2, M = 2, A = 2 Output: 4 Explanation: In the given grid of size 2 × 2, 2 rectangles of dimension 2 × 1 and 2 rectangles of dimension 1 × 2 can be inscribed. Therefore, the required output is 4
10 min read
Size of smallest square that contains N non-overlapping rectangles of given dimensions
Given two positive integers W and H and N rectangles of dimension W*H, the task is to find the smallest size of the square required such that all the N rectangles can be packed without overlapping. Examples: Input: N = 10, W = 2, H = 3Output: 9Explanation:The smallest size of the square is 9 units to pack the given 10 rectangles of size 2*3 as illu
7 min read
Maximize the length of upper boundary formed by placing given N rectangles horizontally or vertically
Given a vector of pairs, V[] denoting the width and height of N rectangles numbered from 1 to N, these rectangles are placed in contact with the horizontal axis and are adjacent from left to right in numerical order. The task is to find the maximum length of the upper boundary formed by placing each of the rectangles either horizontally or vertical
9 min read
Find maximum height to cut all chocolates horizontally such that at least K amount remains
Given an array arr[] consisting of heights of N chocolate bars, the task is to find the maximum height at which the horizontal cut is made to all the chocolates such that the sum remaining amount of chocolate is at least K. Examples: Input: K = 7, arr[] = [15, 20, 8, 17]Output: 15Explanation:Suppose a cut is made at height 8:-> chocolate taken f
10 min read
Find out the maximum value of the expression according to the given constraint
Given an array A[] of length N. Then your task is to output the maximum value of (Sum/LCM). Where Sum is the addition of all the elements of A[] and LCM is a number between [1, ∞] that can't be achieved by any possible non-empty set of A[]. Examples: Input: N = 5, A[] = {1, 2, 3, 4, 5} Output: 2 Explanation: The Sum of A[] is = 1+2+3+4+5 = 15All th
9 min read
three90RightbarBannerImg