Sagar And Pens
You are given N pens of length 1 inch each.Your task is to find the maximum area of the rectangle that can be formed using these N pens in inches.
The number of pens given in each testcase must be considered as the perimeter of the rectangle which is goig to remain constant.
The main logic is quiet simple.Although we need to maximize the area , all the possible rectangles that can be formed using the N number of pens given will have
different lengths and widths , but the perimeter is going to remain the same and is equal to the number of pens as the length of each pen is 1 inch.
As it,s a rectangle , we consider ‘l’ to be the smaller side and ‘w’ to be the larger side.
Now , the smaller side or the length of the rectangle will be one-fourth of the total perimeter i.e number of pens.
Therefore , l = N/4.
For the larger side or the width , the length which remains after dividing it into 4 parts will be added.As there are 2 sides which will have the same width ,
we will have to divide this remaining portion of the perimeter into half
Therefore , w = l + (N % 4)/2.
Now we have the maximum length and width whose product will give you the maximized area of rectangle possible.
AUTHOR’S AND TESTER’S SOLUTIONS:
Solution can be found here.