forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosest Divisors.java
58 lines (58 loc) · 1.34 KB
/
Closest Divisors.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public int[] closestDivisors(int num) {
int ans[]=new int[2];
double a=Math.sqrt(num+1);
double b=Math.sqrt(num+2);
if(num==1){
ans[0]=1;
ans[1]=2;
return ans;
}
else if(a%1==0){
ans[0]=(int)a;
ans[1]=(int)b;
return ans;
}
else if(b%1==0){
ans[0]=(int)b;
ans[1]=(int)b;
return ans;
}
else{
int m=(int)Math.sqrt(num);
int diff1=Integer.MAX_VALUE;
int y=0,z=0,w=0,f=0;
for(int i=2;i<=m;i++){
if((num+1)%i==0){
y=i;
z=(num+1)/y;
int r=Math.abs(y-z);
if(r<diff1){
diff1=r;
}
}
}
int diff2=Integer.MAX_VALUE;
for(int i=2;i<=m;i++){
if((num+2)%i==0){
w=i;
f=(num+2)/w;
int r=Math.abs(w-f);
if(r<diff2){
diff2=r;
}
}
}
if(diff1<diff2){
ans[0]=y;
ans[1]=z;
return ans;
}
else{
ans[0]=w;
ans[1]=f;
return ans;
}
}
}
}```