Arrays: Left Rotation Code Challenge Solved

Input Format
The first line contains two space-separated integers n and d, the size of a and the number of left rotations you must perform.
The second line contains n space-separated integers a[i].

Output Format
Print a single line of n space-separated integers denoting the final state of the array after performing d left rotations.

Sample Input

4 1 2 3 4 5 
Enter fullscreen mode

Exit fullscreen mode

Sample Output

5 1 2 3 4 
Enter fullscreen mode

Exit fullscreen mode

Explanation
When we perform left rotations, the array undergoes the following sequence of changes:

[1, 2, 3, 4, 5] [2, 3, 4, 5, 1] [3, 4, 5, 1, 2] [4, 5, 1, 2, 3] [5, 1, 2, 3, 4] 
Enter fullscreen mode

Exit fullscreen mode

Solution

function rotLeft($a, $d) < $i = 0; $m = $d; if ($d >sizeof($a)) < $m = ($d % sizeof($a)); >while ($i < $m) < $a[] = $a[$i]; $i++; >array_splice($a, 0, $m); return implode(' ', $a); >