diff --git a/PermMissingElem.php b/PermMissingElem.php index 5d65f43..31a197a 100644 --- a/PermMissingElem.php +++ b/PermMissingElem.php @@ -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$v){ - if($k+$timesToRotate $v) { + if (($i+$K) < $arrayLenth) { + $index = $i+$K; + } else { + $index = ($i+$K)%$arrayLenth; + } + $resultArray[$index] = $v; + } + + return $resultArray; } \ No newline at end of file