Tasks Details
easy
Find the missing element in a given permutation.
Task Score
100%
Correctness
100%
Performance
100%
An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Write a function:
function solution(A);
that, given an array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2 A[1] = 3 A[2] = 1 A[3] = 5the function should return 4, as it is the missing element.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- the elements of A are all distinct;
- each element of array A is an integer within the range [1..(N + 1)].
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 7 minutes
Notes
not defined yet
Task timeline
Code: 09:25:36 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.160 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Invalid result type, integer expected, non-integer number found
Code: 09:26:07 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.163 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Invalid result type, integer expected, non-integer number found
Code: 09:26:22 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) {
// write your code in JavaScript (Node.js 4.0.0)
console.log(A.length);
var sum;
for(var i = 0; i < A.length; i++){
sum += A[i];
}
return (A.length*(A.length+1))/2 - sum;
}
Analysis
expand all
Example tests
1.
0.165 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Invalid result type, integer expected, non-integer number foundstdout:
4
Code: 09:28:56 UTC,
js,
verify,
result: Passed
Analysis
Code: 09:29:16 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) {
// write your code in JavaScript (Node.js 4.0.0)
var sum = 0;
for(var i = 0; i < A.length; i++){
sum += A[i];
}
return ((A.length+1)*(A.length+2))/2 - sum;
}
User test case 1:
[4, 2, 3, 5, 1, 6, 8, 9]
User test case 2:
[1]
User test case 3:
[]
User test case 4:
[1, 2]
User test case 5:
[2, 3]
User test case 6:
[2]
Analysis
expand all
User tests
1.
0.172 s
OK
function result: 7
function result: 7
1.
0.152 s
OK
function result: 2
function result: 2
1.
0.160 s
OK
function result: 1
function result: 1
1.
0.160 s
OK
function result: 3
function result: 3
1.
0.158 s
OK
function result: 1
function result: 1
1.
0.161 s
OK
function result: 1
function result: 1
Code: 09:29: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) {
// write your code in JavaScript (Node.js 4.0.0)
var sum = 0;
for(var i = 0; i < A.length; i++){
sum += A[i];
}
return ((A.length+1)*(A.length+2))/2 - sum;
}
User test case 1:
[4, 2, 3, 5, 1, 6, 8, 9]
User test case 2:
[1]
User test case 3:
[]
User test case 4:
[1, 2]
User test case 5:
[2, 3]
User test case 6:
[2]
User test case 7:
[1, 2, 3, 4, 5]
Analysis
expand all
User tests
1.
0.158 s
OK
function result: 7
function result: 7
1.
0.159 s
OK
function result: 2
function result: 2
1.
0.160 s
OK
function result: 1
function result: 1
1.
0.163 s
OK
function result: 3
function result: 3
1.
0.158 s
OK
function result: 1
function result: 1
1.
0.158 s
OK
function result: 1
function result: 1
1.
0.161 s
OK
function result: 6
function result: 6
Code: 09:29:41 UTC,
js,
verify,
result: Passed
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0)
var sum = 0;
for(var i = 0; i < A.length; i++){
sum += A[i];
}
return ((A.length+1)*(A.length+2))/2 - sum;
}
User test case 1:
[4, 2, 3, 5, 1, 6, 8, 9]
User test case 2:
[1]
User test case 3:
[]
User test case 4:
[1, 2]
User test case 5:
[2, 3]
User test case 6:
[2]
User test case 7:
[1, 2, 3, 4, 5]
Analysis
expand all
User tests
1.
0.159 s
OK
function result: 7
function result: 7
1.
0.159 s
OK
function result: 2
function result: 2
1.
0.160 s
OK
function result: 1
function result: 1
1.
0.160 s
OK
function result: 3
function result: 3
1.
0.157 s
OK
function result: 1
function result: 1
1.
0.159 s
OK
function result: 1
function result: 1
1.
0.159 s
OK
function result: 6
function result: 6
Code: 09:29:46 UTC,
js,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.157 s
OK
2.
0.155 s
OK
1.
0.162 s
OK
2.
0.160 s
OK
1.
0.157 s
OK
2.
0.161 s
OK
1.
0.160 s
OK
2.
0.162 s
OK
3.
0.161 s
OK
1.
0.162 s
OK
expand all
Performance tests
1.
0.174 s
OK
1.
0.174 s
OK
1.
0.216 s
OK
2.
0.192 s
OK
3.
0.193 s
OK
1.
0.214 s
OK
1.
0.199 s
OK