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 7 minutes
Notes
not defined yet
Code: 06:32:56 UTC,
java,
autosave
Code: 06:33:01 UTC,
cpp,
autosave
Code: 06:36:40 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<A.size(); i++){
if(number[])
}
}
Code: 06:36:55 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<number.size(); i++){
if(number[i])
}
}
Code: 06:37:07 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<number.size(); i++){
if(!number[i]) return i;
}
}
Code: 06:37:26 UTC,
cpp,
verify,
result: Failed
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<number.size(); i++){
if(!number[i]) return i;
}
}
Analysis
Compile error
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:9:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int i = 0; i<A.size(); i++){ ~^~~~~~~~~ func.cpp:16:29: error: request for member 'size' in 'number', which is of non-class type 'bool [1000001]' for(int i = 1; i<number.size(); i++){ ^~~~
Code: 06:37:41 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000001; i++){
if(!number[i]) return i;
}
}
Code: 06:37:43 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)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000001; i++){
if(!number[i]) return i;
}
}
Analysis
Code: 06:38:12 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000001; i++){
if(!number[i]) return i;
}
}
Code: 06:38:13 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)
bool number[1000001] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000001; i++){
if(!number[i]) return i;
}
}
Analysis
User test case 1:
[1000000]
Code: 06:38:43 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000002] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000001; i++){
if(!number[i]) return i;
}
}
Code: 06:38:53 UTC,
cpp,
autosave
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000002] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000002; i++){
if(!number[i]) return i;
}
}
Code: 06:39:03 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)
bool number[1000002] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000002; i++){
if(!number[i]) return i;
}
}
Analysis
User test case 1:
[1000000]
Code: 06:39:15 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)
bool number[1000002] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000002; i++){
if(!number[i]) return i;
}
}
Analysis
User test case 1:
[1000000]
Code: 06:39:19 UTC,
cpp,
final,
score: 
100
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
bool number[1000002] = {false,};
for(int i = 0; i<A.size(); i++){
if(A[i] >= 1){
number[A[i]] = true;
}
}
//검사
for(int i = 1; i<1000002; i++){
if(!number[i]) return i;
}
}
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.001 s
OK
1.
0.004 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.004 s
OK