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:
int solution(int A[], int N);
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 C
Total time used 7 minutes
Effective time used 7 minutes
Notes
not defined yet
Task timeline
Code: 00:27:38 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long i;
long ordered[N+1];
for (i=1;i<N + 1;i++){
ordered[i] = 0;
}
ordered[0] = 1;
for (i=0;i<N;i++){
ordered[A[i]] = 1;
}
for (i=0;i<N;i++){
if (ordered[i] == 0)
return i;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[3, 2]
User test case 4:
[5, 3, 2, 1]
Analysis
expand all
User tests
1.
0.006 s
OK
function result: 0
function result: 0
1.
0.006 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 4
function result: 4
Code: 00:30:14 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long i;
long ordered[N+1];
for (i=1;i<N + 1;i++){
ordered[i] = 0;
}
for (i=0;i<N;i++){
ordered[A[i]] = 1;
}
for (i=1;i<N + 1;i++){
if (ordered[i] == 0)
return i;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[3, 2]
User test case 4:
[5, 3, 2, 1]
Analysis
expand all
User tests
1.
0.006 s
OK
function result: 0
function result: 0
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 4
function result: 4
Code: 00:31:08 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
if (N == 0) return 1;
long i;
long ordered[N+1];
for (i=1;i<N + 1;i++){
ordered[i] = 0;
}
for (i=0;i<N;i++){
ordered[A[i]] = 1;
}
for (i=1;i<N + 1;i++){
if (ordered[i] == 0)
return i;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[3, 2]
User test case 4:
[5, 3, 2, 1]
Analysis
expand all
User tests
1.
0.006 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 4
function result: 4
Code: 00:31:15 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
if (N == 0) return 1;
long i;
long ordered[N+1];
for (i=1;i<N + 1;i++){
ordered[i] = 0;
}
for (i=0;i<N;i++){
ordered[A[i]] = 1;
}
for (i=1;i<N + 1;i++){
if (ordered[i] == 0)
return i;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[3, 2]
User test case 4:
[5, 3, 2, 1]
Analysis
expand all
User tests
1.
0.006 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 4
function result: 4
Code: 00:31:18 UTC,
c,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
if (N == 0) return 1;
long i;
long ordered[N+1];
for (i=1;i<N + 1;i++){
ordered[i] = 0;
}
for (i=0;i<N;i++){
ordered[A[i]] = 1;
}
for (i=1;i<N + 1;i++){
if (ordered[i] == 0)
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.006 s
OK
2.
0.006 s
OK
1.
0.006 s
OK
2.
0.006 s
OK
1.
0.006 s
OK
2.
0.006 s
OK
1.
0.006 s
OK
2.
0.006 s
OK
3.
0.006 s
OK
1.
0.006 s
OK
expand all
Performance tests
1.
0.007 s
OK
1.
0.007 s
OK
1.
0.020 s
OK
2.
0.013 s
OK
3.
0.013 s
OK
1.
0.020 s
OK
1.
0.014 s
OK