# Codility cyclic rotation solution in PHP

## Problem statement

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

## My Solution

function solution($A,$K) {
// when array is empty or has only one element
if(count($A) == 0 || count($A) == 1){
return $A; } //runs k times for($j=1; $j<=$K; $j++){$last_element = $A[count($A)-1];
//runs for each element
for($i=(count($A)-1); $i>0;$i--){
$A[$i] = $A[$i-1];
}
$A[0] =$last_element;
}
return $A; }$A = [1, 2, 3, 4];
$K = 4;$result = solution($A,$K);
print_r($result);  ## Output Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )  • How about rotating the 4-element A array K=4 million times...? Commented Mar 16, 2018 at 8:07 • Welcome de CodeReview.SE! Please add a link to the original problem in your question. Commented Mar 16, 2018 at 16:54 ## 3 Answers You could use array functions. They simplify and speed up the handling of arrays. See: http://php.net/manual/en/ref.array.php So your code could become: function rotateArray($inputArray,$rightShiftCount) // shift all elements of the array to the right a number of times { // extract the part of the array to move to the front$partToMove = array_splice($inputArray,-($rightShiftCount % count($inputArray))); // return extract part followed by what was left of the array return array_merge($partToMove,$inputArray); }  This function does not check its arguments, if needed you can add that. Notice that I have used sensible names for my variables instead of $A and $K. This is intentional. 1. solution may be an acceptable function name in a one-off coding challenge, but in a real application try to give your functions an intuitive name that describes its functionality. 2. Use type declarations with your input parameters and return values to enforce stable coding practices in your application. 3. Perform as many early returns as possible to reduce time complexity and improve efficiency. 4. An array with a size of zero or one cannot possibly be rotated, so return early. 5. If the number of popShifts required has a remainder of zero after dividing it by the array size, then return early because whether you call the rotating functions or not, the output will be identical to the input. 6. Avoid declaring single-use variables. 7. Cut off the appropriate number of elements from the back of the array and prepend them to what is left. Code: (Demo) function popUnshift(array$indexedArray, int $popShiftsCount): array {$count = count($indexedArray); if ($count < 2) {
return $indexedArray; }$remainder = $popShiftsCount %$count;
if (!$remainder) { return$indexedArray;
}
return array_merge(
array_splice($indexedArray, -$remainder),
$indexedArray ); }  Actually, you don't need to rotate the initial array K times: <?php function solution($A, $K) { // when array is empty or has only one element if (count($A) == 0 || count($A) == 1) { return$A;
}

// The number of rotations needed
$rotateTimes =$K % count($A); //runs $rotateTimes times
for ($j = 1;$j <= $rotateTimes;$j++) {
$last_element =$A[count($A) - 1]; //runs for each element for ($i = (count($A) - 1);$i > 0; $i--) {$A[$i] =$A[$i - 1]; }$A[0] = $last_element; } return$A;
}

• This review is rather "light" on the "review" part. Commented Jun 14, 2022 at 22:13