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:
function solution(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 JavaScript
Time spent on task 6 minutes
Notes
not defined yet
Task timeline
Code: 07:50:00 UTC,
java,
autosave
Code: 07:51:10 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const N = A.length;
const counter = new Array(2 * N + 1).fill(0);
for (let i = 1; i <= 2 * N; i++) {
counter[A[i]]++;
}
const nonDivisors = [];
for (let i = 0; i < N; i++) {
let nDivisors = 0;
for (let j = 1; j * j <= A[i]; j++) {
if (A[i] % j === 0) {
nDivisors += counter[j];
if (j * j !== A[i]) nDivisors += counter[A[i] / j];
}
}
nonDivisors.push(N - nDivisors);
}
return nonDivisors;
}
Code: 07:51:10 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const N = A.length;
const counter = new Array(2 * N + 1).fill(0);
for (let i = 1; i <= 2 * N; i++) {
counter[A[i]]++;
}
const nonDivisors = [];
for (let i = 0; i < N; i++) {
let nDivisors = 0;
for (let j = 1; j * j <= A[i]; j++) {
if (A[i] % j === 0) {
nDivisors += counter[j];
if (j * j !== A[i]) nDivisors += counter[A[i] / j];
}
}
nonDivisors.push(N - nDivisors);
}
return nonDivisors;
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got [3, 4, 3, 3, 1] expected [2, 4, 3, 2, 0]
Code: 07:55:27 UTC,
js,
autosave
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const N = A.length;
const counter = new Array(2 * N + 1).fill(0);
for (let i = 0; i < N; i++) {
counter[A[i]]++;
}
const nonDivisors = [];
for (let i = 0; i < N; i++) {
let nDivisors = 0;
for (let j = 1; j * j <= A[i]; j++) {
if (A[i] % j === 0) {
nDivisors += counter[j];
if (j * j !== A[i]) nDivisors += counter[A[i] / j];
}
}
nonDivisors.push(N - nDivisors);
}
return nonDivisors;
}
Code: 07:55:27 UTC,
js,
verify,
result: Passed
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const N = A.length;
const counter = new Array(2 * N + 1).fill(0);
for (let i = 0; i < N; i++) {
counter[A[i]]++;
}
const nonDivisors = [];
for (let i = 0; i < N; i++) {
let nDivisors = 0;
for (let j = 1; j * j <= A[i]; j++) {
if (A[i] % j === 0) {
nDivisors += counter[j];
if (j * j !== A[i]) nDivisors += counter[A[i] / j];
}
}
nonDivisors.push(N - nDivisors);
}
return nonDivisors;
}
Analysis
Code: 07:55:32 UTC,
js,
verify,
result: Passed
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const N = A.length;
const counter = new Array(2 * N + 1).fill(0);
for (let i = 0; i < N; i++) {
counter[A[i]]++;
}
const nonDivisors = [];
for (let i = 0; i < N; i++) {
let nDivisors = 0;
for (let j = 1; j * j <= A[i]; j++) {
if (A[i] % j === 0) {
nDivisors += counter[j];
if (j * j !== A[i]) nDivisors += counter[A[i] / j];
}
}
nonDivisors.push(N - nDivisors);
}
return nonDivisors;
}
Analysis
Code: 07:55:34 UTC,
js,
final,
score: 
100
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const N = A.length;
const counter = new Array(2 * N + 1).fill(0);
for (let i = 0; i < N; i++) {
counter[A[i]]++;
}
const nonDivisors = [];
for (let i = 0; i < N; i++) {
let nDivisors = 0;
for (let j = 1; j * j <= A[i]; j++) {
if (A[i] % j === 0) {
nDivisors += counter[j];
if (j * j !== A[i]) nDivisors += counter[A[i] / j];
}
}
nonDivisors.push(N - nDivisors);
}
return nonDivisors;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N * log(N))
expand all
Correctness tests
1.
0.068 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
expand all
Performance tests
1.
0.084 s
OK
2.
0.084 s
OK
1.
0.112 s
OK
2.
0.112 s
OK
1.
0.136 s
OK
2.
0.152 s
OK
1.
0.216 s
OK
2.
0.104 s
OK
3.
0.112 s
OK