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 6 minutes
Effective time used 6 minutes
Notes
not defined yet
Task timeline
Code: 12:22:19 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x < 0:
continue;
b = b + 1
if x != b:
return x
return m + 1
Analysis
Code: 12:22:49 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x < 0:
continue;
b = b + 1
if x != b:
return x
return m + 1
User test case 1:
[0, 1, 2, 3, 4, 4, 5, 10]
Analysis
Code: 12:23:06 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return x
return m + 1
User test case 1:
[0, 1, 2, 3, 4, 4, 5, 10]
Analysis
Code: 12:23:30 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
print b, x
if x != b:
return x
return m + 1
User test case 1:
[0, 1, 2, 3, 4, 4, 5, 10]
Analysis
expand all
Example tests
1.
0.020 s
WRONG ANSWER,
got 6 expected 5
stdout:
1 1 2 2 3 3 4 4 5 6
1.
0.020 s
OK
stdout:
1 1 2 2 3 3
1.
0.020 s
OK
expand all
User tests
1.
0.020 s
OK
function result: 10
function result: 10
stdout:
1 1 2 2 3 3 4 4 5 5 6 10
Code: 12:23:46 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return b
return m + 1
User test case 1:
[0, 1, 2, 3, 4, 4, 5, 10]
Analysis
Code: 12:24:02 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return b
return m + 1
User test case 1:
[-1, 2, 4, 10, 8, 0, 1, 2, 3, 4, 4, 5, 10]
Analysis
Code: 12:24:20 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return b
return m + 1
User test case 1:
[101, 102, 103]
Analysis
Code: 12:24: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):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return b
return m + 1
User test case 1:
[1, 2, 3, 4, 5, 101, 102, 103]
Analysis
Code: 12:24:38 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return b
return m + 1
User test case 1:
[-1, -2, 1, 2, 3, 4, 5, 101, 102, 103]
Analysis
Code: 12:24:44 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return b
return m + 1
User test case 1:
[-1, -2, 1, 2, 3, 4, 5, 101, 102, 103]
Analysis
Code: 12:24:47 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
m = max(A)
if m <= 0:
return 1
if m == 1:
return 2
s = sorted(list(set(A)))
b = 0
for i in xrange(len(s)):
x = s[i]
if x <= 0:
continue;
b = b + 1
if x != b:
return b
return m + 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.016 s
OK
2.
0.016 s
OK
3.
0.016 s
OK
4.
0.016 s
OK
1.
0.016 s
OK
2.
0.016 s
OK
3.
0.016 s
OK
1.
0.020 s
OK
2.
0.016 s
OK
1.
0.020 s
OK
2.
0.016 s
OK
1.
0.016 s
OK
expand all
Performance tests
1.
0.028 s
OK
2.
0.028 s
OK
3.
0.028 s
OK
1.
0.100 s
OK
1.
0.120 s
OK
2.
0.112 s
OK
1.
0.104 s
OK