Tasks Details
easy
Find the missing element in a given permutation.
Task Score
20%
Correctness
40%
Performance
0%
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
Time spent on task 26 minutes
Notes
not defined yet
Task timeline
Code: 11:52:08 UTC,
c,
verify,
result: Failed
// 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
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0, i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem = N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
Analysis
Compile error
func.c: In function 'solution': func.c:11:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token for(int i = 0, i < N; i++) ^ func.c:11:30: error: expected ';' before ')' token for(int i = 0, i < N; i++) ^ func.c:15:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses] else if (elem = N + 1) { nPlusOnePosition = elem; } ^
Code: 11:52:39 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
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem = N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
Analysis
Code: 11:53:04 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
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem = (N + 1)) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
Analysis
Code: 11:53:25 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
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
Analysis
Code: 11:54:55 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
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
Analysis
expand all
User tests
1.
0.007 s
OK
function result: 2
function result: 2
1.
0.007 s
OK
function result: 1
function result: 1
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.007 s
OK
function result: 3
function result: 3
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 4
function result: 4
Code: 11:55:22 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
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
User test case 7:
[3, 1]
Analysis
expand all
User tests
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: 2
function result: 2
1.
0.006 s
OK
function result: 3
function result: 3
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 4
function result: 4
1.
0.006 s
OK
function result: 2
function result: 2
Code: 11:55:43 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
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
User test case 7:
[3, 1]
User test case 8:
[3, 2]
Analysis
expand all
User tests
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: 2
function result: 2
1.
0.006 s
OK
function result: 3
function result: 3
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 4
function result: 4
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 2
function result: 2
Code: 11:57:07 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
printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
User test case 7:
[3, 1]
User test case 8:
[3, 2]
Analysis
expand all
User tests
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 1
1.
0.006 s
OK
function result: 1
function result: 1
stdout:
N = 1
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2
1.
0.006 s
OK
function result: 3
function result: 3
stdout:
N = 2
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2
1.
0.006 s
OK
function result: 4
function result: 4
stdout:
N = 4
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2
Code: 11:57:54 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
printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
printf("nthPosition = %d\n", nthPosition);
printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
User test case 7:
[3, 1]
User test case 8:
[3, 2]
Analysis
expand all
Example tests
1.
0.006 s
OK
stdout:
N = 4 nthPosition = -1 nPlusOnePosition = 5
expand all
User tests
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 1 nthPosition = 1 nPlusOnePosition = -1
1.
0.006 s
OK
function result: 1
function result: 1
stdout:
N = 1 nthPosition = -1 nPlusOnePosition = 2
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2 nthPosition = -1 nPlusOnePosition = -1
1.
0.006 s
OK
function result: 3
function result: 3
stdout:
N = 2 nthPosition = 2 nPlusOnePosition = -1
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2 nthPosition = -1 nPlusOnePosition = -1
1.
0.006 s
OK
function result: 4
function result: 4
stdout:
N = 4 nthPosition = -1 nPlusOnePosition = 5
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2 nthPosition = -1 nPlusOnePosition = 3
1.
0.006 s
OK
function result: 2
function result: 2
stdout:
N = 2 nthPosition = -1 nPlusOnePosition = 3
Code: 12:01:54 UTC,
c,
verify,
result: Failed
// 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
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
A[elem] = elem;
i = elem;
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
User test case 7:
[3, 1]
User test case 8:
[3, 2]
Analysis
expand all
Example tests
1.
5.000 s
TIMEOUT ERROR,
running time: >5.00 sec., time limit: 5.00 sec.
expand all
User tests
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 1
function result: 1
1.
5.000 s
TIMEOUT ERROR,
running time: >5.00 sec., time limit: 5.00 sec.
1.
0.006 s
OK
function result: 1
function result: 1
1.
5.000 s
TIMEOUT ERROR,
running time: >5.00 sec., time limit: 5.00 sec.
1.
5.000 s
TIMEOUT ERROR,
running time: >5.00 sec., time limit: 5.00 sec.
1.
5.000 s
TIMEOUT ERROR,
running time: >5.00 sec., time limit: 5.00 sec.
1.
0.006 s
OK
function result: 1
function result: 1
Code: 12:06:09 UTC,
c,
verify,
result: Passed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
// 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
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
if (A[elem] != elem)
{
A[elem] = elem;
i = elem;
}
else
{
i++;
}
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
User test case 7:
[3, 1]
User test case 8:
[3, 2]
Analysis
expand all
User tests
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: 2
function result: 2
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: 4
function result: 4
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 1
function result: 1
Code: 12:07:00 UTC,
c,
verify,
result: Passed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
// 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
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
if (A[elem] != elem)
{
A[elem] = elem;
i = elem;
}
else
{
i++;
}
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
User test case 1:
[1]
User test case 2:
[2]
User test case 3:
[1, 3]
User test case 4:
[2, 3]
User test case 5:
[1, 2]
User test case 6:
[2, 3, 1, 5]
User test case 7:
[3, 1]
User test case 8:
[3, 2]
Analysis
expand all
User tests
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: 2
function result: 2
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: 4
function result: 4
1.
0.006 s
OK
function result: 2
function result: 2
1.
0.006 s
OK
function result: 1
function result: 1
Code: 12:07:05 UTC,
c,
final,
score: 
20
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
// 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
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
if (A[elem] != elem)
{
A[elem] = elem;
i = elem;
}
else
{
i++;
}
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
Analysis summary
The following issues have been detected: wrong answers.
For example, for the input [] the solution returned a wrong answer (got 0 expected 1).
Analysis
expand all
Correctness tests
1.
0.006 s
WRONG ANSWER,
got 0 expected 1
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
WRONG ANSWER,
got 2 expected 3
2.
0.006 s
OK
3.
0.006 s
OK
1.
0.006 s
WRONG ANSWER,
got 8 expected 7
expand all
Performance tests
1.
0.007 s
WRONG ANSWER,
got 9999 expected 456
1.
0.007 s
WRONG ANSWER,
got 9999 expected 9998
1.
0.019 s
WRONG ANSWER,
got 100000 expected 100001
2.
0.013 s
WRONG ANSWER,
got 49999 expected 1
3.
0.013 s
WRONG ANSWER,
got 49998 expected 12345
1.
0.020 s
WRONG ANSWER,
got 100000 expected 76541
1.
0.014 s
WRONG ANSWER,
got 60049 expected 10001