Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
33%
Correctness
40%
Performance
25%
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 15 minutes
Notes
not defined yet
Task timeline
Code: 06:24:17 UTC,
java,
autosave
Code: 06:24:52 UTC,
py,
verify,
result: Failed
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, 3, 6, 4, 1, 2]
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.stdout:
[-1, -3]
Code: 06:26:10 UTC,
py,
verify,
result: Failed
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:
dict_keys([1, 3, 6, 4, 2])
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
dict_keys([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.stdout:
dict_keys([-1, -3])
Code: 06:31:54 UTC,
py,
verify,
result: Failed
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:
6
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0
Code: 06:32:37 UTC,
py,
autosave
Code: 06:32:39 UTC,
py,
verify,
result: Failed
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:
6 [True]
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 [True]
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 [False]
Code: 06:32:57 UTC,
py,
verify,
result: Failed
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:
6 [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:
3 [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.stdout:
0 []
Code: 06:33:16 UTC,
py,
autosave
Code: 06:33:42 UTC,
py,
verify,
result: Failed
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:
6 {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:
3 {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.stdout:
0 set()
Code: 06:33:48 UTC,
py,
autosave
Code: 06:34:00 UTC,
py,
autosave
Code: 06:34:05 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
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:
6 {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 6}
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 {1, 2, 3} {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.stdout:
0 set() {-3, -1}
Code: 06:34:26 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
print(compare_set - a_set)
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:
6 {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 6} {5}
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 {1, 2, 3} {1, 2, 3} set()
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 set() {-3, -1} set()
Code: 06:34:44 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_set - a_set)
Code: 06:35:01 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
list(compare_set - a_set)
Code: 06:35:11 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
list(compare_set - a_set)
Code: 06:35:42 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
Code: 06:35:53 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
print(compare_list)
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:
6 {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 6} [5]
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 {1, 2, 3} {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.stdout:
0 set() {-3, -1} []
Code: 06:36:08 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
print(compare_list)
if
Code: 06:36:35 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
print(max(compare_list))
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:
6 {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 6} 5
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 20, in solution print(max(compare_list)) ValueError: max() arg is an empty sequencestdout:
3 {1, 2, 3} {1, 2, 3}
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 20, in solution print(max(compare_list)) ValueError: max() arg is an empty sequencestdout:
0 set() {-3, -1}
Code: 06:36:44 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
if
Code: 06:37:02 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
if compare_list:
print("hello")
else:
print("no")
Analysis
expand all
Example tests
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
6 {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 6} hello
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 {1, 2, 3} {1, 2, 3} no
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 set() {-3, -1} no
Code: 06:37:15 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
if compare_list:
else:
print("no")
Code: 06:37:28 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
else:
print("no")
Code: 06:37:40 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
elif max_num >= 0:
print("no")
Code: 06:37:55 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
elif max_num > 0:
return max_num + 1
Code: 06:38:01 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
print(max_num)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
print(compare_set)
print(a_set)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
6 {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 6}
1.
0.036 s
OK
stdout:
3 {1, 2, 3} {1, 2, 3}
1.
0.036 s
OK
stdout:
0 set() {-3, -1}
Code: 06:38:14 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 06:38:15 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 06:38:28 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 06:38:31 UTC,
py,
final,
score: 
33
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_num = 0
for i in A:
max_num = max(max_num, i)
compare_set = set([i for i in range(1, max_num + 1)])
a_set = set(A)
compare_list = list(compare_set - a_set)
if compare_list:
return max(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis summary
The following issues have been detected: wrong answers, timeout errors.
For example, for the input [4, 5, 6, 2] the solution returned a wrong answer (got 3 expected 1).
Analysis
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.036 s
WRONG ANSWER,
got 3 expected 1
2.
0.036 s
OK
3.
0.036 s
WRONG ANSWER,
got 89 expected 1
1.
0.208 s
WRONG ANSWER,
got 999999 expected 1
2.
0.208 s
WRONG ANSWER,
got 999999 expected 6
1.
0.036 s
OK
2.
0.036 s
WRONG ANSWER,
got 99 expected 1
1.
0.036 s
OK
expand all
Performance tests
1.
0.048 s
WRONG ANSWER,
got 788 expected 111
2.
0.048 s
OK
3.
0.220 s
TIMEOUT ERROR,
running time: 0.220 sec., time limit: 0.192 sec.
large_1
chaotic + sequence 1, 2, ..., 40000 (without minus)
chaotic + sequence 1, 2, ..., 40000 (without minus)
✘
WRONG ANSWER
got 99997 expected 40000
got 99997 expected 40000
1.
0.172 s
WRONG ANSWER,
got 99997 expected 40000
1.
0.168 s
OK
2.
0.168 s
OK
1.
0.332 s
WRONG ANSWER,
got 999835 expected 10000