Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
100%
Correctness
100%
Performance
100%
This is a demo task.
Write a function:
def solution(A)
that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−1,000,000..1,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 7 minutes
Notes
not defined yet
Task timeline
Code: 08:16:48 UTC,
java,
autosave
Code: 08:18:49 UTC,
py,
autosave
Code: 08:19:12 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
print(j)
Code: 08:19:27 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
print(j)
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
1 2 3 4 5 6
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
1 2 3
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
Code: 08:21:19 UTC,
py,
autosave
Code: 08:21:39 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
if count_dict[j] == 0:
return j
Code: 08:21:52 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
if count_dict[j] == 0:
return j
Analysis
expand all
Example tests
1.
0.036 s
OK
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
Code: 08:22:09 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
if count_dict[j] == 0:
return j
Code: 08:22:39 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
if count_dict[j] == 0:
return j
if max_num <= 0:
return 1
else:
return max_num +
Code: 08:22:42 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
if count_dict[j] == 0:
return j
if max_num <= 0:
return 1
else:
return max_num + 1
Analysis
Code: 08:22:46 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
if count_dict[j] == 0:
return j
if max_num <= 0:
return 1
else:
return max_num + 1
Analysis
Code: 08:22:49 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
max_num = -1
count_dict = collections.defaultdict(int)
for i in A:
max_num = max(i, max_num)
count_dict[i] += 1
for j in range(1, max_num + 1):
if count_dict[j] == 0:
return j
if max_num <= 0:
return 1
else:
return max_num + 1
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
1.
0.044 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
expand all
Performance tests
1.
0.048 s
OK
2.
0.048 s
OK
3.
0.052 s
OK
1.
0.172 s
OK
1.
0.204 s
OK
2.
0.196 s
OK
1.
0.160 s
OK