Simple Java Solution


  • 0
    U
    class Solution {
        public int[] constructRectangle(int area) {
            int sqrt =(int)Math.floor(Math.sqrt(area));
            while(area%sqrt!=0){
                sqrt--;
            }
            int[] ans = {area/sqrt,sqrt};
            return ans;
        }
    }
    

    The difference in length and breadth would be smallest for sqrt, but if the area is not a perfect square we try the nearest number smaller than the sqrt, that would divide the area.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.