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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Total time used 3 minutes
Effective time used 3 minutes
Notes
not defined yet
Task timeline
Code: 17:17:43 UTC,
java,
autosave
Code: 17:17:58 UTC,
java,
autosave
// 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");
class Solution {
static int solution(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
// write your code in Java SE 8
}
}
Code: 17:18:12 UTC,
java,
autosave
// 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");
class Solution {
static int solution(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solution(a);
}
}
Code: 17:18: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");
class Solution {
static int solution(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solution(a);
}
}
Analysis
Compile error
Solution.java:30: error: method solution(int[]) is already defined in class Solution public int solution(int[] A) { ^ Solution.java:12: error: cannot find symbol HashMap<Integer, Boolean> dict = new HashMap<>(); ^ symbol: class HashMap location: class Solution Solution.java:12: error: cannot find symbol HashMap<Integer, Boolean> dict = new HashMap<>(); ^ symbol: class HashMap location: class Solution Solution.java:31: error: cannot find symbol solution(a); ^ symbol: variable a location: class Solution 4 errors
Code: 17:18:36 UTC,
java,
autosave
// 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");
class Solution {
static int solution(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solution(a);
}
}
Code: 17:18:41 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solution(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solution(a);
}
}
Analysis
Compile error
Solution.java:30: error: method solution(int[]) is already defined in class Solution public int solution(int[] A) { ^ Solution.java:31: error: cannot find symbol solution(a); ^ symbol: variable a location: class Solution 2 errors
Code: 17:18:56 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solution(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solution(A);
}
}
Analysis
Compile error
Solution.java:30: error: method solution(int[]) is already defined in class Solution public int solution(int[] A) { ^ 1 error
Code: 17:19:09 UTC,
java,
autosave
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solv(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solution(A);
}
}
Code: 17:19:16 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solve(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solve(A);
}
}
Analysis
Compile error
Solution.java:32: error: missing return statement } ^ 1 error
Code: 17:19:42 UTC,
java,
autosave
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solution(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solve(A);
}
}
Code: 17:19:45 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solve(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
solve(A);
}
}
Analysis
Compile error
Solution.java:32: error: missing return statement } ^ 1 error
Code: 17:19:55 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solve(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
return solve(A);
}
}
Analysis
Code: 17:20:04 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solve(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
return solve(A);
}
}
Analysis
Code: 17:20:09 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
import java.util.HashMap;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static int solve(int[] A) {
int max = -1;
HashMap<Integer, Boolean> dict = new HashMap<>();
for(int a : A) {
if(dict.get(a) == null) {
dict.put(a, Boolean.TRUE);
}
if(max<a) {
max = a;
}
}
for(int i = 1; i<max; i++) {
if(dict.get(i) == null) {
return i;
}
}
return max>0 ? max+1 : 1;
}
public int solution(int[] A) {
return solve(A);
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
expand all
Performance tests
1.
0.036 s
OK
2.
0.028 s
OK
3.
0.056 s
OK
1.
0.424 s
OK
1.
0.576 s
OK
2.
0.520 s
OK
1.
0.268 s
OK