A solution attempt: (EDITED, see below)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
// Solution for Lesson 9, "CountNonDivisible"
// of http://codility.com/demo/train/
public class Solution
{
public static void main(String[] args)
{
int A[] = new int[5];
A[0] = 3;
A[1] = 1;
A[2] = 2;
A[3] = 3;
A[4] = 6;
Solution s = new Solution();
int B[] = s.solution(A);
System.out.println("Input : "+Arrays.toString(A));
System.out.println("Result : "+Arrays.toString(B));
}
public int[] solution(int[] A)
{
Set<Integer> setA = asSet(A);
List<Set<Integer>> divisors = computeDivisors(A.length * 2);
int occurrences[] = computeOccurrences(A);
int nonDivisors[] = new int[A.length];
for (int i=0; i<A.length; i++)
{
int value = A[i];
Set<Integer> d = divisors.get(value);
int totalOccurances = 0;
for (Integer divisor : d)
{
if (setA.contains(divisor))
{
totalOccurances += occurrences[divisor];
}
}
nonDivisors[i] = A.length-totalOccurances;
}
return nonDivisors;
}
/**
* Returns a set containing all elements of the given array
*
* Space: O(N)
* Time: O(N)
*
* @param A The input array
* @return The set
*/
private static Set<Integer> asSet(int A[])
{
Set<Integer> result = new HashSet<Integer>();
for (int value : A)
{
result.add(value);
}
return result;
}
/**
* Computes a list that contains for each i in [0...maxValue+1] a set
* with all divisors of i. This is basically an "Eratosthenes Sieve".
* But in addition to setting the entries of a list to 'false'
* (indicating that the respective numbers are non-prime), this
* methods inserts the divisors into the corresponding set.
*
* Space: O(N) (?)
* Time: O(N*logN) (?)
*
* @param maxValue The maximum value
* @return The list
*/
private static List<Set<Integer>> computeDivisors(int maxValue)
{
List<Boolean> prime = new ArrayList<Boolean>();
prime.addAll(Collections.nCopies(maxValue+1, Boolean.TRUE));
List<Set<Integer>> divisors = new ArrayList<Set<Integer>>();
for (int i = 0; i < maxValue + 1; i++)
{
Set<Integer> d = new HashSet<Integer>();
d.add(1);
d.add(i);
divisors.add(d);
}
for (int i = 2; i <= maxValue; i++)
{
int next = i + i;
while (next <= maxValue)
{
divisors.get(next).addAll(divisors.get(i));
prime.set(next, Boolean.FALSE);
next += i;
}
}
return divisors;
}
/**
* Computes an array of length 2*A.length+1, where each entry i contains
* the number of occurrences of value i in array A
*
* Space: O(N)
* Time: O(N)
*
* @param A The input array
* @return The occurrences array
*/
private static int[] computeOccurrences(int A[])
{
int occurances[] = new int[A.length * 2 + 1];
for (int i=0; i<A.length; i++)
{
int value = A[i];
occurances[value]++;
}
return occurances;
}
}
The maximum value for the numbers that occur in the array was defined to be 2*arrayLength. For each number that MAY occur in the array, it computes
- The set of divisors of this number (using the Erathostenes Sieve)
- How often the number actually occurs in the array
Given this information, one can walk through the array. For each value that is found in the array, one can look up the set of divisors, and compute the total number occurences of all the divisors. The result is then simply the array length, minus this total number of occurances of divisors.
Since it uses only the Sieve of Erathostenes for the computation (and only walks through the set of divisors for each number, which should be logN as well), it should have a worst-case time complexity of O(N*logN). But I'm not entirely sure whether the storage complexity really can be considered to be strictly O(N), because for each of the N numbers, it has to store the set of divisors. Maybe this can somehow be avoided, by combining some of the methods, but in any case, the storage is at least in O(N*logN) as well.
EDIT: The exceptions came from the array for the occurrences storing only values up to A.length*2-1, this was fixed now. Additionally, the set of divisors was not computed properly, this should also be fixed now.
Apart from that, analysis results like
got [8, 8, 9, 10, 6, 8, ..
expected [8, 8, 9, 10, 6, 8, ..
are not really helpful. Maybe this is part of the "game", but I'm not playing this game right now. The basic idea should be clear, and I assume that it's now working properly until someone shows be a counterexample ;-P
It still does not reach the O(N) storage complexity, but I haven't thought about a possible way to achieve this thoroughly...