RectangleArea Solution | CodeChef [Easy]

Problem

There are several sticks each 1 inch long. Your job is to form a rectangle with the biggest possible area, using these sticks as the perimeter.

CodeChef Problem Link

Input

Input will begin with an integer T, the number of test cases. Then there are T lines, each containing exactly one number N, the number of sticks.

Output

For every number N, output a single line containing the maximal area (in square inches) of a rectangle.

Constraints

  • 4 <= n <= 5000

Sample Input

3
14
10
111

Sample Output

12
6
756

Solution

Approach:
Since we’ve a maximum possible perimeter that is the number of sticks (N). Now, for a rectangle with a given perimeter to have the maximum area the length and breadth has to be closest and maximum possible.

Code Implementation

//
//  main.cpp
//  Rectangle Area
//
//  Created by Himanshu on 16/04/22.
//

#include <iostream>
using namespace std;

int main() {
    int T, num, a, b;
    cin>>T;
    
    while (T--) {
        cin>>num;
        
        a = num /4;
        b = (num - (2*a))/2;
        
        cout<<(a*b)<<endl;
    }
    
    return 0;
}

Time Complexity: O(1)

Leave a Reply