Web Projects Consulting

TapeEquilibrium Demo Task at Codility.com

codility netbeansLesson 1 – Time Complexity – TapeEquilibrium demo task solution code written in php. Gives 100/100 score at the time of publishing. The description of the problem is copyrighted, so please see the following link for it: //codility.com/c/run/demoFKRFBE-5SC

Solution in php:

function solution($A) {
$sum1   = $A[0];
$sum2   = array_sum($A) - $sum1;
$found  = array('index'=>0, 'abs' => abs($sum1 - $sum2));
$c = count($A) - 1;
$i = 1;
while($i < $c){
$sum1 += $A[$i];
$sum2 -= $A[$i];
$abs = abs($sum1 - $sum2);
if($abs < $found['abs']){
$found['index'] = $i;
$found['abs'] = $abs;
}
$i++;
}
return $found['abs'];
}

Given “AS IS”, can be ported from other languages from solutions found on the internet, please use with care.

Please note: we think that codility.com does not give a correct assessment of your real-world programming skills. For instance, reading the below would bring more understanding why: //codility-test-questions.blogspot.com/2013/01/my-experience-with-codility-test.html

2 thoughts on “TapeEquilibrium Demo Task at Codility.com

  1. Chepry

    You’re using unnecessary array. Check this out:

    function solution($A) {
    $keys = count($A);
    $left = $A[0];
    $right = array_sum($A) – $left;
    $min = abs($left – $right);
    for ($i = 1; $i < $keys – 1; $i++) {
    $left += $A[$i];
    $right -= $A[$i];
    $diff = abs($left – $right);
    $min = ($diff < $min) ? $diff : $min;
    }
    return $min;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.