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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C
Time spent on task 6 minutes
Notes
not defined yet
Code: 12:31:12 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int B[N] = {0};
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Compile error
Solution.java:7: error: class, interface, or enum expected int solution(int A[], int N) { ^ Solution.java:11: error: class, interface, or enum expected for (int i = 0 ; i<N ;i++ ) ^ Solution.java:11: error: class, interface, or enum expected for (int i = 0 ; i<N ;i++ ) ^ Solution.java:11: error: class, interface, or enum expected for (int i = 0 ; i<N ;i++ ) ^ Solution.java:16: error: class, interface, or enum expected } ^ Solution.java:18: error: class, interface, or enum expected for (int i = 0 ; i<N ;i++ ) ^ Solution.java:18: error: class, interface, or enum expected for (int i = 0 ; i<N ;i++ ) ^ Solution.java:23: error: class, interface, or enum expected } ^ Solution.java:27: error: class, interface, or enum expected } ^ 9 errors
Code: 12:31:34 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 B[N] = {0};
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Compile error
func.c: In function 'solution': func.c:6:5: error: variable-sized object may not be initialized int B[N] = {0}; ^~~ func.c:6:17: warning: excess elements in array initializer int B[N] = {0}; ^ func.c:6:17: note: (near initialization for 'B')
Code: 12:32:39 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 B[N];
B= {0};
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Compile error
func.c: In function 'solution': func.c:7:8: error: expected expression before '{' token B= {0}; ^
Code: 12:33:10 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 B[N] = {0};
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Compile error
func.c: In function 'solution': func.c:6:5: error: variable-sized object may not be initialized int B[N] = {0}; ^~~ func.c:6:17: warning: excess elements in array initializer int B[N] = {0}; ^ func.c:6:17: note: (near initialization for 'B')
Code: 12:34:09 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 B[N] = {0};
memset( B, 0, N*sizeof(int) );
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Compile error
func.c: In function 'solution': func.c:6:5: error: variable-sized object may not be initialized int B[N] = {0}; ^~~ func.c:6:17: warning: excess elements in array initializer int B[N] = {0}; ^ func.c:6:17: note: (near initialization for 'B') func.c:8:5: warning: implicit declaration of function 'memset' [-Wimplicit-function-declaration] memset( B, 0, N*sizeof(int) ); ^~~~~~ func.c:8:5: warning: incompatible implicit declaration of built-in function 'memset' func.c:8:5: note: include '<string.h>' or provide a declaration of 'memset'
Code: 12:34:22 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 B[N];
memset( B, 0, N*sizeof(int) );
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Code: 12:35:15 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
#include <string.h>
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int B[N];
memset( B, 0, N*sizeof(int) );
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Code: 12:36:31 UTC,
c,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
#include <string.h>
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int B[N];
memset( B, 0, N*sizeof(int) );
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis
Code: 12:36:35 UTC,
c,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
#include <string.h>
int solution(int A[], int N) {
// write your code in C99 (gcc 6.2.0)
int B[N];
memset( B, 0, N*sizeof(int) );
for (int i = 0 ; i<N ;i++ )
{
if (A[i]>0 && A[i]-1<N)
{
B[A[i]-1] =1;
}
}
for (int i = 0 ; i<N ;i++ )
{
if ( B[i] == 0)
{
return i+1;
}
}
return N+1;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.008 s
OK
2.
0.009 s
OK
3.
0.008 s
OK
4.
0.008 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
3.
0.008 s
OK
1.
0.009 s
OK
2.
0.008 s
OK
1.
0.009 s
OK
2.
0.009 s
OK
1.
0.008 s
OK
expand all
Performance tests
1.
0.009 s
OK
2.
0.009 s
OK
3.
0.010 s
OK
1.
0.019 s
OK
1.
0.022 s
OK
2.
0.022 s
OK
1.
0.016 s
OK