Tasks Details
easy
Find the missing element in a given permutation.
Task Score
100%
Correctness
100%
Performance
100%
An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2 A[1] = 3 A[2] = 1 A[3] = 5the function should return 4, as it is the missing element.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- the elements of A are all distinct;
- each element of array A is an integer within the range [1..(N + 1)].
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 15 minutes
Notes
not defined yet
Task timeline
Code: 13:36:13 UTC,
java,
autosave
Code: 13:37:09 UTC,
java,
autosave
Code: 13:37:39 UTC,
java,
autosave
Code: 13:41:51 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
ㅗㅁ노
return answer;
}
}
Code: 13:42:06 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
HashSet<Integer>
return answer;
}
}
Code: 13:42:31 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
HashSet<Integer> set = new HashSet<>();
return answer;
}
}
Code: 13:43:02 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
HashSet<Integer> set = new HashSet<>();
for ()
return answer;
}
}
Code: 13:43:18 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
HashSet<Integer> set = new HashSet<>();
for (int i : )
return answer;
}
}
Code: 13:43:29 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
HashSet<Integer> set = new HashSet<>();
for (int i=1)
return answer;
}
}
Code: 13:43:59 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2)
return answer;
}
}
Code: 13:44:14 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add()
}
return answer;
}
}
Code: 13:44:25 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
return answer;
}
}
Code: 13:44:38 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
return answer;
}
}
Code: 13:44:55 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
return answer;
}
}
Code: 13:45:06 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
return answer;
}
}
Code: 13:45:16 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
return answer;
}
}
Code: 13:45:27 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
return answer;
}
}
Code: 13:45:43 UTC,
java,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 0 expected 4
Code: 13:46:39 UTC,
java,
autosave
Code: 13:46:58 UTC,
java,
autosave
Code: 13:47:19 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
set.
}
return answer;
}
}
Code: 13:47:49 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
set.toArray()
}
return answer;
}
}
Code: 13:48:01 UTC,
java,
verify,
result: Failed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = set.toArray()[0];
}
return answer;
}
}
Analysis
Compile error
Solution.java:18: error: incompatible types: Object cannot be converted to int answer = set.toArray()[0]; ^ 1 error
Code: 13:48:14 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = set.toArray;
}
return answer;
}
}
Code: 13:48:25 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = set.toArray();
}
return answer;
}
}
Code: 13:48:45 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = ((Integer[]) set.toArray());
}
return answer;
}
}
Code: 13:48:58 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = ((Integer[]) set.toArray())[0];
}
return answer;
}
}
Code: 13:49:07 UTC,
java,
verify,
result: Failed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = ((Integer[]) set.toArray())[0];
} else {
answer = -1;
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.008 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; at Solution.solution(Solution.java:18) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Code: 13:49:38 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = (set.toArray())[0];
} else {
answer = -1;
}
return answer;
}
}
Code: 13:49:48 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = (int) (set.toArray())[0];
} else {
answer = -1;
}
return answer;
}
}
Analysis
Code: 13:50:00 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = (int) set.toArray()[0];
} else {
answer = -1;
}
return answer;
}
}
Analysis
Code: 13:50:08 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = (int) set.toArray()[0];
} else {
answer = -1;
}
return answer;
}
}
Analysis
Code: 13:50:13 UTC,
java,
final,
score: 
100
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer = 0;
int n = A.length;
HashSet<Integer> set = new HashSet<>();
for (int i=1; i<n+2; i++) {
set.add(i);
}
for (int num : A) {
set.remove(num);
}
if (set.size() == 1) {
answer = (int) set.toArray()[0];
} else {
answer = -1;
}
return answer;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.012 s
OK
2.
0.008 s
OK
1.
0.012 s
OK
2.
0.004 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.012 s
OK
2.
0.004 s
OK
3.
0.008 s
OK
1.
0.008 s
OK
expand all
Performance tests
1.
0.056 s
OK
1.
0.052 s
OK
1.
0.528 s
OK
2.
0.264 s
OK
3.
0.264 s
OK
1.
0.528 s
OK
1.
0.316 s
OK