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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C#
Time spent on task 21 minutes
Notes
not defined yet
Task timeline
Code: 13:21:46 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
in[] A_copy = new int[A.Length + 2];
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < A.Length; i++)
{
int elem = A[i];
A_copy[elem] = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(i = 1; i < A_copy.Length; i++)
{
if (A[i] != i)
{
return i;
}
}
}
Console.WriteLine("this is a debug message");
return 1;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 1 warnings Solution.cs(15,8): error CS1525: Unexpected symbol `in' Solution.cs(15,19): warning CS0642: Possible mistaken empty statement
Code: 13:22:28 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
int[] A_copy = new int[A.Length + 2];
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < A.Length; i++)
{
int elem = A[i];
A_copy[elem] = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(i = 1; i < A_copy.Length; i++)
{
if (A[i] != i)
{
return i;
}
}
}
//Console.WriteLine("this is a debug message");
return 1;
}
}
Analysis
Compile error
Compilation failed: 6 error(s), 0 warnings Solution.cs(33,17): error CS0103: The name `i' does not exist in the current context Solution.cs(33,24): error CS0103: The name `i' does not exist in the current context Solution.cs(35,23): error CS0103: The name `i' does not exist in the current context Solution.cs(35,29): error CS0103: The name `i' does not exist in the current context Solution.cs(37,28): error CS0103: The name `i' does not exist in the current context Solution.cs(33,43): error CS0103: The name `i' does not exist in the current context
Code: 13:22:40 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
int[] A_copy = new int[A.Length + 2];
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < A.Length; i++)
{
int elem = A[i];
A_copy[elem] = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < A_copy.Length; i++)
{
if (A[i] != i)
{
return i;
}
}
}
//Console.WriteLine("this is a debug message");
return 1;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[1, 3]
User test case 5:
[2, 3]
User test case 6:
[1, 2]
Analysis
expand all
User tests
1.
0.058 s
OK
function result: 1
function result: 1
1.
0.060 s
OK
function result: 1
function result: 1
1.
0.058 s
OK
function result: 1
function result: 1
1.
0.058 s
OK
function result: 2
function result: 2
1.
0.058 s
OK
function result: 2
function result: 2
1.
0.058 s
OK
function result: 2
function result: 2
Code: 13:25:05 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
int[] A_copy = new int[A.Length + 2];
//for (int i = 0; i< A_copy.Length;i++) { A_copy[i] = -1; }
for(int i = 0; i < A.Length; i++)
{
int elem = A[i];
A_copy[elem] = elem;
}
// you can replace this with O(log(n)) next time
for(int i = 1; i < A_copy.Length; i++)
{
if (A[i] != i)
{
return i;
}
}
return 1;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[1, 3]
User test case 5:
[2, 3]
User test case 6:
[1, 2]
Analysis
expand all
Example tests
1.
0.059 s
WRONG ANSWER,
got 1 expected 4
expand all
User tests
1.
0.058 s
OK
function result: 1
function result: 1
1.
0.053 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
1.
0.053 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
1.
0.059 s
OK
function result: 1
function result: 1
1.
0.059 s
OK
function result: 1
function result: 1
1.
0.062 s
OK
function result: 1
function result: 1
Code: 13:27:56 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
int[] A_copy = new int[A.Length + 2];
//for (int i = 0; i< A_copy.Length;i++) { A_copy[i] = -1; }
for(int i = 0; i < A.Length; i++)
{
int elem = A[i];
A_copy[elem] = elem;
}
// you can replace this with O(log(n)) next time
for(int i = 1; i < A_copy.Length; i++)
{
if (A_copy[i] != i)
{
return i;
}
}
return 1;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[1, 3]
User test case 5:
[2, 3]
User test case 6:
[1, 2]
Analysis
expand all
User tests
1.
0.058 s
OK
function result: 1
function result: 1
1.
0.058 s
OK
function result: 2
function result: 2
1.
0.058 s
OK
function result: 1
function result: 1
1.
0.058 s
OK
function result: 2
function result: 2
1.
0.059 s
OK
function result: 1
function result: 1
1.
0.058 s
OK
function result: 3
function result: 3
Code: 13:28:12 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
int[] A_copy = new int[A.Length + 2];
//for (int i = 0; i< A_copy.Length;i++) { A_copy[i] = -1; }
for(int i = 0; i < A.Length; i++)
{
int elem = A[i];
A_copy[elem] = elem;
}
// you can replace this with O(log(n)) next time
for(int i = 1; i < A_copy.Length; i++)
{
if (A_copy[i] != i)
{
return i;
}
}
return 1;
}
}
User test case 1:
[]
User test case 2:
[1]
User test case 3:
[2]
User test case 4:
[1, 3]
User test case 5:
[2, 3]
User test case 6:
[1, 2]
Analysis
expand all
User tests
1.
0.059 s
OK
function result: 1
function result: 1
1.
0.060 s
OK
function result: 2
function result: 2
1.
0.059 s
OK
function result: 1
function result: 1
1.
0.059 s
OK
function result: 2
function result: 2
1.
0.060 s
OK
function result: 1
function result: 1
1.
0.060 s
OK
function result: 3
function result: 3
Code: 13:28:17 UTC,
cs,
final,
score: 
100
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
int[] A_copy = new int[A.Length + 2];
//for (int i = 0; i< A_copy.Length;i++) { A_copy[i] = -1; }
for(int i = 0; i < A.Length; i++)
{
int elem = A[i];
A_copy[elem] = elem;
}
// you can replace this with O(log(n)) next time
for(int i = 1; i < A_copy.Length; i++)
{
if (A_copy[i] != i)
{
return i;
}
}
return 1;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.060 s
OK
2.
0.061 s
OK
1.
0.060 s
OK
2.
0.060 s
OK
1.
0.061 s
OK
2.
0.062 s
OK
1.
0.062 s
OK
2.
0.061 s
OK
3.
0.062 s
OK
1.
0.060 s
OK
expand all
Performance tests
1.
0.065 s
OK
1.
0.065 s
OK
1.
0.099 s
OK
2.
0.079 s
OK
3.
0.080 s
OK
1.
0.094 s
OK
1.
0.082 s
OK