Tasks Details
medium
Calculate the number of elements of an array that are not divisors of each element.
Task Score
66%
Correctness
100%
Performance
25%
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 1 minutes
Notes
not defined yet
Code: 06:43:06 UTC,
java,
autosave
Code: 06:43:11 UTC,
go,
autosave
Code: 06:43:21 UTC,
go,
verify,
result: Passed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
package solution
type Element struct {
index []int
count int
nonDivisorsCount int
}
func Solution(A []int) []int {
lenA := len(A)
// remove duplicates
mapA := uniqueMap(A)
for k, v := range mapA {
/**
For 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.
E C ND
1 1 0
2 1 0
3 2 0
6 1 0
*/
// loop through all the elements.
// calculate non divisors.
for i := 0; i < lenA; i++ {
if A[i] < k && k%A[i] != 0 {
v.nonDivisorsCount++
} else if A[i] > k {
v.nonDivisorsCount++
}
}
}
for i := 0; i < lenA; i++ {
if v, e := mapA[A[i]]; e {
A[i] = v.nonDivisorsCount
}
}
return A
}
// Returns map of elements with element property
func uniqueMap(A []int) map[int]*Element {
uniqueNum := map[int]*Element{}
for k, a := range A {
if _, ok := uniqueNum[a]; !ok {
uniqueNum[a] = &Element{[]int{k}, 1, 0}
} else {
uniqueNum[a].count = uniqueNum[a].count + 1
uniqueNum[a].index = append(uniqueNum[a].index, k)
// uniqueNum[a] = element{count, 0}
}
}
return uniqueNum
}
/**
Previous logic before creating uniqueMap()
i := 1
for i < lenA {
nonDivisorsCount := 0
if i != lenA-1 {
if copyA[i] != copyA[i+1] {
//check if the number already exists in map
if _, exists := mapA[copyA[i]]; !exists {
mapA[copyA[i]] = 0
// check previous numbers divide i
for j := i - 1; j >= 0; j-- {
if copyA[i]%copyA[j] != 0 {
nonDivisorsCount++
}
}
mapA[copyA[i]] += lenA - (1 + i + nonDivisorsCount)
}
}
}
i++
}
for k, a := range A {
A[k] = mapA[a]
}
return A
*/
Analysis
Code: 06:43:25 UTC,
go,
verify,
result: Passed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
package solution
type Element struct {
index []int
count int
nonDivisorsCount int
}
func Solution(A []int) []int {
lenA := len(A)
// remove duplicates
mapA := uniqueMap(A)
for k, v := range mapA {
/**
For 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.
E C ND
1 1 0
2 1 0
3 2 0
6 1 0
*/
// loop through all the elements.
// calculate non divisors.
for i := 0; i < lenA; i++ {
if A[i] < k && k%A[i] != 0 {
v.nonDivisorsCount++
} else if A[i] > k {
v.nonDivisorsCount++
}
}
}
for i := 0; i < lenA; i++ {
if v, e := mapA[A[i]]; e {
A[i] = v.nonDivisorsCount
}
}
return A
}
// Returns map of elements with element property
func uniqueMap(A []int) map[int]*Element {
uniqueNum := map[int]*Element{}
for k, a := range A {
if _, ok := uniqueNum[a]; !ok {
uniqueNum[a] = &Element{[]int{k}, 1, 0}
} else {
uniqueNum[a].count = uniqueNum[a].count + 1
uniqueNum[a].index = append(uniqueNum[a].index, k)
// uniqueNum[a] = element{count, 0}
}
}
return uniqueNum
}
/**
Previous logic before creating uniqueMap()
i := 1
for i < lenA {
nonDivisorsCount := 0
if i != lenA-1 {
if copyA[i] != copyA[i+1] {
//check if the number already exists in map
if _, exists := mapA[copyA[i]]; !exists {
mapA[copyA[i]] = 0
// check previous numbers divide i
for j := i - 1; j >= 0; j-- {
if copyA[i]%copyA[j] != 0 {
nonDivisorsCount++
}
}
mapA[copyA[i]] += lenA - (1 + i + nonDivisorsCount)
}
}
}
i++
}
for k, a := range A {
A[k] = mapA[a]
}
return A
*/
Analysis
Code: 06:43:28 UTC,
go,
final,
score: 
66
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
package solution
type Element struct {
index []int
count int
nonDivisorsCount int
}
func Solution(A []int) []int {
lenA := len(A)
// remove duplicates
mapA := uniqueMap(A)
for k, v := range mapA {
/**
For 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.
E C ND
1 1 0
2 1 0
3 2 0
6 1 0
*/
// loop through all the elements.
// calculate non divisors.
for i := 0; i < lenA; i++ {
if A[i] < k && k%A[i] != 0 {
v.nonDivisorsCount++
} else if A[i] > k {
v.nonDivisorsCount++
}
}
}
for i := 0; i < lenA; i++ {
if v, e := mapA[A[i]]; e {
A[i] = v.nonDivisorsCount
}
}
return A
}
// Returns map of elements with element property
func uniqueMap(A []int) map[int]*Element {
uniqueNum := map[int]*Element{}
for k, a := range A {
if _, ok := uniqueNum[a]; !ok {
uniqueNum[a] = &Element{[]int{k}, 1, 0}
} else {
uniqueNum[a].count = uniqueNum[a].count + 1
uniqueNum[a].index = append(uniqueNum[a].index, k)
// uniqueNum[a] = element{count, 0}
}
}
return uniqueNum
}
/**
Previous logic before creating uniqueMap()
i := 1
for i < lenA {
nonDivisorsCount := 0
if i != lenA-1 {
if copyA[i] != copyA[i+1] {
//check if the number already exists in map
if _, exists := mapA[copyA[i]]; !exists {
mapA[copyA[i]] = 0
// check previous numbers divide i
for j := i - 1; j >= 0; j-- {
if copyA[i]%copyA[j] != 0 {
nonDivisorsCount++
}
}
mapA[copyA[i]] += lenA - (1 + i + nonDivisorsCount)
}
}
}
i++
}
for k, a := range A {
A[k] = mapA[a]
}
return A
*/
Analysis summary
The following issues have been detected: timeout errors.
Analysis
Detected time complexity:
O(N ** 2)
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
medium_random
medium, random numbers length = 5,000
medium, random numbers length = 5,000
✘
TIMEOUT ERROR
running time: 0.184 sec., time limit: 0.100 sec.
running time: 0.184 sec., time limit: 0.100 sec.
1.
0.184 s
TIMEOUT ERROR,
running time: 0.184 sec., time limit: 0.100 sec.
2.
0.044 s
OK
large_range
1, 2, ..., N, length = ~20,000
1, 2, ..., N, length = ~20,000
✘
TIMEOUT ERROR
running time: 3.648 sec., time limit: 0.100 sec.
running time: 3.648 sec., time limit: 0.100 sec.
1.
3.648 s
TIMEOUT ERROR,
running time: 3.648 sec., time limit: 0.100 sec.
2.
0.008 s
OK
large_random
large, random numbers, length = ~30,000
large, random numbers, length = ~30,000
✘
TIMEOUT ERROR
Killed. Hard limit reached: 6.000 sec.
Killed. Hard limit reached: 6.000 sec.
1.
6.000 s
TIMEOUT ERROR,
Killed. Hard limit reached: 6.000 sec.
2.
0.044 s
OK
1.
0.020 s
OK
2.
0.012 s
OK
3.
0.016 s
OK