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
Task timeline
Code: 05:51:14 UTC,
java,
autosave
Code: 05:51:23 UTC,
py,
autosave
def solution(A):
"""
solution at https://app.codility.com/demo/results/trainingV4KX2W-3KS/
100%
idea is to take temp array of max length of array items
for all positive use item of array as index and mark in tem array as 1 ie. present item
traverse again temp array if first found value in tem array is zero that index is the smallest positive integer
:param A:
:return:
"""
max_value = max(A)
if max_value < 1:
# max is less than 1 ie. 1 is the smallest positive integer
return 1
if len(A) == 1:
# one element with 1 value
if A[0] == 1:
return 2
# one element other than 1 value
return 1
# take array of length max value
# this will work as set ie. using original array value as index for this array
temp_max_len_array = [0] * max_value
for i in range(len(A)):
# do only for positive items
if A[i] > 0:
# check at index for the value in A
if temp_max_len_array[A[i] - 1] != 1:
# set that as 1
temp_max_len_array[A[i] - 1] = 1
print(temp_max_len_array)
for i in range(len(temp_max_len_array)):
# first zero ie. this index is the smallest positive integer
if temp_max_len_array[i] == 0:
return i + 1
# if no value found between 1 to max then last value should be smallest one
return i + 2
Code: 05:51:27 UTC,
py,
verify,
result: Passed
def solution(A):
"""
solution at https://app.codility.com/demo/results/trainingV4KX2W-3KS/
100%
idea is to take temp array of max length of array items
for all positive use item of array as index and mark in tem array as 1 ie. present item
traverse again temp array if first found value in tem array is zero that index is the smallest positive integer
:param A:
:return:
"""
max_value = max(A)
if max_value < 1:
# max is less than 1 ie. 1 is the smallest positive integer
return 1
if len(A) == 1:
# one element with 1 value
if A[0] == 1:
return 2
# one element other than 1 value
return 1
# take array of length max value
# this will work as set ie. using original array value as index for this array
temp_max_len_array = [0] * max_value
for i in range(len(A)):
# do only for positive items
if A[i] > 0:
# check at index for the value in A
if temp_max_len_array[A[i] - 1] != 1:
# set that as 1
temp_max_len_array[A[i] - 1] = 1
#print(temp_max_len_array)
for i in range(len(temp_max_len_array)):
# first zero ie. this index is the smallest positive integer
if temp_max_len_array[i] == 0:
return i + 1
# if no value found between 1 to max then last value should be smallest one
return i + 2
Analysis
Code: 05:51:30 UTC,
py,
verify,
result: Passed
def solution(A):
"""
solution at https://app.codility.com/demo/results/trainingV4KX2W-3KS/
100%
idea is to take temp array of max length of array items
for all positive use item of array as index and mark in tem array as 1 ie. present item
traverse again temp array if first found value in tem array is zero that index is the smallest positive integer
:param A:
:return:
"""
max_value = max(A)
if max_value < 1:
# max is less than 1 ie. 1 is the smallest positive integer
return 1
if len(A) == 1:
# one element with 1 value
if A[0] == 1:
return 2
# one element other than 1 value
return 1
# take array of length max value
# this will work as set ie. using original array value as index for this array
temp_max_len_array = [0] * max_value
for i in range(len(A)):
# do only for positive items
if A[i] > 0:
# check at index for the value in A
if temp_max_len_array[A[i] - 1] != 1:
# set that as 1
temp_max_len_array[A[i] - 1] = 1
#print(temp_max_len_array)
for i in range(len(temp_max_len_array)):
# first zero ie. this index is the smallest positive integer
if temp_max_len_array[i] == 0:
return i + 1
# if no value found between 1 to max then last value should be smallest one
return i + 2
Analysis
Code: 05:51:32 UTC,
py,
final,
score: 
100
def solution(A):
"""
solution at https://app.codility.com/demo/results/trainingV4KX2W-3KS/
100%
idea is to take temp array of max length of array items
for all positive use item of array as index and mark in tem array as 1 ie. present item
traverse again temp array if first found value in tem array is zero that index is the smallest positive integer
:param A:
:return:
"""
max_value = max(A)
if max_value < 1:
# max is less than 1 ie. 1 is the smallest positive integer
return 1
if len(A) == 1:
# one element with 1 value
if A[0] == 1:
return 2
# one element other than 1 value
return 1
# take array of length max value
# this will work as set ie. using original array value as index for this array
temp_max_len_array = [0] * max_value
for i in range(len(A)):
# do only for positive items
if A[i] > 0:
# check at index for the value in A
if temp_max_len_array[A[i] - 1] != 1:
# set that as 1
temp_max_len_array[A[i] - 1] = 1
#print(temp_max_len_array)
for i in range(len(temp_max_len_array)):
# first zero ie. this index is the smallest positive integer
if temp_max_len_array[i] == 0:
return i + 1
# if no value found between 1 to max then last value should be smallest one
return i + 2
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.052 s
OK
4.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.040 s
OK
2.
0.044 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.044 s
OK
3.
0.052 s
OK
1.
0.144 s
OK
1.
0.172 s
OK
2.
0.160 s
OK
1.
0.164 s
OK