-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConstruct the Rectangle
More file actions
34 lines (27 loc) · 849 Bytes
/
Construct the Rectangle
File metadata and controls
34 lines (27 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.lang.Math;
class Solution {
public int[] constructRectangle(int area) {
int[] array = new int[2];
int width = 0;
int difference = 0;
int min = area;
for(int i = 1; i<=Math.sqrt(area); i++){
width = area/i;
if(width * i == area){
difference = Math.abs(i-width);
if(difference<min){
min = difference;
if(i>width){
array[0]=i;
array[1]=width;
}
else{
array[0]=width;
array[1]=i;
}
}
}
}
return array;
}
}