Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
88%
Correctness
100%
Performance
75%
This is a demo task.
Write a function:
function 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 JavaScript
Time spent on task 4 minutes
Notes
not defined yet
Task timeline
Code: 09:47:12 UTC,
java,
autosave
Code: 09:47:52 UTC,
js,
autosave
Code: 09:49:37 UTC,
js,
autosave
Code: 09:49:58 UTC,
js,
autosave
Code: 09:50:23 UTC,
js,
autosave
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100001;
const check = new Array(MAX_LENGTH);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<MAX_LENGTH;i++){
if(!check[i])
return i;
}
}
Code: 09:50:26 UTC,
js,
verify,
result: Passed
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100001;
const check = new Array(MAX_LENGTH);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<MAX_LENGTH;i++){
if(!check[i])
return i;
}
}
Analysis
Code: 09:50:33 UTC,
js,
verify,
result: Passed
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100001;
const check = new Array(MAX_LENGTH);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<MAX_LENGTH;i++){
if(!check[i])
return i;
}
}
Analysis
Code: 09:50:38 UTC,
js,
verify,
result: Passed
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100001;
const check = new Array(MAX_LENGTH);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<MAX_LENGTH;i++){
if(!check[i])
return i;
}
}
Analysis
Code: 09:50:40 UTC,
js,
final,
score: 
88
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100001;
const check = new Array(MAX_LENGTH);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<MAX_LENGTH;i++){
if(!check[i])
return i;
}
}
Analysis summary
The following issues have been detected: runtime errors.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
expand all
Performance tests
1.
0.080 s
OK
2.
0.080 s
OK
3.
0.084 s
OK
1.
0.112 s
OK
large_2
shuffled sequence 1, 2, ..., 100000 (without minus)
shuffled sequence 1, 2, ..., 100000 (without minus)
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.120 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
2.
0.116 s
OK
1.
0.116 s
OK