Arrays: Left Rotation Code Challenge Solved

rezende79

Marcos Rezende

Posted on September 5, 2020

Arrays: Left Rotation Code Challenge Solved

A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].

Given an array a of n integers and a number d, perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Function Description
Complete the function rotLeft in the editor below. It should return the resulting array of integers.

rotLeft has the following parameter(s):

  • An array of integers a
  • An integer d, the number of rotations.

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].

Constraints
1 <= n <= 10^5
1 <= d <= n
1 <= a[i] <= 10^6

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);
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
rezende79
Marcos Rezende

Posted on September 5, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Arrays: Left Rotation Code Challenge Solved
codechallenge Arrays: Left Rotation Code Challenge Solved

September 5, 2020

Sparse Arrays Code Challenge Solved
codechallenge Sparse Arrays Code Challenge Solved

September 5, 2020

Jumping on the Clouds Code Challenge Solved
codechallenge Jumping on the Clouds Code Challenge Solved

September 5, 2020

2D Array - DS Code Challenge Solved
codechallenge 2D Array - DS Code Challenge Solved

September 5, 2020