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 9 minutes
Notes
not defined yet
Task timeline
Code: 07:25:11 UTC,
java,
autosave
Code: 07:29:11 UTC,
py,
autosave
Code: 07:29:31 UTC,
py,
autosave
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).sort()
if compare_list:
return min(compare_list)
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:29:52 UTC,
py,
verify,
result: Failed
Analysis
Code: 07:30:06 UTC,
py,
autosave
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).sort(rever)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:30:16 UTC,
py,
autosave
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).sort(reverse= True)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:30:17 UTC,
py,
verify,
result: Failed
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).sort(reverse= True)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 07:30:39 UTC,
py,
verify,
result: Failed
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).sort(reverse= True)
if compare_list:
return compare_list.pop()
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 07:30:47 UTC,
py,
verify,
result: Failed
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).sort(reverse= True)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 07:30:50 UTC,
py,
autosave
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).sort(reverse= True)
if compare_list:
p
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:30:55 UTC,
py,
verify,
result: Failed
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).sort(reverse= True)
if compare_list:
print(compare_list)
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 07:31:04 UTC,
py,
verify,
result: Failed
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).sort(reverse= True)
if compare_list:
print(compare_list)
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 07:31:20 UTC,
py,
autosave
Code: 07:31:26 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)
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
[5]
1.
0.036 s
OK
stdout:
[]
1.
0.036 s
OK
stdout:
[]
Code: 07:31:50 UTC,
py,
autosave
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).sort()
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:31:54 UTC,
py,
verify,
result: Failed
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).sort()
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
expand all
Example tests
1.
0.036 s
WRONG ANSWER,
got 7 expected 5
stdout:
None
1.
0.036 s
OK
stdout:
None
1.
0.036 s
OK
stdout:
None
Code: 07:32:11 UTC,
py,
autosave
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 = sort(list(compare_set - a_set))
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:32:18 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)
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
[5]
1.
0.036 s
OK
stdout:
[]
1.
0.036 s
OK
stdout:
[]
Code: 07:32:44 UTC,
py,
autosave
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 = sorlist(compare_set - a_set)
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:33:03 UTC,
py,
autosave
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 = sorted(list(compare_set - a_set), reverse=True)
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:33:04 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 = sorted(list(compare_set - a_set), reverse=True)
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
expand all
Example tests
1.
0.044 s
OK
stdout:
[5]
1.
0.036 s
OK
stdout:
[]
1.
0.036 s
OK
stdout:
[]
Code: 07:33:19 UTC,
py,
autosave
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 = sorted(list(compare_set - a_set), reverse=True)
print(compare_list)
if compare_list:
return compare_list.pop()
elif max_num > 0:
return max_num + 1
else:
return 1
Code: 07:33:30 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 = sorted(list(compare_set - a_set))
print(compare_list)
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
expand all
Example tests
1.
0.040 s
OK
stdout:
[5]
1.
0.036 s
OK
stdout:
[]
1.
0.036 s
OK
stdout:
[]
Code: 07:33: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 = sorted(list(compare_set - a_set))
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 07:33:43 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 = sorted(list(compare_set - a_set))
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
Analysis
Code: 07:33:46 UTC,
py,
final,
score: 
88
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 = sorted(list(compare_set - a_set))
if compare_list:
return compare_list[0]
elif max_num > 0:
return max_num + 1
else:
return 1
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.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.216 s
OK
2.
0.212 s
OK
1.
0.036 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.228 sec., time limit: 0.192 sec.
running time: 0.228 sec., time limit: 0.192 sec.
1.
0.048 s
OK
2.
0.048 s
OK
3.
0.228 s
TIMEOUT ERROR,
running time: 0.228 sec., time limit: 0.192 sec.
1.
0.156 s
OK
1.
0.164 s
OK
2.
0.172 s
OK
1.
0.324 s
OK