Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
100%
Correctness
100%
Performance
100%
This is a demo task.
Write a function:
int solution(vector<int> &A);
that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−1,000,000..1,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 25 minutes
Notes
not defined yet
Code: 06:02:13 UTC,
java,
autosave
Code: 06:02:18 UTC,
cpp,
autosave
Code: 06:02:49 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int,>
for (int i = 0; i < A.size(); i++)
}
Code: 06:03:10 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
memo_[A[i]] = i;
}
Code: 06:04:14 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
memo_[A[i]] = i;
}
Code: 06:04:44 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
memo_[A[i]] = i;
for (int i = 1; i < memo_.size(); i++) {
if (memo_.find(i) == memo_)
}
}
Code: 06:05:02 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
memo_[A[i]] = i;
for (int i = 1; i < memo_.size(); i++) {
if (memo_.find(i) == memo_.end()) {
return i;
}
}
return memo_.size() + 1;
}
Analysis
Code: 06:06:32 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
memo_[A[i]] = i;
for (int i = 1; i < memo_.size(); i++) {
if (memo_.find(i) == memo_.end()) {
return i;
}
}
return memo_.size() + 1;
}
Code: 06:06:44 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
if(A[i] > 0) memo_[A[i]] = i;
for (int i = 1; i < memo_.size(); i++) {
if (memo_.find(i) == memo_.end()) {
return i;
}
}
return memo_.size() + 1;
}
Analysis
Code: 06:17:23 UTC,
cpp,
autosave
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
if(A[i] > 0) memo_[A[i]] = i;
for (int i = 1; i < memo_.size() + 1; i++) {
if (memo_.find(i) == memo_.end()) {
return i;
}
}
return memo_.size() + 1;
}
Code: 06:17:26 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
if(A[i] > 0) memo_[A[i]] = i;
for (int i = 1; i < memo_.size() + 1; i++) {
if (memo_.find(i) == memo_.end()) {
return i;
}
}
return memo_.size() + 1;
}
Analysis
Code: 06:26:22 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
if(A[i] > 0) memo_[A[i]] = i;
for (int i = 1; i < memo_.size() + 1; i++) {
if (memo_.find(i) == memo_.end()) {
return i;
}
}
return memo_.size() + 1;
}
Analysis
Code: 06:26:25 UTC,
cpp,
final,
score: 
100
// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// 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)
unordered_map<int, int> memo_;
for (int i = 0; i < A.size(); i++)
if(A[i] > 0) memo_[A[i]] = i;
for (int i = 1; i < memo_.size() + 1; i++) {
if (memo_.find(i) == memo_.end()) {
return i;
}
}
return memo_.size() + 1;
}
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
3.
0.001 s
OK
4.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
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
expand all
Performance tests
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.004 s
OK
1.
0.016 s
OK
1.
0.020 s
OK
2.
0.020 s
OK
1.
0.008 s
OK