Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion PermMissingElem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@
echo solution([2,3,4,1]);
function solution($A){
/*
* Solution 2: Correctness 100%; performance 100%
* Solution 3: Correctness 100%; performance 100%
*/
$arrayCount = count($A);
$lastNumber = $arrayCount+1;
$firstNumber = 1;

//Sum of given array
$sumA = array_sum($A);
// Sum of arithmetic series
$sumOfSeries = (($arrayCount+1)*($firstNumber + $lastNumber)) / 2;

return (int)($sumOfSeries - $sumA);

/*
* Solution 2: Correctness 100%; performance 100%
*
sort($A);
for($i=0;$i<count($A); $i++){
if($A[$i]!=$i+1){
Expand Down
28 changes: 14 additions & 14 deletions cyclicRotation.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
var_dump(solution([3, 8, 9, 7, 6], 3));
function solution($A, $K){
$timesToRotate = $K%count($A);
$array=array();
//$array = new SplFixedArray(count($A));
foreach($A as $k=>$v){
if($k+$timesToRotate<count($A)){
$array[$k+$timesToRotate]=$v;
}
else {
$array[($k+$timesToRotate)-count($A)]=$v;
}
}
ksort($array);
return $array;
function solution($A, $K) {
// write your code in PHP7.0
$resultArray = [];
$arrayLenth = count($A);
foreach ($A as $i => $v) {
if (($i+$K) < $arrayLenth) {
$index = $i+$K;
} else {
$index = ($i+$K)%$arrayLenth;
}
$resultArray[$index] = $v;
}
return $resultArray;
}