Root Root!

  • 0

    Solution needs the difference between the length and the width to be minimum,
    implies preferably they should be equal.
    So ideally, perfect squares would give perfect answers.
    Meaning, Take the square root of the area now that's your mid way value. From then on, justs keep reducing the root until you hit a factor of area.
    That's the biggest factor of area between 1 and halfway point, ie. the square root.
    The rest is simple, since from halfway we are reducing the number, the corresponding value would be bigger.
    ie., Root is width and Area/root is length.

            int root = (int)(Math.sqrt(area));
            while(area%root!=0) root--;
            int[] returnVal = new int[2];
            returnVal[0] = area/root;
            returnVal[1] = root;
            return returnVal;

Log in to reply

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