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 21
Time spent on task 13 minutes
Notes
not defined yet
Code: 06:06:55 UTC,
java,
autosave
Code: 06:07:00 UTC,
java,
autosave
Code: 06:10:42 UTC,
java,
autosave
Code: 06:10:58 UTC,
java,
autosave
Code: 06:11:09 UTC,
java,
autosave
Code: 06:11:19 UTC,
java,
autosave
Code: 06:11:44 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x =
}
}
Code: 06:11:54 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
}
}
Code: 06:12: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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
1 2 3 5
}
}
Code: 06:12:35 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// 11 -
}
}
Code: 06:13:15 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
//
}
}
Code: 06:13:46 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
}
}
Code: 06:14: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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n =
}
}
Code: 06:14:19 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
for()
}
}
Code: 06:14: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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
for(int i:A){
}
}
}
Code: 06:15:08 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
for(int i:A){
}
}
}
Code: 06:15:39 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum =
for(int i:A){
all +=i;
}
all += n;
all -
}
}
Code: 06:16:00 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
for(int i:A){
all +=i;
sum += A[i];
}
all += n;
return all - sum;
}
}
Analysis
expand all
Example tests
1.
0.023 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 4 at Solution.solution(Solution.java:20) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Code: 06:16:47 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
for(int i:){
all +=i;
sum += A[i];
}
all += n;
return all - sum;
}
}
Code: 06:16:59 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
for(int i=0; i < n; i++){
all +=i;
sum += A[i];
}
all += n;
return all - sum;
}
}
Analysis
expand all
Example tests
1.
0.031 s
WRONG ANSWER,
got -1 expected 4
Code: 06:17: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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
for(int i=1; i < n; i++){
all +=i;
sum += A[i];
}
all += n;
return all - sum;
}
}
Code: 06:18:04 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
for(int i=1; i < n+1; i++){
all +=i;
sum += A[i-1];
}
all += n;
return all - sum;
}
}
Code: 06:18:05 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
for(int i=1; i < n+1; i++){
all +=i;
sum += A[i-1];
}
all += n;
return all - sum;
}
}
Analysis
expand all
Example tests
1.
0.022 s
WRONG ANSWER,
got 3 expected 4
Code: 06:18:30 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
// n=4
for(int i=0; i < n; i++){
all +=i;
sum += A[i-1];
}
all += n;
return all - sum;
}
}
Code: 06:18:42 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
// n=4
// 0,1,2,3
for(int i=0; i < n; i++){
all +=i+1;
sum += A[i-1];
}
all += n;
return all - sum;
}
}
Code: 06:18:52 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 {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
// n=4
// 0,1,2,3
for(int i=0; i < n; i++){
all +=i+1; // 1,2,3,4
sum += A[i-1];
}
all += n;
return all - sum;
}
}
Code: 06:19: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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
// n=4
// 0,1,2,3
for(int i=0; i < n; i++){
all +=i+1; // 1,2,3,4
sum += A[i-1];
}
all += n+1;
return all - sum;
}
}
Code: 06:19:10 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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
// n=4
// 0,1,2,3
for(int i=0; i < n; i++){
all +=i+1; // 1,2,3,4
sum += A[i];
}
all += n+1;
return all - sum;
}
}
Analysis
Code: 06:19:37 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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
// n=4
// 0,1,2,3
for(int i=0; i < n; i++){
all +=i+1; // 1,2,3,4
sum += A[i];
}
all += n+1;
return all - sum;
}
}
Analysis
Code: 06:19:43 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");
class Solution {
public int solution(int[] A) {
// Implement your solution here
//1 + .. N+1 = 2+3+1+5 = 11
// 11 - x = 4 ,x = 7
// N이 4 1+2+3+4+5 = 15
// 11
// 15-11=4
int n = A.length;
int all = 0;
int sum = 0;
// n=4
// 0,1,2,3
for(int i=0; i < n; i++){
all +=i+1; // 1,2,3,4
sum += A[i];
}
all += n+1;
return all - sum;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.023 s
OK
2.
0.023 s
OK
1.
0.024 s
OK
2.
0.023 s
OK
1.
0.023 s
OK
2.
0.023 s
OK
1.
0.023 s
OK
2.
0.023 s
OK
3.
0.023 s
OK
1.
0.024 s
OK
expand all
Performance tests
1.
0.032 s
OK
1.
0.032 s
OK
1.
0.122 s
OK
2.
0.074 s
OK
3.
0.071 s
OK
1.
0.123 s
OK
1.
0.082 s
OK