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:
function solution($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 PHP
Time spent on task 30 minutes
Notes
not defined yet
Task timeline
Code: 11:45:52 UTC,
php,
verify,
result: Passed
Analysis
Code: 11:46:29 UTC,
php,
verify,
result: Passed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
return count($A);
}
User test case 1:
[3, 3, 3, 4]
Analysis
Code: 12:01:24 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
for($i=0;$i<count($A);$i++){
if($A[$i]==$i){
return test($i,$A);
}else{
return 1;
}
}
}
function test($i, $A){
if(count($A)==1){
return test($i, $B);
}else{
for($j=0;$j<count($A);$j++){
if($i!=$j){
$B[]=$A[$j];
}
}
}
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.026 s
WRONG ANSWER,
got 1 expected 4
Code: 12:02:31 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
for($i=0;$i<count($A);$i++){
$k=$i+1;
if($A[$i]==$k){
return test($i,$A);
}else{
return 1;
}
}
}
function test($i, $A){
if(count($A)==1){
return test($i, $B);
}else{
for($j=0;$j<count($A);$j++){
if($i!=$j){
$B[]=$A[$j];
}
}
}
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 1 expected 4
Code: 12:07:12 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
return array_sum($A);
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 11 expected 4
Code: 12:08:44 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=0;$i<count($i);$i++){
$sum=$A[$i]+$sum;
}
return $sum;
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 2 expected 4
Code: 12:08:55 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=0;$i<count($A);$i++){
$sum=$A[$i]+$sum;
}
return $sum;
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 11 expected 4
Code: 12:09:13 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=0;$i<count($A)+1;$i++){
$sum=$A[$i]+$sum;
}
return $sum;
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 11 expected 4
stderr:
PHP Notice: Undefined offset: 4 in /tmp/user.php on line 11 Notice: Undefined offset: 4 in /tmp/user.php on line 11
expand all
User tests
1.
0.025 s
OK
function result: 13
function result: 13
stderr:
PHP Notice: Undefined offset: 4 in /tmp/user.php on line 11 Notice: Undefined offset: 4 in /tmp/user.php on line 11
Code: 12:09:47 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=0;$i<count($A)+1;$i++){
$sum=$sum+$i;
}
return $sum;
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 10 expected 4
Code: 12:10:00 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=0;$i<count($A)+2;$i++){
$sum=$sum+$i;
}
return $sum;
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 15 expected 4
Code: 12:10:22 UTC,
php,
verify,
result: Failed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=1;$i<count($A)+1;$i++){
$sum=$sum+$i;
}
return $sum;
}
User test case 1:
[3, 3, 3, 4]
Analysis
expand all
Example tests
1.
0.025 s
WRONG ANSWER,
got 10 expected 4
Code: 12:10:43 UTC,
php,
verify,
result: Passed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=1;$i<count($A)+2;$i++){
$sum=$sum+$i;
}
return $sum-$s;
}
User test case 1:
[3, 3, 3, 4]
Analysis
Code: 12:11:09 UTC,
php,
verify,
result: Passed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=1;$i<count($A)+2;$i++){
$sum=$sum+$i;
}
return $sum-$s;
}
User test case 1:
[3, 6, 2, 4, 1, 8, 10, 7]
Analysis
Code: 12:11:26 UTC,
php,
verify,
result: Passed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=1;$i<count($A)+2;$i++){
$sum=$sum+$i;
}
return $sum-$s;
}
User test case 1:
[3, 6, 2, 4, 1, 8, 10, 7, 5]
Analysis
Code: 12:11:34 UTC,
php,
verify,
result: Passed
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$s=array_sum($A);
$sum=0;
for($i=1;$i<count($A)+2;$i++){
$sum=$sum+$i;
}
return $sum-$s;
}
User test case 1:
[3, 6, 2, 4, 1, 8, 10, 7, 5]
Analysis
Code: 12:11:37 UTC,
php,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))