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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 1 minutes
Notes
not defined yet
Code: 13:36:27 UTC,
java,
autosave
Code: 13:36:35 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 4:Counting Elements
# P 4.3 MissingInteger
def solution(A):
"""
返回数组A中未出现的最小的正整数
:param A: 数组
:return: 未出现的最小的正整数
"""
x_dict = {i: 0 for i in A}
length = len(x_dict)
for i in range(1, len(x_dict) + 1):
if i not in x_dict:
return i
return length + 1
Analysis
Code: 13:36:38 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 4:Counting Elements
# P 4.3 MissingInteger
def solution(A):
"""
返回数组A中未出现的最小的正整数
:param A: 数组
:return: 未出现的最小的正整数
"""
x_dict = {i: 0 for i in A}
length = len(x_dict)
for i in range(1, len(x_dict) + 1):
if i not in x_dict:
return i
return length + 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.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.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
expand all
Performance tests
1.
0.048 s
OK
2.
0.048 s
OK
3.
0.044 s
OK
1.
0.140 s
OK
1.
0.148 s
OK
2.
0.140 s
OK
1.
0.132 s
OK