-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivisor.java
More file actions
43 lines (40 loc) · 1.01 KB
/
Copy pathDivisor.java
File metadata and controls
43 lines (40 loc) · 1.01 KB
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
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Divisor {
public static void main(String[] args) {
System.out.println(divisor(36));
System.out.println(divisor1(36));
}
/**
* Time O(n)
* Space Input O(1)
* Space Auxiliary O(n)
* @param n input
*/
private static Set<Integer> divisor(int n) {
Set<Integer> divisors = new HashSet<>();
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
divisors.add(i);
}
}
return divisors;
}
/**
* Time O(sqrt N)
* Space Input O(1)
* Space Auxiliary O(2*Sqrt N) -> O(sqrt N)
* @param n input
*/
private static Set<Integer> divisor1(int n) {
Set<Integer> divisors = new HashSet<>();
for (int i = 1; i <= (int) Math.sqrt(n); i++) {
if (n % i == 0) {
divisors.add(i);
divisors.add(n / i);
}
}
return divisors;
}
}