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:
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 2 minutes
Notes
not defined yet
Task timeline
Code: 09:52:27 UTC,
java,
autosave
Code: 09:52:34 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:52:45 UTC,
js,
autosave
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100002;
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:52:55 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:53:06 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-1] = true;
});
for(let i=1; i<MAX_LENGTH;i++){
if(!check[i])
return i;
}
}
Code: 09:53:13 UTC,
js,
verify,
result: Failed
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-1] = true;
});
for(let i=0; i<MAX_LENGTH;i++){
if(!check[i])
return i;
}
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 4 expected 5
1.
0.072 s
WRONG ANSWER,
got 3 expected 4
1.
0.068 s
OK
Code: 09:53:23 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-1] = true;
});
for(let i=0; i<MAX_LENGTH;i++){
if(!check[i])
return i+1;
}
}
Analysis
Code: 09:53:36 UTC,
js,
autosave
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100002;
const check = new Array(MAX_LENGTH);
positiveArray.forEach(val => {
check[val-1] = true;
});
for(let i=0; i<MAX_LENGTH;i++){
if(!check[i])
return i+1;
}
}
Code: 09:53:47 UTC,
js,
autosave
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100002;
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+1;
}
}
Code: 09:54:08 UTC,
js,
verify,
result: Passed
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100000;
const check = new Array(MAX_LENGTH+2);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<check.length;i++){
if(!check[i])
return i;
}
}
Analysis
Code: 09:54:15 UTC,
js,
verify,
result: Passed
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100000;
const check = new Array(MAX_LENGTH+2);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<check.length;i++){
if(!check[i])
return i;
}
}
Analysis
Code: 09:54:18 UTC,
js,
final,
score: 
100
function solution(A) {
const positiveArray = A.filter(val => val > 0);
if(positiveArray.length == 0)
return 1;
const MAX_LENGTH = 100000;
const check = new Array(MAX_LENGTH+2);
positiveArray.forEach(val => {
check[val] = true;
});
for(let i=1; i<check.length;i++){
if(!check[i])
return i;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.072 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.072 s
OK
1.
0.068 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.072 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.104 s
OK
2.
0.080 s
OK
3.
0.084 s
OK
1.
0.112 s
OK
1.
0.116 s
OK
2.
0.116 s
OK
1.
0.116 s
OK