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(int A[], int N);
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 2 minutes
Notes
not defined yet
Task timeline
Code: 03:09:43 UTC,
java,
autosave
Code: 03:10:05 UTC,
c,
autosave
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, sum = 0, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
sum += A[i];
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Code: 03:10:15 UTC,
c,
autosave
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Code: 03:10:25 UTC,
c,
autosave
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 < A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Code: 03:10:36 UTC,
c,
autosave
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Code: 03:10:46 UTC,
c,
autosave
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Code: 03:10:49 UTC,
c,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Analysis
Compile error
func.c: In function 'solution': func.c:9:19: error: 'lt' undeclared (first use in this function) for(i = 0; i < N; i++){ ^~ func.c:9:19: note: each undeclared identifier is reported only once for each function it appears in func.c:9:5: warning: statement with no effect [-Wunused-value] for(i = 0; i < N; i++){ ^~~ func.c:9:24: error: expected ')' before ';' token for(i = 0; i < N; i++){ ^
Code: 03:10:59 UTC,
c,
autosave
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Code: 03:11:02 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Analysis
Code: 03:11:12 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 1;
}
Analysis
Code: 03:11:14 UTC,
c,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int i, max = 0;
int arr[1000001];
for(i = 0; i < N; i++){
if(0 > A[i]) continue;
if(arr[A[i]] != A[i]){
arr[A[i]] = A[i];
if(max < A[i]) max = A[i];
}
}
if(max == 0)
return 1;
for(i = 1; i <= max; i++){
if(arr[i] != i)
return i;
}
return max + 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.008 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.008 s
OK