title: CountNonDivisible - Codility training task
body: <p>I'm traning on codility now. Some tasks I can solve by myself, but with some tasks have problems.
  Difficulty of this task is &lt;**>. It's medium, but I stalled.</p>
  
  <p>Problem:</p>
  
  <hr>
  
  <p>You are given a non-empty zero-indexed array A consisting of N integers.
  For each number A[i] such that 0 ≤ i &lt; N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors.
  For example, consider integer N = 5 and array A such that:</p>
  
  <pre><code>A[0] = 3
  A[1] = 1
  A[2] = 2
  A[3] = 3
  A[4] = 6
  </code></pre>
  
  <p>For the following elements:</p>
  
  <pre><code>A[0] = 3, the non-divisors are: 2, 6,
  A[1] = 1, the non-divisors are: 3, 2, 3, 6,
  A[2] = 2, the non-divisors are: 3, 3, 6,
  A[3] = 3, the non-divisors are: 2, 6,
  A[6] = 6, there aren't any non-divisors.
  </code></pre>
  
  <p>Write a function:</p>
  
  <pre><code>class Solution { public int[] solution(int[] A); }
  </code></pre>
  
  <p>that, given a non-empty zero-indexed array A consisting of N integers, returns a sequence of integers representing the numbers of non-divisors.
  The sequence should be returned as:</p>
  
  <ul>
  <li>a structure Results (in C), </li>
  <li>or a vector of integers (in C++),</li>
  <li>or a    record Results (in Pascal),</li>
  <li>or an array of integers (in any other programming language).</li>
  </ul>
  
  <p>For example, given:</p>
  
  <pre><code>A[0] = 3
  A[1] = 1
  A[2] = 2
  A[3] = 3
  A[4] = 6
  </code></pre>
  
  <p>the function should return [2, 4, 3, 2, 0], as explained above.
  Assume that:</p>
  
  <ul>
  <li>N is an integer within the range [1..50,000];</li>
  <li>each element of array A is an integer within the range [1..2 * N].</li>
  </ul>
  
  <p>Complexity:</p>
  
  <ul>
  <li>expected worst-case time complexity is O(N*log(N));</li>
  <li>expected worst-case space complexity is O(N), beyond input storage
  (not counting the storage required for input arguments).</li>
  </ul>
  
  <p>Elements of input arrays can be modified.</p>
  
  <hr>
  
  <p>I have written some solutions. But my solutions bulky and still have O(n^2) complexity.
  Can you help me with some ideas or algorithms how to do it optimally? It's not an interview task or something else. I'm just training and try to solve all tasks.
  You can find this task here: <a href="http://codility.com/demo/train/" rel="noreferrer">http://codility.com/demo/train/</a> Lesson 9, first task in lesson.</p>
  
  <p>Thank you!</p>