Tasks Details
easy
Find the minimal perimeter of any rectangle whose area equals N.
Task Score
100%
Correctness
100%
Performance
100%
An integer N is given, representing the area of some rectangle.
The area of a rectangle whose sides are of length A and B is A * B, and the perimeter is 2 * (A + B).
The goal is to find the minimal perimeter of any rectangle whose area equals N. The sides of this rectangle should be only integers.
For example, given integer N = 30, rectangles of area 30 are:
- (1, 30), with a perimeter of 62,
- (2, 15), with a perimeter of 34,
- (3, 10), with a perimeter of 26,
- (5, 6), with a perimeter of 22.
Write a function:
class Solution { public int solution(int N); }
that, given an integer N, returns the minimal perimeter of any rectangle whose area is exactly equal to N.
For example, given an integer N = 30, the function should return 22, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..1,000,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 13 minutes
Notes
not defined yet
Task timeline
Code: 01:15:49 UTC,
java,
autosave
Code: 01:18:14 UTC,
java,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 8 expected 22
Code: 01:25:43 UTC,
java,
autosave
Code: 01:25:57 UTC,
java,
autosave
Code: 01:26:27 UTC,
java,
autosave
Code: 01:26:58 UTC,
java,
autosave
Code: 01:27:12 UTC,
java,
autosave
Code: 01:27:29 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int N) {
int cnt = 0;
int min = Integer.MAX_VALUE;
int sq = (int)Math.sqrt(N);
for(int i=1; i<=sq; i++){
if(N%i == 0){
if((i + (N/i)) * 2 < min){
min =
}
}
}
cnt*=2;
if(sq * sq == N){
cnt--;
}
return cnt;
}
}
Code: 01:27:40 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int N) {
int cnt = 0;
int min = Integer.MAX_VALUE;
int sq = (int)Math.sqrt(N);
for(int i=1; i<=sq; i++){
if(N%i == 0){
if((i + (N/i)) * 2 < min){
min =(i + (N/i)) * 2;
}
}
}
cnt*=2;
if(sq * sq == N){
cnt--;
}
return cnt;
}
}
Code: 01:27:58 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int N) {
int cnt = 0;
int min = Integer.MAX_VALUE;
int sq = (int)Math.sqrt(N);
for(int i=1; i<=sq; i++){
if(N%i == 0){
if((i + (N/i)) * 2 < min){
min =(i + (N/i)) * 2;
}
}
}
cnt*=2;
if(sq * sq == N){
cnt--;
}
return cnt;
}
}
Code: 01:28:10 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int N) {
int cnt = 0;
int min = Integer.MAX_VALUE;
int sq = (int)Math.sqrt(N);
for(int i=1; i<=sq; i++){
if(N%i == 0){
if((i + (N/i)) * 2 < min){
min =(i + (N/i)) * 2;
}
}
}
cnt*=2;
if(sq * sq == N){
cnt--;
}
return cnt;
}
}
Code: 01:28:24 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int N) {
int cnt = 0;
int min = Integer.MAX_VALUE;
int sq = (int)Math.sqrt(N);
for(int i=1; i<=sq; i++){
if(N%i == 0){
if((i + (N/i)) * 2 < min){
min =(i + (N/i)) * 2;
}
}
}
cnt*=2;
if(sq * sq == N){
cnt--;
}
return cnt;
}
}
Code: 01:28:36 UTC,
java,
verify,
result: Passed
Analysis
Code: 01:28:44 UTC,
java,
verify,
result: Passed
Analysis
Code: 01:28:47 UTC,
java,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))