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 17 minutes
Notes
not defined yet
Task timeline
Code: 04:19:21 UTC,
java,
autosave
Code: 04:19:40 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0 && !set.has(A[i])) {
set.add(A[i])
}
}
const array = [...set]
if (array.length === 0) {
return 1
} else {
for (let i=0; i<array.length; i++) {
if (array.indexOf(i+1) === -1) {
return i+1
}
}
return array.length + 1
}
}
Code: 04:19:55 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0 && !set.has(A[i])) {
set.add(A[i])
}
}
// const array = [...set]
// if (array.length === 0) {
// return 1
// } else {
// for (let i=0; i<array.length; i++) {
// if (array.indexOf(i+1) === -1) {
// return i+1
// }
// }
// return array.length + 1
// }
}
Code: 04:20:24 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0 && !set.has(A[i])) {
set.add(A[i])
}
}
console.log(set)
// const array = [...set]
// if (array.length === 0) {
// return 1
// } else {
// for (let i=0; i<array.length; i++) {
// if (array.indexOf(i+1) === -1) {
// return i+1
// }
// }
// return array.length + 1
// }
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
Set { 1, 3, 6, 4, 2 }
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
Set { 1, 2, 3 }
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
Set {}
Code: 04:20:44 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0 && !set.has(A[i])) {
set.add(A[i])
}
}
console.log(set)
const
// const array = [...set]
// if (array.length === 0) {
// return 1
// } else {
// for (let i=0; i<array.length; i++) {
// if (array.indexOf(i+1) === -1) {
// return i+1
// }
// }
// return array.length + 1
// }
}
Code: 04:21:03 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0 && !set.has(A[i])) {
set.add(A[i])
}
}
console.log(set)
const check = Array(set.size).fill(false)
// const array = [...set]
// if (array.length === 0) {
// return 1
// } else {
// for (let i=0; i<array.length; i++) {
// if (array.indexOf(i+1) === -1) {
// return i+1
// }
// }
// return array.length + 1
// }
}
Code: 04:21:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0 && !set.has(A[i])) {
set.add(A[i])
}
}
console.log(set)
const check = Array(set.size).fill(false)
const array = [...set]
if (array.length === 0) {
return 1
} else {
for (let i=0; i<array.length; i++) {
if (array.indexOf(i+1) === -1) {
return i+1
}
}
return array.length + 1
}
}
Code: 04:21:33 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0 && !set.has(A[i])) {
set.add(A[i])
}
}
console.log(set)
const check = Array(set.size).fill(false)
// const array = [...set]
// if (array.length === 0) {
// return 1
// } else {
// for (let i=0; i<array.length; i++) {
// if (array.indexOf(i+1) === -1) {
// return i+1
// }
// }
// return array.length + 1
// }
}
Code: 04:25:00 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const set = new Set()
for (let i=0; i<A.length; i++) {
if (A[i] > 0) {
set.add(A[i])
}
}
console.log(set)
const check = Array(set.size).fill(false)
// const array = [...set]
// if (array.length === 0) {
// return 1
// } else {
// for (let i=0; i<array.length; i++) {
// if (array.indexOf(i+1) === -1) {
// return i+1
// }
// }
// return array.length + 1
// }
}
Code: 04:26:18 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 1, 2, 3, 4, 6 ]
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 2, 3 ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[]
Code: 04:26:52 UTC,
js,
autosave
Code: 04:27:13 UTC,
js,
autosave
Code: 04:27:23 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next i > next + 1) {
return next
}
}
return 1
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
solution.js:9 if (i !== next i > next + 1) { ^ SyntaxError: Unexpected identifier at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
solution.js:9 if (i !== next i > next + 1) { ^ SyntaxError: Unexpected identifier at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
solution.js:9 if (i !== next i > next + 1) { ^ SyntaxError: Unexpected identifier at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
Code: 04:27:32 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next && i > next + 1) {
return next
}
}
return 1
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 1 expected 5
1.
0.072 s
WRONG ANSWER,
got 1 expected 4
1.
0.072 s
OK
Code: 04:27:42 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next && i > next + 1) {
return next
}
}
return next
}
Analysis
expand all
Example tests
1.
0.092 s
WRONG ANSWER,
got 1 expected 5
1.
0.076 s
WRONG ANSWER,
got 1 expected 4
1.
0.080 s
OK
Code: 04:28:06 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next && i > next + 1) {
return next
}
if ()
}
return next
}
Code: 04:28:16 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next && i > next + 1) {
return next
}
if (i === next + 1) {
next += 1
}
}
return next
}
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: 04:29:38 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next i > next + 1) {
return next
}
if (i === next + 1) {
next += 1
}
}
return next
}
Code: 04:29:49 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next || i > next + 1) {
return next
}
if (i === next + 1) {
next += 1
}
}
return next
}
Code: 04:29:52 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const sorted = A.filter((i) => i > 0).sort((a, b) => a- b)
let next = 1
for (let i of sorted) {
if (i !== next || i > next + 1) {
return next
}
if (i === next + 1) {
next += 1
}
}
return next
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 1 expected 5
1.
0.072 s
WRONG ANSWER,
got 1 expected 4
1.
0.072 s
OK
Code: 04:31:55 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ true, true, true, true, true, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ true, true, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ true, true ]
Code: 04:32:10 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, true, true, true, true, false, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, true, true, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, false, '-1': true, '-3': true ]
Code: 04:32:22 UTC,
js,
autosave
Code: 04:32:24 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ true, true, true, true, false, true ]
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ true, true, true ]
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, false, '-2': true, '-4': true ]
Code: 04:32:47 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, true, true, true, false, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, true, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, false ]
Code: 04:32:58 UTC,
js,
autosave
Code: 04:33:02 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
console.log(A[i])
if (A[i]-1 > 0) {
checker[A[i]-1] = true
}
}
console.log(checker)
}
Analysis
expand all
Example tests
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
1 3 6 4 1 2 [ false, true, true, true, false, true ]
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
1 2 3 [ false, true, true ]
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
-1 -3 [ false, false ]
Code: 04:33:27 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
console.log(A[i])
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
console.log(checker)
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
1 3 6 4 1 2 [ true, true, true, true, false, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
1 2 3 [ true, true, true ]
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
-1 -3 [ false, false ]
Code: 04:33:39 UTC,
js,
autosave
Code: 04:33:47 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
console.log(A[i])
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
return checker.indexOf(false)
}
Analysis
expand all
Example tests
1.
0.076 s
WRONG ANSWER,
got 4 expected 5
stdout:
1 3 6 4 1 2
1.
0.076 s
WRONG ANSWER,
got -1 expected 4
stdout:
1 2 3
1.
0.072 s
WRONG ANSWER,
got 0 expected 1
stdout:
-1 -3
Code: 04:34:05 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {=
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
return checker.indexOf(false) + 1
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
solution.js:7 for (let i=0; i<A.length; i++) {= ^ SyntaxError: Unexpected token = at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
solution.js:7 for (let i=0; i<A.length; i++) {= ^ SyntaxError: Unexpected token = at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
solution.js:7 for (let i=0; i<A.length; i++) {= ^ SyntaxError: Unexpected token = at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
Code: 04:34:10 UTC,
js,
autosave
Code: 04:34:36 UTC,
js,
verify,
result: Failed
Analysis
Code: 04:34:45 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
i
return checker.indexOf(false) + 1
}
Code: 04:34:58 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
const index = checker.indexOf(false)
return checker.indexOf(false) + 1
}
Code: 04:35:11 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
const index = checker.indexOf(false)
return index === -1 ? checker.length : index + 1
}
Analysis
Code: 04:35:19 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
const index = checker.indexOf(false)
return index === -1 ? checker.length + 1 : index + 1
}
Analysis
Code: 04:35:33 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
const index = checker.indexOf(false)
return index === -1 ? checker.length + 1 : index + 1
}
Analysis
Code: 04:35:37 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const checker = Array(A.length).fill(false)
for (let i=0; i<A.length; i++) {
if (A[i] > 0) {
checker[A[i]-1] = true
}
}
const index = checker.indexOf(false)
return index === -1 ? checker.length + 1 : index + 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.072 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
3.
0.072 s
OK
1.
0.068 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.076 s
OK
expand all
Performance tests
1.
0.080 s
OK
2.
0.080 s
OK
3.
0.088 s
OK
1.
0.124 s
OK
1.
0.112 s
OK
2.
0.112 s
OK
1.
0.404 s
OK