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:
class Solution { public int solution(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 Java 8
Time spent on task 4 minutes
Notes
not defined yet
Code: 07:04:05 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.HashSet;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<Integer> temp = new HashSet<>(); // to add just positives
int max = 1;
for (int i =0; i < A.length; ++i) {
int current = A[i];
if (current < 0 ) {
continue;
} else {
temp.add(current);
if( max <= current) {
max = current;
}
}
}
max = Math.max(max, A.length); // taking into account arrays with random elements
for(int i = 1; i <= max ; ++i) {
boolean contains = temp.contains(i);
if(!contains) {
return i;
}
}
return A.length + 1; // array is complete
}
}
Analysis
Code: 07:05:22 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.HashSet;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<Integer> positives = new HashSet<>(); // to add just positives
int max = 1, length = A.length;
for (int i =0; i < length; ++i) {
int current = A[i];
if (current < 0 ) {
continue;
} else {
positives.add(current);
if( max <= current) {
max = current;
}
}
}
max = Math.max(max, length); // taking into account arrays with random elements
for(int i = 1; i <= max ; ++i) {
if(!positives.contains(i)) {
return i;
}
}
return length + 1; // array is complete
}
}
Analysis
Code: 07:06:58 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.HashSet;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
// (-2,1) = 2
// (2) = 1
// (1) = 2
// (1,2,4) = 3
// (1,2,3) = 4
public int solution(int[] A) {
HashSet<Integer> positives = new HashSet<>(); // to add just positives
int max = 1, length = A.length;
for (int i =0; i < length; ++i) {
int current = A[i];
if (current < 0 ) {
continue;
} else {
positives.add(current);
if( max <= current) {
max = current;
}
}
}
max = Math.max(max, length); // taking into account arrays with random elements
for(int i = 1; i <= max ; ++i) {
if(!positives.contains(i)) {
return i;
}
}
return length + 1; // array is complete
}
}
Analysis
Code: 07:07:09 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.HashSet;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
// (-2,1) = 2
// (2) = 1
// (1) = 2
// (1,2,4) = 3
// (1,2,3) = 4
public int solution(int[] A) {
HashSet<Integer> positives = new HashSet<>(); // to add just positives
int max = 1, length = A.length;
for (int i =0; i < length; ++i) {
int current = A[i];
if (current < 0 ) {
continue;
} else {
positives.add(current);
if( max <= current) {
max = current;
}
}
}
max = Math.max(max, length); // taking into account arrays with random elements
for(int i = 1; i <= max ; ++i) {
if(!positives.contains(i)) {
return i;
}
}
return length + 1; // array is complete
}
}
Analysis
Code: 07:07:14 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
import java.util.HashSet;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
// (-2,1) = 2
// (2) = 1
// (1) = 2
// (1,2,4) = 3
// (1,2,3) = 4
public int solution(int[] A) {
HashSet<Integer> positives = new HashSet<>(); // to add just positives
int max = 1, length = A.length;
for (int i =0; i < length; ++i) {
int current = A[i];
if (current < 0 ) {
continue;
} else {
positives.add(current);
if( max <= current) {
max = current;
}
}
}
max = Math.max(max, length); // taking into account arrays with random elements
for(int i = 1; i <= max ; ++i) {
if(!positives.contains(i)) {
return i;
}
}
return length + 1; // array is complete
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
1.338 s
OK
2.
1.335 s
OK
3.
1.347 s
OK
1.
1.338 s
OK
2.
1.343 s
OK
1.
1.327 s
OK
2.
1.338 s
OK
1.
1.343 s
OK
1.
1.353 s
OK
expand all
Performance tests
1.
1.377 s
OK
2.
1.352 s
OK
3.
1.404 s
OK
1.
1.760 s
OK
1.
1.830 s
OK
2.
1.800 s
OK
1.
1.603 s
OK