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
Time spent on task 17 minutes
Notes
not defined yet
Task timeline
Code: 11:17:32 UTC,
java,
autosave
Code: 11:27:16 UTC,
c,
autosave
Code: 11:27:46 UTC,
c,
autosave
Code: 11:27:57 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int sum, quo;
sum = A[0] + A[N];
quo /= (N+1);
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
}
Code: 11:28:25 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo /= (N+1);
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
}
Code: 11:28:36 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo /= (N+1);
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
}
Code: 11:29:06 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo /= (N+1);
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
for(i = 0; i =< N; i++)
sum += A[i];
return
}
Code: 11:29:19 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo /= (N+1);
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
for(i = 0; i =< N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
Compile error
func.c: In function 'solution': func.c:17:19: error: expected expression before '<' token for(i = 0; i =< N; i++) ^
Code: 11:29:26 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo /= (N+1);
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got -9 expected 4
Code: 11:29:39 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo = (N+1);
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:29:48 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo = (N+1)/2;
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got -6 expected 4
Code: 11:30:09 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo = (N+1)/2;
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum)
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:30:11 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = A[0] + A[N];
quo = (N+1)/2;
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got -6 expected 4
stdout:
5
Code: 11:30:39 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + A[N];
quo = (N+1)/2;
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:30:45 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + N+1;
quo = (N+1)/2;
if((N+1) %2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got 2 expected 4
stdout:
13
Code: 11:31:07 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + N+1;
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:31:23 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N+1);
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:31:44 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
printf("%d", N);
sum = 1 + (N+1);
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got 2 expected 4
stdout:
413
Code: 11:31:53 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N+1);
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo];
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:33:12 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N+1);
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + A[quo]
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:33:19 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N+1);
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + (quo+1)
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
Compile error
func.c: In function 'solution': func.c:17:5: error: expected ';' before 'printf' printf("%d", sum); ^~~~~~
Code: 11:33:26 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N+1);
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + (quo+1);
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
Code: 11:33:33 UTC,
c,
autosave
// 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N+1);
quo = (N+1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + (quo + 1);
printf("%d", sum);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Code: 11:33:42 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N + 1);
quo = (N + 1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + (quo + 1);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis
Code: 11:33:44 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 (gcc 6.2.0)
int i, sum, quo, arr_sum = 0;
sum = 1 + (N + 1);
quo = (N + 1)/2;
if((N+1) % 2 == 0)
sum *= quo;
else
sum = (sum * quo) + (quo + 1);
for(i = 0; i <= N; i++)
arr_sum += A[i];
return sum - arr_sum;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
expand all
Performance tests
1.
0.001 s
OK
1.
0.001 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK