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(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 5 minutes
Notes
not defined yet
Code: 09:18:04 UTC,
java,
autosave
Code: 09:18:55 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)
if (A.empty())
return 1;
int ans = 0;
for (int i = 0; i < A.size(); i++) {
ans = (ans ^ A[i] ^ [i+1]);
}
return ans;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:13:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (int i = 0; i < A.size(); i++) { ~~^~~~~~~~~~ func.cpp:14:31: error: expected ',' before '+' token ans = (ans ^ A[i] ^ [i+1]); ^ func.cpp:14:31: error: expected identifier before '+' token func.cpp: In lambda function: func.cpp:14:34: error: expected '{' before ')' token ans = (ans ^ A[i] ^ [i+1]); ^ func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:14:27: error: no match for 'operator^' (operand types are 'int' and 'solution(std::vector<int>&)::<lambda()>') ans = (ans ^ A[i] ^ [i+1]); In file included from /opt/lang/gcc/include/c++/6.2.0/ios:42:0, from /opt/lang/gcc/include/c++/6.2.0/ostream:38, from /opt/lang/gcc/include/c++/6.2.0/iostream:39, from solution.cpp:4: /opt/lang/gcc/include/c++/6.2.0/bits/ios_base.h:91:3: note: candidate: constexpr std::_Ios_Fmtflags std::operator^(std::_Ios_Fmtflags, std::_Ios_Fmtflags) operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b) ^~~~~~~~ /opt/lang/gcc/include/c++/6.2.0/bits/ios_base.h:91:3: note: no known conversion for argument 2 from 'solution(std::vector<int>&)::<lambda()>' to 'std::_Ios_Fmtflags' /opt/lang/gcc/include/c++/6.2.0/bits/ios_base.h:133:3: note: candidate: constexpr std::_Ios_Openmode std::operator^(std::_Ios_Openmode, std::_Ios_Openmode) operator^(_Ios_Openmode __a, _Ios_Openmode __b) ^~~~~~~~ /opt/lang/gcc/include/c++/6.2.0/bits/ios_base.h:133:3: note: no known conversion for argument 2 from 'solution(std::vector<int>&)::<lambda()>' to 'std::_Ios_Openmode' /opt/lang/gcc/include/c++/6.2.0/bits/ios_base.h:173:3: note: candidate: constexpr std::_Ios_Iostate std::operator^(std::_Ios_Iostate, std::_Ios_Iostate) operator^(_Ios_Iostate __a, _Ios_Iostate __b) ^~~~~~~~ /opt/lang
Code: 09:19:11 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)
if (A.empty())
return 1;
int ans = 0;
for (int i = 0; i < A.size(); i++) {
ans = (ans ^ A[i] ^ (i+1));
}
return ans;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got 1 expected 4
Code: 09:22:39 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)
if (A.empty())
return 1;
int ans = A.size() + 1;
for (int i = 0; i < A.size(); i++) {
ans = (ans ^ A[i] ^ (i+1));
}
return ans;
}
Analysis
Code: 09:22:48 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)
if (A.empty())
return 1;
int ans = A.size() + 1;
for (int i = 0; i < A.size(); i++) {
ans = (ans ^ A[i] ^ (i+1));
}
return ans;
}
Analysis
Code: 09:22:51 UTC,
cpp,
final,
score: 
100
// 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)
if (A.empty())
return 1;
int ans = A.size() + 1;
for (int i = 0; i < A.size(); i++) {
ans = (ans ^ A[i] ^ (i+1));
}
return ans;
}
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.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.008 s
OK
1.
0.004 s
OK