BinaryGap

// Link to test: https://codility.com/demo/take-sample-test/binary_gap/ // "Find longest sequence of zeros in binary representation of an integer." function binary_gap($N){ $N = (string)decbin($N); $gap = $highest = $mark = 0; for($i = 0; $i < strlen($N); $i++){ if($N[$i] == 1){ $gap = $i - $mark; $mark = $i; if($gap > $highest) $highest = $gap - 1; } } return (int)$highest; }

CyclicRotation

// Link to test: https://codility.com/demo/take-sample-test/cyclic_rotation/ // "Rotate an array to the right by a given number of steps." function solution($A, $K){ if(empty($A)) return $A; for($K; $K > 0; $K--){ $temp = array_pop($A); array_unshift($A, $temp); } return $A; }

FrogJmp

// Link to test: https://codility.com/demo/take-sample-test/frog_jmp/ // "Count minimal number of jumps from position X to Y." function frog_jmp($X, $Y, $D){ $temp = intval(($Y - $X) / $D); if(($Y - $X) % $D) $temp++; return $temp; }

GenomicRangeQuery

// Link to test: https://codility.com/demo/take-sample-test/genomic_range_query/ // "Count minimal number of jumps from position X to Y." function genomic_range_query($S, $P, $Q) { for($i = 0; $i < sizeof($P);$i++){ $temp = substr($S, $P[$i], $Q[$i] - $P[$i] + 1); if(strpos($temp, 'A') !== false){ $min[$i] = 1; }elseif(strpos($temp, 'C') !== false){ $min[$i] = 2; }elseif(strpos($temp, 'G') !== false){ $min[$i] = 3; }elseif(strpos($temp, 'T') !== false){ $min[$i] = 4; } } return $min; }

MinAvgTwoSlice

// Link to test: https://codility.com/demo/take-sample-test/min_avg_two_slice/ // "Find the minimal average of any slice containing at least two elements." // "100/100" solution. Only checks for slices of 2 and 3 elements (which is not noted in the description of problem). function min_avg_two_slice($A){ $avg_2 = $avg_3 = $min_avg = $min_index = NULL; for($i = 0; $i < sizeof($A); $i++){ if(!isset($A[$i + 1])) break; $avg_2 = ($A[$i] + $A[$i + 1]) / 2; if($avg_2 < $min_avg || $min_avg === NULL){ $min_avg = $avg_2; $min_index = $i; } if(!isset($A[$i + 2])) break; $avg_3 = ($A[$i] + $A[$i + 1] + $A[$i + 2]) / 3; if($avg_3 < $min_avg || $min_avg === NULL){ $min_avg = $avg_3; $min_index = $i; } } return $min_index; } // Correct solution based on description of problem given (fails performance tests though). This will check for 2 or more elements (all possibilities). function min_avg_two_slice($A){ $size = sizeof($A); $min_avg = $min_index = NULL; for($i = 0; $i < $size; $i++){ for($j = 1; $j < $size - $i; $j++){ $avg = array_sum(array_slice($A, $i, $j + 1)) / ($j + 1); if($avg < $min_avg || $min_avg === NULL){ $min_avg = $avg; $min_index = $i; } } } return $min_index; }

OddOccurrencesInArray

// Link to test: https://codility.com/demo/take-sample-test/odd_occurrences_in_array/ // "Find value that occurs in odd number of elements." function odd_occurrences_in_array($A){ $A = array_count_values($A); foreach($A as $key => $num) if($num % 2 != 0) return $key; }

PassingCars

// Link to test: https://codility.com/demo/take-sample-test/passing_cars/ // "Count the number of passing cars on the road." // "100/100" solution. Always assumes the first car is headed east (which is not noted in the description of the problem). function passing_cars($A){ $east = $passing = 0; for($i = 0; $i < count($A); $i++){ if($A[$i] == 0) $east++; if($A[$i] != 0) $passing += $east; if($passing > 1000000000) return -1; } return $passing; } // Correct solution based on description of problem given. Checks whether or not the first car is headed east or west (scores 90/100). function passing_cars($A){ $key_cars = $passing = 0; for($i = 0; $i < count($A); $i++){ if($A[$i] == $A[0]) $key_cars++; if($A[$i] != $A[0]) $passing += $key_cars; if($passing > 1000000000) return -1; } return $passing; }

PermMissingElem

// Link to test: https://codility.com/demo/take-sample-test/perm_missing_elem/ // "Find the missing element in a given permutation." function perm_missing_elem($A){ return (((sizeof($A) + 1) * (sizeof($A) + 2)) / 2) - array_sum($A); }

TapeEquilibrium

// Link to test: https://codility.com/demo/take-sample-test/tape_equilibrium/ // "Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|." function tape_equilibrium($A) { $left = $A[0]; $right = array_sum($A) - $left; $minimum = abs($left - $right); for($i = 1; $i < sizeof($A) - 1; $i++){ $left += $A[$i]; $right -= $A[$i]; $difference = abs($left - $right); if($difference < $minimum) $minimum = $difference; } return $minimum; }