Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
66%
Correctness
60%
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 4 minutes
Notes
not defined yet
Task timeline
Code: 08:06:20 UTC,
java,
autosave
Code: 08:06:52 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
for i, num in enumerate(sorted_A):
if i+1 < length_A:
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
Code: 08:07:26 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
for i, num in enumerate(sorted_A):
if i+1 < length_A:
orub
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
Code: 08:07:37 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
for i, num in enumerate(sorted_A):
if i+1 < length_A:
print("DEBUG1")
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
User test case 1:
[3]
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
DEBUG1 DEBUG1 DEBUG1 DEBUG1 DEBUG1
1.
0.036 s
OK
stdout:
DEBUG1 DEBUG1
1.
0.036 s
OK
stdout:
DEBUG1
Code: 08:09:09 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
if
for i, num in enumerate(sorted_A):
if i+1 < length_A:
print("DEBUG1")
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
Code: 08:09:20 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
if length_A == 1:
return
for i, num in enumerate(sorted_A):
if i+1 < length_A:
print("DEBUG1")
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
Code: 08:09:42 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
if length_A == 1 and A[0] != 1:
return
for i, num in enumerate(sorted_A):
if i+1 < length_A:
print("DEBUG1")
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
Code: 08:09:49 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
if length_A == 1 and A[0] != 1:
return 1
for i, num in enumerate(sorted_A):
if i+1 < length_A:
print("DEBUG1")
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
User test case 1:
[3]
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
DEBUG1 DEBUG1 DEBUG1 DEBUG1 DEBUG1
1.
0.036 s
OK
stdout:
DEBUG1 DEBUG1
1.
0.036 s
OK
stdout:
DEBUG1
Code: 08:09:58 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
if length_A == 1 and A[0] != 1:
return 1
for i, num in enumerate(sorted_A):
if i+1 < length_A:
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
User test case 1:
[3]
Analysis
Code: 08:10:03 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
if length_A == 1 and A[0] != 1:
return 1
for i, num in enumerate(sorted_A):
if i+1 < length_A:
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
User test case 1:
[3]
Analysis
Code: 08:10:05 UTC,
py,
final,
score: 
66
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
sorted_A = sorted(A)
length_A = len(A)
if length_A == 1 and A[0] != 1:
return 1
for i, num in enumerate(sorted_A):
if i+1 < length_A:
if sorted_A[i+1] > num + 1:
if num + 1 > 0:
return num + 1
if sorted_A[0] >= 1:
return sorted_A.pop() + 1
else:
return 1
Analysis summary
The following issues have been detected: wrong answers.
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 94 expected 1
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
WRONG ANSWER,
got 150 expected 1
1.
0.036 s
OK
expand all
Performance tests
1.
0.048 s
OK
2.
0.048 s
WRONG ANSWER,
got 1 expected 101
3.
0.048 s
OK
1.
0.148 s
OK
1.
0.192 s
OK
2.
0.176 s
OK
1.
0.168 s
OK