Codility - we test coders
Training center
Check out Codility training tasks
The total score represent quality of candidate's solution.
The detailed evaluation report pinpoints particular problems in the candidate's solution.
You have completed a Codility training test.
Tweet this!
I scored 100% in #php on @Codility!
https://codility.com/demo/take-sample-test/tape_equilibrium/

Sign up for our newsletter!
Like us on Facebook!
Training ticket

Session

ID: trainingYSRZQ4-YF4
Time limit: 120 min.

Status: closed

Created on: 2018-01-12 06:35 UTC
Started on: 2018-01-12 06:35 UTC
Finished on: 2018-01-12 06:35 UTC

Test score

100% 100 out of 100 points
Tasks in test Correctness Performance Task score
1
TapeEquilibrium Submitted in: PHP
100%
100%
100%
* The solution is being evaluated.
easy
score: 100 of 100 100 100 31,2,1,3,6,10,3,1,14,25
1. TapeEquilibrium
Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.
Task description

A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape.

Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].

The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|

In other words, it is the absolute difference between the sum of the first part and the sum of the second part.

For example, consider array A such that:

A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3

We can split this tape in four places:

  • P = 1, difference = |3 − 10| = 7
  • P = 2, difference = |4 − 9| = 5
  • P = 3, difference = |6 − 7| = 1
  • P = 4, difference = |10 − 3| = 7

Write a function:

function solution($A);

that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved.

For example, given:

A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3

the function should return 1, as explained above.

Assume that:

  • N is an integer within the range [2..100,000];
  • each element of array A is an integer within the range [−1,000..1,000].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Copyright 2009–2018 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used: PHP
Total time used: 1 minutes
Effective time used: 1 minutes
Notes:  
not defined yet
Task timeline
Code: 06:35:46 UTC, java, autosave
show code in pop-up
1234567891011
// 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) {
        // write your code in Java SE 8
    }
}
Code: 06:35:48 UTC, php, autosave
show code in pop-up
123456
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";

function solution($A) {
    // write your code in PHP7.0
}
Code: 06:35:51 UTC, php, verify, result: Passed
show code in pop-up
123456789101112131415
function solution($A) {
    $count = count($A);
    $left = $A[0];
    $right = array_sum($A) - $left;
    $min = abs($left - $right);
    for( $i = 1; $i < $count-1; $i++ ) {
        $left += $A[$i];
        $right -= $A[$i];
        $diff = abs($left - $right);
        if( $diff < $min ) {
            $min = $diff;
        }
    }
    return $min;
}
Analysis
expand all Example tests
▶
example
example test
✔
OK
1.
0.012 s
OK
Code: 06:35:53 UTC, php, final, score:  100
show code in pop-up
123456789101112131415
function solution($A) {
    $count = count($A);
    $left = $A[0];
    $right = array_sum($A) - $left;
    $min = abs($left - $right);
    for( $i = 1; $i < $count-1; $i++ ) {
        $left += $A[$i];
        $right -= $A[$i];
        $diff = abs($left - $right);
        if( $diff < $min ) {
            $min = $diff;
        }
    }
    return $min;
}
Analysis summary

The solution obtained perfect score.

Analysis
Detected time complexity:
O(N)
expand all Example tests
▶
example
example test
✔
OK
1.
0.012 s
OK
expand all Correctness tests
▶
double
two elements
✔
OK
1.
0.012 s
OK
2.
0.012 s
OK
▶
simple_positive
simple test with positive numbers, length = 5
✔
OK
1.
0.012 s
OK
▶
simple_negative
simple test with negative numbers, length = 5
✔
OK
1.
0.012 s
OK
▶
small_random
random small, length = 100
✔
OK
1.
0.012 s
OK
▶
small_range
range sequence, length = ~1,000
✔
OK
1.
0.012 s
OK
▶
small
small elements
✔
OK
1.
0.012 s
OK
expand all Performance tests
▶
medium_random1
random medium, numbers from 0 to 100, length = ~10,000
✔
OK
1.
0.012 s
OK
▶
medium_random2
random medium, numbers from -1,000 to 50, length = ~10,000
✔
OK
1.
0.016 s
OK
▶
large_ones
large sequence, numbers from -1 to 1, length = ~100,000
✔
OK
1.
0.044 s
OK
2.
0.044 s
OK
▶
large_random
random large, length = ~100,000
✔
OK
1.
0.044 s
OK
2.
0.044 s
OK
▶
large_sequence
large sequence, length = ~100,000
✔
OK
1.
0.028 s
OK
▶
large_extreme
large test with maximal and minimal values, length = ~100,000
✔
OK
1.
0.044 s
OK
2.
0.044 s
OK
3.
0.036 s
OK
Training center