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

We are given the dimensions of a sheet of paper, its Length L and Breadth B. Also, we are given the dimensions of a smaller rectangle, its length l and breadth b. The goal is to find the maximum number of smaller rectangles that can be cut out of the sheet of paper.

We will follow these steps −

  • First, try horizontal alignment: divide the sheet length L by rectangle length l, and sheet breadth B by rectangle breadth b, then multiply to get the count.
  • Then try vertical alignment (rotate the rectangle 90°): divide sheet length L by rectangle breadth b, and sheet breadth B by rectangle length l, then multiply to get the count.
  • Return the maximum count from both orientations.

Syntax

int maxRectangles(int L, int B, int l, int b);

Example

Let's understand with a complete example −

#include <stdio.h>

int maxRectangles(int L, int B, int l, int b) {
    int horizontal = 0, vertical = 0;
    
    // Horizontal orientation: rectangle as l×b
    if (l <= L && b <= B) {
        int cols = L / l;  // How many rectangles fit along length
        int rows = B / b;  // How many rectangles fit along breadth
        horizontal = cols * rows;
    }
    
    // Vertical orientation: rectangle rotated as b×l
    if (b <= L && l <= B) {
        int cols = L / b;  // How many fit along length (rotated)
        int rows = B / l;  // How many fit along breadth (rotated)
        vertical = cols * rows;
    }
    
    // Return maximum of both orientations
    return (horizontal > vertical) ? horizontal : vertical;
}

int main() {
    int sheetLength = 18, sheetBreadth = 6;
    int rectLength = 4, rectBreadth = 3;
    
    printf("Sheet dimensions: %d × %d<br>", sheetLength, sheetBreadth);
    printf("Rectangle dimensions: %d × %d<br>", rectLength, rectBreadth);
    
    int result = maxRectangles(sheetLength, sheetBreadth, rectLength, rectBreadth);
    printf("Maximum rectangles: %d<br>", result);
    
    return 0;
}
Sheet dimensions: 18 × 6
Rectangle dimensions: 4 × 3
Maximum rectangles: 8

How It Works

For the above example −

  • Horizontal orientation (4×3): 18÷4 = 4 columns, 6÷3 = 2 rows ? 4×2 = 8 rectangles
  • Vertical orientation (3×4): 18÷3 = 6 columns, 6÷4 = 1 row ? 6×1 = 6 rectangles
  • Maximum: 8 rectangles (horizontal orientation)
Sheet: 18 × 6 Horizontal (4×3): 8 rectangles Vertical (3×4): 6 rectangles

Key Points

  • Always check both orientations of the smaller rectangle to find the maximum fit.
  • Use integer division to find how many rectangles fit along each dimension.
  • The algorithm has O(1) time complexity as it only performs basic arithmetic operations.

Conclusion

This problem demonstrates optimal rectangle packing by trying both orientations. The key insight is that rotating the smaller rectangle might yield a better fit, so we always check both possibilities and return the maximum.

Updated on: 2026-03-15T13:04:21+05:30

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements