Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
100%
Correctness
100%
Performance
100%
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Total time used 20 minutes
Effective time used 20 minutes
Notes
not defined yet
Task timeline
Code: 07:27:05 UTC,
java,
autosave
Code: 07:44:58 UTC,
py,
autosave
Code: 07:45:10 UTC,
py,
autosave
Code: 07:45:30 UTC,
py,
autosave
Code: 07:45:48 UTC,
py,
autosave
Code: 07:46:07 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
min_value = 1000000
missing_list = [True] * 1000000
for num in A:
if i > 0:
missing_list[num-1] = False
for idx, is_missing in enumerate(missing_list):
if
return min_value
Code: 07:46:24 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
min_value = 1000000
missing_list = [True] * 1000000
for num in A:
if i > 0:
missing_list[num-1] = False
for idx, is_missing in enumerate(missing_list):
if is_missing:
return idx + 1
Analysis
expand all
Example tests
1.
0.048 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 9, in solution if i > 0: NameError: name 'i' is not defined
1.
0.048 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 9, in solution if i > 0: NameError: name 'i' is not defined
1.
0.048 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 9, in solution if i > 0: NameError: name 'i' is not defined
Code: 07:46:33 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
min_value = 1000000
missing_list = [True] * 1000000
for num in A:
if num > 0:
missing_list[num-1] = False
for idx, is_missing in enumerate(missing_list):
if is_missing:
return idx + 1
Analysis
Code: 07:46: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):
min_value = 1000000
missing_list = [True] * 1000000
for num in A:
if num > 0:
missing_list[num-1] = False
for idx, is_missing in enumerate(missing_list):
if is_missing:
return idx + 1
Analysis
Code: 07:46:39 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
min_value = 1000000
missing_list = [True] * 1000000
for num in A:
if num > 0:
missing_list[num-1] = False
for idx, is_missing in enumerate(missing_list):
if is_missing:
return idx + 1
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.044 s
OK
2.
0.044 s
OK
3.
0.044 s
OK
4.
0.044 s
OK
1.
0.044 s
OK
2.
0.044 s
OK
3.
0.044 s
OK
1.
0.044 s
OK
2.
0.044 s
OK
1.
0.044 s
OK
2.
0.044 s
OK
1.
0.044 s
OK
expand all
Performance tests
1.
0.056 s
OK
2.
0.052 s
OK
3.
0.056 s
OK
1.
0.140 s
OK
1.
0.148 s
OK
2.
0.148 s
OK
1.
0.148 s
OK