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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 57 minutes
Notes
not defined yet
Task timeline
Code: 04:22:09 UTC,
java,
autosave
Code: 04:22:52 UTC,
cpp,
autosave
Code: 04:23:11 UTC,
cpp,
autosave
Code: 04:23:30 UTC,
cpp,
autosave
Code: 04:23:49 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)
int len = A.size();
if(len == 1){
if(A[i] <=0){
return 1;
}
}
}
Code: 04:23:59 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)
int len = A.size();
if(len == 1){
if(A[i] <=0){
return 1;
}else{
return 2;
}
}
}
Code: 04:24:30 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)
int len = A.size();
if(len == 1){
if(A[i] <=0){
return 1;
}else if(A[i] == 1){
return 2;
}e;se{
return
}
}
}
Code: 04:24:58 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)
int len = A.size();
if(len == 1){
if((A[i] <=0) || A[i]>1){
return 1;
}else if(A[i] == 1){
return 2;
}
}
}
Code: 04:25:10 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)
int len = A.size();
// 원소의 갯수 1개일때
if(len == 1){
if((A[i] <=0) || A[i]>1){
return 1;
}else if(A[i] == 1){
return 2;
}
}
}
Code: 04:25:20 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)
int len = A.size();
// 원소의 갯수 1개일 때
if(len == 1){
if((A[i] <=0) || A[i]>1){
return 1;
}else if(A[i] == 1){
return 2;
}
}
}
Code: 04:25:48 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)
int len = A.size();
// 원소의 갯수 1개일 때
if(len == 1){
if((A[i] <=0) || A[i]>1){
return 1;
}else if(A[i] == 1){
return 2;
}
}
// sort
sort(A.begin(), A.end());
//
}
Code: 04:27:00 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)
int len = A.size();
// 원소의 갯수 1개일 때
if(len == 1){
if((A[i] <=0) || A[i]>1){
return 1;
}else if(A[i] == 1){
return 2;
}
}
// sort
sort(A.begin(), A.end());
//
}
Code: 04:27:20 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)
int len = A.size();
// 원소의 갯수 1개일 때
if(len == 1){
if((A[i] <=0) || A[i]>1){
return 1;
}else if(A[i] == 1){
return 2;
}
}
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == 1){
}
}
}
Code: 05:16:16 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)
int len = A.size();
// 원소의 갯수 1개일 때
// if(len == 1){
// if((A[i] <=0) || A[i]>1){
// return 1;
// }else if(A[i] == 1){
// return 2;
// }
// }
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == 1){
}
}
}
Code: 05:16:25 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)
int len = A.size();
int minVal;
// 원소의 갯수 1개일 때
// if(len == 1){
// if((A[i] <=0) || A[i]>1){
// return 1;
// }else if(A[i] == 1){
// return 2;
// }
// }
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == 1){
}
}
}
Code: 05:16:43 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)
int len = A.size();
int minVal = 1;
// 원소의 갯수 1개일 때
// if(len == 1){
// if((A[i] <=0) || A[i]>1){
// return 1;
// }else if(A[i] == 1){
// return 2;
// }
// }
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
}
Code: 05:17:01 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)
int len = A.size();
int minVal = 1;
// 원소의 갯수 1개일 때
// if(len == 1){
// if((A[i] <=0) || A[i]>1){
// return 1;
// }else if(A[i] == 1){
// return 2;
// }
// }
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
return minVal;
}
Code: 05:17:05 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)
int len = A.size();
int minVal = 1;
// 원소의 갯수 1개일 때
// if(len == 1){
// if((A[i] <=0) || A[i]>1){
// return 1;
// }else if(A[i] == 1){
// return 2;
// }
// }
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
return minVal;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:22:28: error: 'sort' was not declared in this scope sort(A.begin(), A.end()); ^
Code: 05:17:30 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)
int len = A.size();
int minVal = 1;
// 원소의 갯수 1개일 때
// if(len == 1){
// if((A[i] <=0) || A[i]>1){
// return 1;
// }else if(A[i] == 1){
// return 2;
// }
// }
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
return minVal;
}
Code: 05:17:39 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int minVal = 1;
// 원소의 갯수 1개일 때
// if(len == 1){
// if((A[i] <=0) || A[i]>1){
// return 1;
// }else if(A[i] == 1){
// return 2;
// }
// }
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
return minVal;
}
Analysis
Code: 05:18:00 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int minVal = 1;
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
return minVal;
}
Code: 05:18:06 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int minVal = 1;
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
return minVal;
}
Analysis
Code: 05:18:12 UTC,
cpp,
final,
score: 
100
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int minVal = 1;
// sort
sort(A.begin(), A.end());
//
for(int i=0; i<len; i++){
if(A[i] == minVal){
minVal++;
}
}
return minVal;
}
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.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.008 s
OK