-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourDivisors.java
128 lines (102 loc) · 3 KB
/
FourDivisors.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package math.medium;
import java.util.ArrayList;
/***
* Problem 1390 in Leetcode: https://leetcode.com/problems/four-divisors/
*
* Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
* If there is no such integer in the array, return 0.
*
* Example 1:
* Input: nums = [21,4,7]
* Output: 32
*
* Example 2:
* Input: nums = [21,21]
* Output: 64
*
* Example 3:
* Input: nums = [7286,18704,70773,8224,91675]
* Output: 10932
*/
public class FourDivisors {
public static void main(String[] args) {
int[] nums = {7286, 18704, 70773, 8224, 91675};
System.out.println("Brute Force: " + getFourDivisorsSumBruteForce(nums));
System.out.println("Sieve: " + getFourDivisorsSumSieve(nums));
System.out.println("Only two divisors between 1 and num: " + getFourDivisorsSumOptimized(nums));
}
private static int getFourDivisorsSumBruteForce(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += sumOfFourDivisorsOf(num);
}
return sum;
}
private static int sumOfFourDivisorsOf(int num) {
int sum = 0, count = 0;
for (int i = 1; i * i <= num; i++) {
if ((num % i) == 0) {
count++;
sum += i;
if (i != (num / i)) {
count++;
sum += (num / i);
}
}
}
if (count == 4) {
return sum;
}
return 0;
}
private static int getFourDivisorsSumSieve(int[] nums) {
int n = (int) 1e5 + 2;
ArrayList<Integer>[] divisors = new ArrayList[n];
divisors[1] = new ArrayList<>();
divisors[1].add(1);
for (int i = 2; i < n; i++) {
divisors[i] = new ArrayList<>();
divisors[i].add(1);
divisors[i].add(i);
}
for (int i = 2; i < n; i++) {
for (int j = i + i; j < n; j += i) {
divisors[j].add(i);
}
}
int sum = 0;
for (int num : nums) {
int count = divisors[num].size();
if (count == 4) {
for (int element : divisors[num]) {
sum += element;
}
}
}
return sum;
}
private static int getFourDivisorsSumOptimized(int[] nums) {
int totalSum = 0;
for (int num : nums) {
int sqrt = (int) Math.sqrt(num);
if ((sqrt * sqrt) == num) {
continue;
}
int count = 0;
int sum = 1 + num;
for (int i = 2; i <= sqrt; i++) {
if ((num % i) == 0) {
count++;
sum += (i + (num / i));
}
if (count >= 2) {
break;
}
}
if (count == 1) {
totalSum += sum;
}
}
return totalSum;
}
}