Last Updated: 17 Apr, 2021

Ninja’s Apartment

Easy
Asked in company
Goldman Sachs

Problem statement

Ninja is planning to build a new apartment but he wants an apartment in the shape of a rectangle whose length is greater than the breadth of the rectangle. Also, the difference between the length and breadth should be minimum for the area of the apartment.

So your task is to print the length and breadth of the rectangle. You are provided with the ‘AREA’ of the region in which the apartment is to be built.

Example:

Suppose given ‘AREA’ is ‘6’. So, possible combinations are [ [ 1, 6 ], [ 2, 3 ], [ 3, 2 ], [ 6, 1 ] ]. We can’t choose [ 1, 6 ], [ 2, 3 ] as in this case breadth is greater than length.
So, we choose [ 3, 2 ] over [ 6, 1 ] as the difference between | 3 - 2 | < | 6 - 1 | so we return | 3, 2 | as the answer.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.

Input Format:

The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains an integer ‘AREA’ denoting the area of the rectangle.

Output Format:

For each test case, print the length and breadth of the given area.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints:

1 <= 'T' <= 10^2
2 <= ‘AREA’ <= 10 ^ 6

Where ‘T’ represents the number of test cases and ‘AREA’ represents the given area of the apartment.

Time Limit: 1sec

Approaches

01 Approach

Approach: The idea here is to use a brute approach. Starting from ‘1’ up to ‘AREA’, we run a loop and compare the differences of length and breadth and one with the minimum is our answer.

 

Algorithm is as follows:

  1. First, we declare an array/list named ‘ANS’.
  2. Now we run a loop starting ‘i’ from ‘1’ up to ‘AREA’’ and for every iteration we check:
    • If the ‘AREA’ is multiple of ‘i’:
      • If ‘AREA % i == 0’ then ‘i’ becomes our length. Now we find the breadth by dividing the is by ‘i’.
      • Now we check if the breadth is smaller than the length.
        • We compare it with ‘MIN’. If ‘MIN’ is greater than their difference we update our length and breadth and push it into the ‘ANS’.
      • Else we continue the loop.
    • Else we continue the loop.
  3. In the end, we return ‘ANS’.

02 Approach

Approach: The idea here is that if we want the minimum difference between length and breadth, then the breadth should be equal to or less than the square root of the given area.

If ‘LENGTH' * ‘BREATH’ = ‘AREA’, and ‘LENGTH’ >= 'BREADTH’, then ‘BREADTH’ can be at max sqrt of ‘AREA’

So we can say that the minimum difference between ‘LENGTH’ and ‘BREADTH’  is when ‘LENGTH' = ‘BREADTH’ and ‘AREA’ = ‘LENGTH’ * 'LENGTH’.

If ‘LENGTH' * ‘BREADTH' < 'AREA’, then increase ‘LENGTH’, if ‘LENGTH’ * ‘BREADTH’ > 'AREA’ decreases ‘BREADTH’.

 

Algorithm is as follows:

  1. First, we declare the array/list named as ‘ANS’.
  2. Now we initialize our breadth as the square root of ‘AREA’.
  3. Now we start iterating until breadth is the multiple of a given area:
    • If it is multiple the area we find the length by ‘AREA’ divided by the breadth and push it into ‘ANS’ and return the ‘ANS’.
    • Else we reduce the value of breadth by ‘1’ and continue the loop.