Java Count Non Divisible
import java.util.Arrays;
public class Solution {
public static int[] countNonDivisible(int[] a) {
int size = a.length;
int[] nondivisor = new int[size];
int maxValue = Arrays.stream(a).max().orElse(0);
int[] occurrences = new int[maxValue + 1];
for (int v : a) {
occurrences[v]++;
}
for (int k = 0; k < size; k++) {
int v = a[k];
int count = 0;
long i = 1;
while (i * i <= v) {
if (v % i == 0) {
count += occurrences[(int) i];
if (v / i != i) {
count += occurrences[(int) (v / i)];
}
}
i++;
}
nondivisor[k] = size - count;
}
return nondivisor;
}
}
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.