I'm currently working on a leetcode problem to construct a rectangle. An int value will be passed into a method, and from there I need two values for the length and width, where the length and width must be as small as possible and the L >= W. I looked at a couple solutions but cannot grasp the concept. On line 4, wouldn't the result always yield the area? So how does the for loop continue ? For instance if the area is 26, and we do the calculations on line 4, Wouldn't we always get the same number on line 4? However when I run the code, it gives the correct results of [13,2].
1 class Solution {
2 public int[] constructRectangle(int area) {
3 for(int w=(int)Math.sqrt(area);w>0;w--){
4 if((area/w)*w==area)return new int[]{area/w,w};
}return new int[]{};
}
}