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

CServer Side ProgrammingProgramming

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

We will do following steps −

  • Firstly, we will take horizontal alignment, lengths L and l of sheet and rectangle respectively. Start aligning the L by l and B by b and count the rectangles.

  • Then also do the same in vertical alignments. Count again. Return the maximum value of count.

Let us understand with an example.

Input 

Sheet L=18, B=6 Rectangle l=4, b=3

Output 

Maximum rectangles: 8

Explanation 

Horizontal 18/4=4 6/3=2 2*4=8 rectangles possible
Vertical 18/3=6 6/4=1 6*1=6 rectangles possible
Maximum rectangles here is 8

Input 

Sheet L=10, B=6 Rectangle l=4, b=2

Output 

Maximum rectangles: 6

Explanation 

Horizontal 10/4=2 6/2=3 2*3=6 rectangles possible
Vertical 10/2=5 6/4=1 5*1=5 rectangles possible
Maximum rectangles here is 6

Approach used in the below program is as follows

  • The variables Length and Breadth are used to store dimensions of sheet.

  • The variables len and bre are used to store dimensions of rectangle.

  • Function maxRectangles (int L, int B, int l, int b) takes dimensions of sheet and rectangle and returns the maximum number of rectangles possible.

  • Variables numh and numv are used to store the number of rectangles that can be cut horizontally and vertically.

  • For horizontally, divide cols=L/l and rows=B/b, rectangles possible, numh=cols*rows.

  • For vertically, divide cols=L/b and rows=B/l, rectangles possible, numv=cols*rows.

  • Return maximum value as result obtained in above two steps numh or numv.

Example

 Live Demo

#include <stdio.h>
int maxRectangles (int L, int B, int l, int b){
   int numh = 0, numv = 0;
   // Cut rectangles horizontally if possible
   if (l <= L && b <= B){
      // One rectangle is a single cell
      int cols = B / b;
      int rows = L / l;
      // Total rectangles = total cells
      numh = rows * cols;
   }
   // Cut rectangles vertically if possible
   if (l <= B && b <= L){
      int cols = L / b;
      int rows = B / l;
      numv = rows * cols;
   }
   // Return the maximum possible rectangles
   return numh>numv?numh:numv;
}
// Driver code
int main (){
   int Length = 18;
   int Breadth =6;
   int len = 4, bre = 3;
   printf("Maximum rectangles: %d",maxRectangles(Length,Breadth,len,bre));
   return 0;
}

Output

If we run the above code it will generate the following output −

Maximum given sized rectangles that can be cut out of a sheet of paper: 8
raja
Published on 17-Aug-2020 12:23:07
Advertisements