Tasks Details
easy
Find the missing element in a given permutation.
Task Score
90%
Correctness
100%
Performance
80%
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(vector<int> &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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 7 minutes
Notes
not defined yet
Code: 17:11:49 UTC,
java,
autosave
Code: 17:12:00 UTC,
cpp,
autosave
Code: 17:13:27 UTC,
cpp,
autosave
Code: 17:13:37 UTC,
cpp,
autosave
Code: 17:14:02 UTC,
cpp,
autosave
Code: 17:14:32 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
int number[10]
}
Code: 17:15:03 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =)
}
Code: 17:15:33 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i =0; i<A.size(); i++){
}
}
Code: 17:15:57 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+; i++){
}
}
Code: 17:16:18 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i])
}
}
Code: 17:16:34 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return number[i];
}
return 0;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got 0 expected 4
Code: 17:17:03 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Code: 17:17:05 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Analysis
Code: 17:17:33 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Code: 17:17:35 UTC,
cpp,
verify,
result: Passed
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Analysis
Code: 17:17:59 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Code: 17:18:08 UTC,
cpp,
verify,
result: Passed
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Analysis
User test case 1:
[2, 3, 4, 5]
Code: 17:18:23 UTC,
cpp,
verify,
result: Passed
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Analysis
User test case 1:
[2, 3, 4, 5]
Code: 17:18:27 UTC,
cpp,
final,
score: 
90
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//10만개를 sort하면 최소 time이 약 O(10만*log(10만))
bool number[100001] = {false,};
for(int i =0; i<A.size(); i++){
number[A[i]] = true;
}
for(int i = 1; i<A.size()+2; i++){
if(!number[i]) return i;
}
return 0;
}
Analysis summary
The following issues have been detected: wrong answers.
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.008 s
WRONG ANSWER,
got 0 expected 100001
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.008 s
OK
1.
0.004 s
OK