Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
55%
Correctness
40%
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 21 minutes
Notes
not defined yet
Task timeline
Code: 07:36:21 UTC,
java,
autosave
Code: 07:37:49 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
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 6, in solution temp_num = int(sys.minsize) NameError: name 'sys' is not defined
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 6, in solution temp_num = int(sys.minsize) NameError: name 'sys' is not defined
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 6, in solution temp_num = int(sys.minsize) NameError: name 'sys' is not defined
Code: 07:38:06 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.040 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 7, in solution temp_num = int(sys.minsize) AttributeError: module 'sys' has no attribute 'minsize'
1.
0.040 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 7, in solution temp_num = int(sys.minsize) AttributeError: module 'sys' has no attribute 'minsize'
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 7, in solution temp_num = int(sys.minsize) AttributeError: module 'sys' has no attribute 'minsize'
Code: 07:38:14 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.044 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 7, in solution temp_num = int(sys.minsize) AttributeError: module 'sys' has no attribute 'minsize'
1.
0.040 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 7, in solution temp_num = int(sys.minsize) AttributeError: module 'sys' has no attribute 'minsize'
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 7, in solution temp_num = int(sys.minsize) AttributeError: module 'sys' has no attribute 'minsize'
Code: 07:41: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:
1 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:
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:
-3 -1
Code: 07:41:29 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, 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:
[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:
[-3, -1]
Code: 07:46:36 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
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 10, in solution if sorted_A[i+1]: IndexError: list index out of rangestdout:
1 2 3 4 6
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 10, in solution if sorted_A[i+1]: IndexError: list index out of rangestdout:
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 10, in solution if sorted_A[i+1]: IndexError: list index out of rangestdout:
-1
Code: 07:47:20 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.040 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 12, in solution print(sorted_A[i+1]) IndexError: list index out of rangestdout:
1 2 3 4 6
1.
0.040 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 12, in solution print(sorted_A[i+1]) IndexError: list index out of rangestdout:
2 3
1.
0.040 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 12, in solution print(sorted_A[i+1]) IndexError: list index out of rangestdout:
-1
Code: 07:47:25 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 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:
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
Code: 07:47:42 UTC,
py,
autosave
Code: 07:48:12 UTC,
py,
autosave
Code: 07:48:25 UTC,
py,
autosave
Code: 07:49:31 UTC,
py,
autosave
Code: 07:49:41 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
WRONG ANSWER,
got 2 expected 5
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 1 1 2 2 3
1.
0.036 s
WRONG ANSWER,
got -2 expected 1
Code: 07:49:57 UTC,
py,
autosave
Code: 07:50:27 UTC,
py,
autosave
Code: 07:50:43 UTC,
py,
autosave
Code: 07:50:45 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.040 s
WRONG ANSWER,
got 2 expected 5
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 1 1 2 2 3
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
0 -3 1 -1
Code: 07:51:52 UTC,
py,
autosave
Code: 07:52:03 UTC,
py,
autosave
Code: 07:54:10 UTC,
py,
autosave
Code: 07:54:15 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
0 1 1 1 2 2 3 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 1 1 2 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 -3 1 -1
Code: 07:55:36 UTC,
py,
autosave
Code: 07:55:51 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] >=
Code: 07:56:04 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
Code: 07:56:19 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:
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
Code: 07:56: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):
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
Analysis
Code: 07:56:31 UTC,
py,
final,
score: 
55
# 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
Analysis summary
The following issues have been detected: wrong answers.
For example, for the input [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
WRONG ANSWER,
got 3 expected 1
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.052 s
OK
2.
0.052 s
WRONG ANSWER,
got 1 expected 101
3.
0.052 s
OK
1.
0.156 s
OK
1.
0.196 s
OK
2.
0.180 s
OK
1.
0.176 s
OK