Tasks Details
easy
Find the missing element in a given permutation.
Task Score
50%
Correctness
20%
Performance
80%
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 9 minutes
Notes
not defined yet
Code: 08:41:34 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// 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) {
// write your code in Java SE 8
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
}
}
Analysis
Compile error
Solution.java:10: error: cannot find symbol Arrays.sort(A); ^ symbol: variable Arrays location: class Solution 1 error
Code: 08:42:34 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
import java.util.Arrays;
// 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) {
// write your code in Java SE 8
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
}
}
Analysis
Compile error
Solution.java:17: error: missing return statement } ^ 1 error
Code: 08:42:48 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
import java.util.Arrays;
// 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) {
// write your code in Java SE 8
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
return 1;
}
}
User test case 1:
[2, 3, 1, 5]
Analysis
Code: 08:43:05 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
import java.util.Arrays;
// 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) {
// write your code in Java SE 8
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
return 1;
}
}
User test case 1:
[2, 3, 1, 5]
User test case 2:
[]
Analysis
Code: 08:45:12 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
import java.util.Arrays;
// 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) {
// write your code in Java SE 8
if(A.length == 0){
throw new RuntimeException("Array empty !");
}
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
throw
return 1;
}
}
Analysis
Compile error
Solution.java:22: error: illegal start of expression return 1; ^ Solution.java:22: error: not a statement return 1; ^ 2 errors
Code: 08:45:42 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
import java.util.Arrays;
// 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) {
// write your code in Java SE 8
if(A.length == 0){
throw new RuntimeException("Array empty !");
}
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
throw new RuntimeException("Missing Element Not Found !");
}
}
User test case 1:
[2, 3, 1, 5]
User test case 2:
[]
Analysis
expand all
User tests
1.
1.312 s
OK
function result: 4
function result: 4
1.
1.296 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Exception in thread "main" java.lang.RuntimeException: Array empty ! at Solution.solution(Solution.java:12) at wrapper.run(wrapper.java:29) at wrapper.main(wrapper.java:22)
Code: 08:46:41 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
import java.util.Arrays;
// 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) {
// write your code in Java SE 8
if(A.length == 0){
//throw new RuntimeException("Array empty !");
return 0;
}
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
//throw new RuntimeException("Missing Element Not Found !");
return 0;
}
}
User test case 1:
[2, 3, 1, 5]
User test case 2:
[]
User test case 3:
[2, 3, 1, 4]
Analysis
expand all
User tests
1.
1.304 s
OK
function result: 4
function result: 4
1.
1.356 s
OK
function result: 0
function result: 0
1.
1.348 s
OK
function result: 0
function result: 0
Code: 08:46:53 UTC,
java,
final,
score: 
50
// you can also use imports, for example:
// import java.util.*;
import java.util.Arrays;
// 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) {
// write your code in Java SE 8
if(A.length == 0){
//throw new RuntimeException("Array empty !");
return 0;
}
Arrays.sort(A);
for(int i= 0; i<A.length; i++){
if(A[i]!= i+1){
return i+1;
}
}
//throw new RuntimeException("Missing Element Not Found !");
return 0;
}
}
Analysis summary
The following issues have been detected: wrong answers.
Analysis
expand all
Correctness tests
1.
1.348 s
WRONG ANSWER,
got 0 expected 1
1.
1.364 s
WRONG ANSWER,
got 0 expected 6
1.
1.352 s
WRONG ANSWER,
got 0 expected 2
1.
1.348 s
WRONG ANSWER,
got 0 expected 3
1.
1.320 s
OK
expand all
Performance tests
1.
1.376 s
OK
1.
1.368 s
OK
1.
1.616 s
WRONG ANSWER,
got 0 expected 100001
1.
1.680 s
OK
1.
1.588 s
OK