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 7 minutes
Notes
not defined yet
Code: 02:29:22 UTC,
java,
verify,
result: Passed
// 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");
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length>1)
{
Set<Integer> hs=new HashSet<Integer>();
for(int i=0;i<A.length;i++)
{
if(A[i]>0)
{
hs.add(A[i]);
}
}
System.out.println("Hashset: "+hs+","+hs.size());
int num=1;
while (hs.contains(num))
{
num++;
}
return num;
}
return 0;
}
}
Analysis
expand all
Example tests
1.
1.337 s
OK
stdout:
Hashset: [1, 2, 3, 4, 6],5
Code: 02:30:05 UTC,
java,
verify,
result: Passed
// 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");
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length>1)
{
Set<Integer> hs=new HashSet<Integer>();
for(int i=0;i<A.length;i++)
{
if(A[i]>0)
{
hs.add(A[i]);
}
}
System.out.println("Hashset: "+hs+","+hs.size());
int num=1;
while (hs.contains(num))
{
num++;
}
return num;
}
return 0;
}
}
User test case 1:
[1, 3, 6, 4, 1, 2]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[-3, -2, 0, 1, 3, 4]
Analysis
expand all
Example tests
1.
0.656 s
OK
stdout:
Hashset: [1, 2, 3, 4, 6],5
expand all
User tests
1.
0.455 s
OK
function result: 5
function result: 5
stdout:
Hashset: [1, 2, 3, 4, 6],5
1.
1.055 s
OK
function result: 0
function result: 0
1.
1.456 s
OK
function result: 0
function result: 0
1.
1.469 s
OK
function result: 2
function result: 2
stdout:
Hashset: [1, 3, 4],3
Code: 02:30:57 UTC,
java,
verify,
result: Passed
// 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");
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length>=1)
{
Set<Integer> hs=new HashSet<Integer>();
for(int i=0;i<A.length;i++)
{
if(A[i]>0)
{
hs.add(A[i]);
}
}
System.out.println("Hashset: "+hs+","+hs.size());
int num=1;
while (hs.contains(num))
{
num++;
}
return num;
}
return 0;
}
}
User test case 1:
[1, 3, 6, 4, 1, 2]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[-3, -2, 0, 1, 3, 4]
Analysis
expand all
Example tests
1.
1.294 s
OK
stdout:
Hashset: [1, 2, 3, 4, 6],5
expand all
User tests
1.
0.594 s
OK
function result: 5
function result: 5
stdout:
Hashset: [1, 2, 3, 4, 6],5
1.
1.480 s
OK
function result: 2
function result: 2
stdout:
Hashset: [1],1
1.
1.472 s
OK
function result: 1
function result: 1
stdout:
Hashset: [2],1
1.
1.473 s
OK
function result: 2
function result: 2
stdout:
Hashset: [1, 3, 4],3
Code: 02:31:35 UTC,
java,
verify,
result: Passed
// 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");
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length>=1)
{
Set<Integer> hs=new HashSet<Integer>();
for(int i=0;i<A.length;i++)
{
if(A[i]>0)
{
hs.add(A[i]);
}
}
System.out.println("Hashset: "+hs+","+hs.size());
int num=1;
while (hs.contains(num))
{
num++;
}
return num;
}
return 0;
}
}
User test case 1:
[1, 3, 6, 4, 1, 2]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[-3, -2, 0, 1, 3, 4]
User test case 5:
[32]
Analysis
expand all
Example tests
1.
0.352 s
OK
stdout:
Hashset: [1, 2, 3, 4, 6],5
expand all
User tests
1.
0.355 s
OK
function result: 5
function result: 5
stdout:
Hashset: [1, 2, 3, 4, 6],5
1.
0.351 s
OK
function result: 2
function result: 2
stdout:
Hashset: [1],1
1.
0.353 s
OK
function result: 1
function result: 1
stdout:
Hashset: [2],1
1.
0.996 s
OK
function result: 2
function result: 2
stdout:
Hashset: [1, 3, 4],3
1.
0.499 s
OK
function result: 1
function result: 1
stdout:
Hashset: [32],1
Code: 02:32:23 UTC,
java,
verify,
result: Passed
// 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");
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length>=1)
{
Set<Integer> hs=new HashSet<Integer>();
for(int i=0;i<A.length;i++)
{
if(A[i]>0)
{
hs.add(A[i]);
}
}
//System.out.println("Hashset: "+hs);
int num=1;
while (hs.contains(num))
{
num++;
}
return num;
}
return 0;
}
}
User test case 1:
[1, 3, 6, 4, 1, 2]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[-3, -2, 0, 1, 3, 4]
User test case 5:
[32]
Analysis
Code: 02:32:40 UTC,
java,
verify,
result: Passed
// 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");
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length>=1)
{
Set<Integer> hs=new HashSet<Integer>();
for(int i=0;i<A.length;i++)
{
if(A[i]>0)
{
hs.add(A[i]);
}
}
//System.out.println("Hashset: "+hs);
int num=1;
while (hs.contains(num))
{
num++;
}
return num;
}
return 0;
}
}
User test case 1:
[1, 3, 6, 4, 1, 2]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[-3, -2, 0, 1, 3, 4]
User test case 5:
[32]
Analysis
Code: 02:32:50 UTC,
java,
final,
score: 
100
// 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");
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length>=1)
{
Set<Integer> hs=new HashSet<Integer>();
for(int i=0;i<A.length;i++)
{
if(A[i]>0)
{
hs.add(A[i]);
}
}
//System.out.println("Hashset: "+hs);
int num=1;
while (hs.contains(num))
{
num++;
}
return num;
}
return 0;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
1.416 s
OK
2.
1.419 s
OK
3.
1.427 s
OK
1.
1.420 s
OK
2.
1.418 s
OK
1.
1.440 s
OK
2.
1.409 s
OK
1.
1.446 s
OK
1.
1.431 s
OK
expand all
Performance tests
1.
1.472 s
OK
2.
1.431 s
OK
3.
1.497 s
OK
1.
1.817 s
OK
1.
1.924 s
OK
2.
1.866 s
OK
1.
1.649 s
OK