Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
88%
Correctness
100%
Performance
75%
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 2 minutes
Notes
not defined yet
Task timeline
Code: 06:47:34 UTC,
java,
autosave
Code: 06:47:59 UTC,
py,
autosave
Code: 06:48:02 UTC,
py,
verify,
result: Passed
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
User test case 1:
[4, 5, 6, 2]
Analysis
Code: 06:48:27 UTC,
py,
autosave
Code: 06:48:37 UTC,
py,
autosave
Code: 06:48:40 UTC,
py,
verify,
result: Passed
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 min(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
User test case 1:
[4, 5, 6, 2]
Analysis
Code: 06:48:46 UTC,
py,
verify,
result: Passed
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 min(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
User test case 1:
[4, 5, 6, 2]
Analysis
Code: 06:48:49 UTC,
py,
final,
score: 
88
Analysis summary
The following issues have been detected: timeout errors.
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.040 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.240 s
OK
2.
0.240 s
OK
1.
0.040 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
expand all
Performance tests
medium
chaotic sequences length=10005 (with minus)
chaotic sequences length=10005 (with minus)
✘
TIMEOUT ERROR
running time: 0.252 sec., time limit: 0.192 sec.
running time: 0.252 sec., time limit: 0.192 sec.
1.
0.048 s
OK
2.
0.048 s
OK
3.
0.252 s
TIMEOUT ERROR,
running time: 0.252 sec., time limit: 0.192 sec.
1.
0.160 s
OK
1.
0.180 s
OK
2.
0.184 s
OK
1.
0.356 s
OK