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:
func Solution(A []int) []int
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 Go
Time spent on task 7 minutes
Notes
not defined yet
Code: 19:10:22 UTC,
java,
autosave
Code: 19:10:25 UTC,
go,
autosave
Code: 19:10:40 UTC,
go,
autosave
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func SqrtInt(n int) int {
return int(math.Sqrt(float64(n)))
}
// PREVIOUS RESULT: https://app.codility.com/demo/results/trainingNW9MNY-B5Q/
func Solution(A []int) []int {
n := len(A)
max, counts := 0, make([]int, n*2+1, n*2+1) // A[i] is <= 2*N
// calc counts and max
for i := 0; i < n; i++ {
counts[A[i]]++
if max < A[i] {
max = A[i]
}
}
// Sieve of 'max' also helps us to count the number of
// all the divisors of all the numbers upto and including 'max'
divs := make([]int, max+1, max+1)
for i := 2; i*i <= max; i++ {
for j := i * i; j <= max; j += i {
// sum of counts of divisors (excludes 1's and number itself)
divs[j] += counts[i]
if j/i != i {
divs[j] += counts[j/i]
}
}
}
// calc the non-divisors
res := make([]int, n, n)
for i := 0; i < n; i++ {
a := A[i]
// also discount the counts of 1s and number itself
res[i] = n - (divs[a] + counts[a] + counts[1])
}
return res
}
Code: 19:10:49 UTC,
go,
verify,
result: Failed
package solution
// you can also use imports, for example:
import "math"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func SqrtInt(n int) int {
return int(math.Sqrt(float64(n)))
}
// PREVIOUS RESULT: https://app.codility.com/demo/results/trainingNW9MNY-B5Q/
func Solution(A []int) []int {
n := len(A)
max, counts := 0, make([]int, n*2+1, n*2+1) // A[i] is <= 2*N
// calc counts and max
for i := 0; i < n; i++ {
counts[A[i]]++
if max < A[i] {
max = A[i]
}
}
// Sieve of 'max' also helps us to count the number of
// all the divisors of all the numbers upto and including 'max'
divs := make([]int, max+1, max+1)
for i := 2; i*i <= max; i++ {
for j := i * i; j <= max; j += i {
// sum of counts of divisors (excludes 1's and number itself)
divs[j] += counts[i]
if j/i != i {
divs[j] += counts[j/i]
}
}
}
// calc the non-divisors
res := make([]int, n, n)
for i := 0; i < n; i++ {
a := A[i]
// also discount the counts of 1s and number itself
res[i] = n - (divs[a] + counts[a] + counts[1])
}
return res
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got [2, 3, 3, 2, 0] expected [2, 4, 3, 2, 0]
Code: 19:16:52 UTC,
go,
autosave
package solution
// you can also use imports, for example:
import "math"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func SqrtInt(n int) int {
return int(math.Sqrt(float64(n)))
}
// PREVIOUS RESULT: https://app.codility.com/demo/results/trainingNW9MNY-B5Q/
func Solution(A []int) []int {
n := len(A)
max, counts := 0, make([]int, n*2+1, n*2+1) // A[i] is <= 2*N
// calc counts and max
for i := 0; i < n; i++ {
counts[A[i]]++
if max < A[i] {
max = A[i]
}
}
// Sieve of 'max' also helps us to count the number of
// all the divisors of all the numbers upto and including 'max'
divs := make([]int, max+1, max+1)
for i := 2; i*i <= max; i++ {
for j := i * i; j <= max; j += i {
// sum of counts of divisors (excludes 1's and number itself)
divs[j] += counts[i]
if j/i != i {
divs[j] += counts[j/i]
}
}
}
// calc the non-divisors
res := make([]int, n, n)
for i := 0; i < n; i++ {
a := A[i]
// discount the counts of number itself
res[i] = n - (divs[a] + counts[a])
// also discount the counts of 1's
if a != 1 {
res[i] -= counts[1]
}
}
return res
}
Code: 19:16:55 UTC,
go,
verify,
result: Passed
package solution
// you can also use imports, for example:
import "math"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func SqrtInt(n int) int {
return int(math.Sqrt(float64(n)))
}
// PREVIOUS RESULT: https://app.codility.com/demo/results/trainingNW9MNY-B5Q/
func Solution(A []int) []int {
n := len(A)
max, counts := 0, make([]int, n*2+1, n*2+1) // A[i] is <= 2*N
// calc counts and max
for i := 0; i < n; i++ {
counts[A[i]]++
if max < A[i] {
max = A[i]
}
}
// Sieve of 'max' also helps us to count the number of
// all the divisors of all the numbers upto and including 'max'
divs := make([]int, max+1, max+1)
for i := 2; i*i <= max; i++ {
for j := i * i; j <= max; j += i {
// sum of counts of divisors (excludes 1's and number itself)
divs[j] += counts[i]
if j/i != i {
divs[j] += counts[j/i]
}
}
}
// calc the non-divisors
res := make([]int, n, n)
for i := 0; i < n; i++ {
a := A[i]
// discount the counts of number itself
res[i] = n - (divs[a] + counts[a])
// also discount the counts of 1's
if a != 1 {
res[i] -= counts[1]
}
}
return res
}
Analysis
Code: 19:17:18 UTC,
go,
verify,
result: Passed
package solution
// you can also use imports, for example:
import "math"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func SqrtInt(n int) int {
return int(math.Sqrt(float64(n)))
}
// PREVIOUS RESULT: https://app.codility.com/demo/results/trainingNW9MNY-B5Q/
func Solution(A []int) []int {
n := len(A)
max, counts := 0, make([]int, n*2+1, n*2+1) // A[i] is <= 2*N
// calc counts and max
for i := 0; i < n; i++ {
counts[A[i]]++
if max < A[i] {
max = A[i]
}
}
// Sieve of 'max' also helps us to count the number of
// all the divisors of all the numbers upto and including 'max'
divs := make([]int, max+1, max+1)
for i := 2; i*i <= max; i++ {
for j := i * i; j <= max; j += i {
// sum of counts of divisors (excludes 1's and number itself)
divs[j] += counts[i]
if j/i != i {
divs[j] += counts[j/i]
}
}
}
// calc the non-divisors
res := make([]int, n, n)
for i := 0; i < n; i++ {
a := A[i]
// discount the counts of number itself
res[i] = n - (divs[a] + counts[a])
// also discount the counts of 1's
if a != 1 {
res[i] -= counts[1]
}
}
return res
}
Analysis
Code: 19:17:20 UTC,
go,
final,
score: 
100
package solution
// you can also use imports, for example:
import "math"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func SqrtInt(n int) int {
return int(math.Sqrt(float64(n)))
}
// PREVIOUS RESULT: https://app.codility.com/demo/results/trainingNW9MNY-B5Q/
func Solution(A []int) []int {
n := len(A)
max, counts := 0, make([]int, n*2+1, n*2+1) // A[i] is <= 2*N
// calc counts and max
for i := 0; i < n; i++ {
counts[A[i]]++
if max < A[i] {
max = A[i]
}
}
// Sieve of 'max' also helps us to count the number of
// all the divisors of all the numbers upto and including 'max'
divs := make([]int, max+1, max+1)
for i := 2; i*i <= max; i++ {
for j := i * i; j <= max; j += i {
// sum of counts of divisors (excludes 1's and number itself)
divs[j] += counts[i]
if j/i != i {
divs[j] += counts[j/i]
}
}
}
// calc the non-divisors
res := make([]int, n, n)
for i := 0; i < n; i++ {
a := A[i]
// discount the counts of number itself
res[i] = n - (divs[a] + counts[a])
// also discount the counts of 1's
if a != 1 {
res[i] -= counts[1]
}
}
return res
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N * log(N))
expand all
Correctness tests
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
expand all
Performance tests
1.
0.004 s
OK
2.
0.001 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.016 s
OK
2.
0.016 s
OK
1.
0.028 s
OK
2.
0.008 s
OK
3.
0.012 s
OK