Codility Min Perimeter Rectangle

Posted on January 2, 2014

Last updated on January 2, 2014

Solution to Codility Min-Perimeter-Rectangle problem.

Given a rectangle area \(A\), find the smallest possible perimeter for a rectangle with integer sidelengths and area \(A\).

We just iterate through all the possible divisors, and update the mininum perimeter.

public int solution(int N) {
  int minPer = Integer.MAX_VALUE;

  for(int i = 1; i*i <= N; i++){
    if(N % i == 0){
      minPer = Math.min(minPer, 2*(N/i + i));
    }
  }

  return minPer;
}
Codility Min-Perimeter Rectangle
Markdown SHA1: 1d2bd7cbb57678bc8c8dd2ac7f38807a3e6f3170