Tasks Details
medium
Calculate the number of elements of an array that are not divisors of each element.
Task Score
100%
Correctness
100%
Performance
100%
You are given an array A consisting of N integers.
For each number A[i] such that 0 ≤ i < 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:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 6For the following elements:
- 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[4] = 6, there aren't any non-divisors.
Write a function:
class Solution { public int[] solution(int[] A); }
that, given an array A consisting of N integers, returns a sequence of integers representing the amount of non-divisors.
Result array should be returned as an array of integers.
For example, given:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 6the function should return [2, 4, 3, 2, 0], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- each element of array A is an integer within the range [1..2 * N].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 21
Time spent on task 39 minutes
Notes
not defined yet
Code: 10:44:20 UTC,
java,
autosave
Code: 10:44:50 UTC,
java,
autosave
Code: 11:15:49 UTC,
java,
autosave
Code: 11:16:19 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is []1,
int[] divisors = new int[(A.length * 2) + 1];
return result;
}
}
Code: 11:16:29 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
return result;
}
}
Code: 11:16:50 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
return result;
}
}
Code: 11:18:51 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for
return result;
}
}
Code: 11:19:18 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx = 0; idx < A.length; idx++) {
int count = 0;
for (int idx)
}
return result;
}
}
Code: 11:20:32 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx < A.length; idx++) {
int count = 0;
for (int idx)
}
return result;
}
}
Code: 11:21:03 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
if (A[idx1] % idx2 == 0)
}
}
return result;
}
}
Code: 11:21:34 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
// Common factor
if (A[idx1] % idx2 == 0) {
count += divisors[idx2];
if (A[idx1] / idx2 != idx2) {
count
}
}
}
}
return result;
}
}
Code: 11:21:44 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
// Common factor
if (A[idx1] % idx2 == 0) {
count += divisors[idx2];
if (A[idx1] / idx2 != idx2) {
count += divisors[A[idx1] / idx2];
}
}
}
}
return result;
}
}
Code: 11:22:04 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
// Common factor
if (A[idx1] % idx2 == 0) {
count += divisors[idx2];
// Not square root
if (A[idx1] / idx2 != idx2) {
count += divisors[A[idx1] / idx2];
}
}
}
}
return result;
}
}
Code: 11:22:22 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
// Common factor
if (A[idx1] % idx2 == 0) {
count += divisors[idx2];
// Not square root
if (A[idx1] / idx2 != idx2) {
count += divisors[A[idx1] / idx2];
}
}
}
result[idx1] = A.length - count;
}
return result;
}
}
Code: 11:22:26 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
// Common factor
if (A[idx1] % idx2 == 0) {
count += divisors[idx2];
// Not square root
if (A[idx1] / idx2 != idx2) {
count += divisors[A[idx1] / idx2];
}
}
}
result[idx1] = A.length - count;
}
return result;
}
}
Analysis
Code: 11:22:33 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
// Common factor
if (A[idx1] % idx2 == 0) {
count += divisors[idx2];
// Not square root
if (A[idx1] / idx2 != idx2) {
count += divisors[A[idx1] / idx2];
}
}
}
result[idx1] = A.length - count;
}
return result;
}
}
Analysis
Code: 11:22:38 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] result = new int[A.length];
// Initializing array, element's range is [1, 2 * N]
int[] divisors = new int[(A.length * 2) + 1];
for (int idx = 0; idx < A.length; idx++) {
divisors[A[idx]]++;
}
for (int idx1 = 0; idx1 < A.length; idx1++) {
int count = 0;
for (int idx2 = 1; idx2 * idx2 <= A[idx1]; idx2++) {
// Common factor
if (A[idx1] % idx2 == 0) {
count += divisors[idx2];
// Not square root
if (A[idx1] / idx2 != idx2) {
count += divisors[A[idx1] / idx2];
}
}
}
result[idx1] = A.length - count;
}
return result;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N * log(N))
expand all
Correctness tests
1.
0.008 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
expand all
Performance tests
1.
0.076 s
OK
2.
0.072 s
OK
1.
0.308 s
OK
2.
0.284 s
OK
1.
0.504 s
OK
2.
0.552 s
OK
1.
1.000 s
OK
2.
0.376 s
OK
3.
0.504 s
OK