{"id":432,"date":"2020-04-01T06:22:00","date_gmt":"2020-04-01T06:22:00","guid":{"rendered":"https:\/\/danwritescode.com\/?p=432"},"modified":"2020-06-26T06:46:45","modified_gmt":"2020-06-26T06:46:45","slug":"min-perimeter-rectangle-codility-100-correct-javascript-solution","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/min-perimeter-rectangle-codility-100-correct-javascript-solution\/","title":{"rendered":"Min Perimeter Rectangle – Codility 100% Correct Javascript Solution"},"content":{"rendered":"\n
The Min Perimeter Rectangle challenge brings us back to a lot of math. I had a gut feeling on this one and I only checked with a few examples but the minimum perimeter of a rectangle for a given area happens when the rectangle is in fact a square.<\/p>\n\n\n\n
For those of you who want a to be 100% sure of this, here is a proper demonstration<\/a>.<\/p>\n\n\n\n So all we need to do in this case is to find the closest integer number to the square root which is a dividing factor for the given area.<\/p>\n\n\n\nSolution in Javascript with O(sqrt(N)) complexity<\/h2>\n\n\n\n
function solution(N) {\n \/\/ write your code in JavaScript (Node.js 8.9.4)\n for (let i = Math.floor(Math.sqrt(N)); i > 0; i--) {\n if (N % i === 0) {\n return (i + N \/ i) * 2;\n }\n }\n}<\/code><\/pre>\n\n\n\n